public class Paint extends PaintActivity implements
ColorPickerDialog.OnColorChangedListener {
private Paint mPaint;
private PaintView view;
private int wid;
private int wsize;
private int pref;
private int RESULT_TOGGLE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
view = new PaintView(this);
setContentView(R.layout.paint_layout);
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setColor(Color.parseColor("black"));
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(12);
Button close = (Button) findViewById(R.id.closePaint);
close.setOnClickListener(new View.OnClickListener() {
public void onClick(View View) {
//save();
//updateWidgets();
setResult(RESULT_OK);
finish();
}
});
Button toggle = (Button) findViewById(R.id.togglePaint);
toggle.setOnClickListener(new View.OnClickListener() {
public void onClick(View View) {
//save();
if (pref == 0)
//launchText();
else
{
setResult(RESULT_TOGGLE);
finish();
}
}
});
}
else
{
//Problem - just bail
TextView tv = new TextView(this);
tv.setText("Error.");
setContentView(tv);
}
}
public class PaintView extends View {
private Bitmap mBitmap;
private Canvas mCanvas;
private Path mPath;
private Paint mBitmapPaint;
public PaintView(Context c) {
super(c);
initialize();
}
public PaintView(Context c, AttributeSet s) {
super(c,s);
initialize();
}
public void initialize() {
mBitmap = Bitmap.createBitmap(320, 430,
Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
mPath = new Path();
mBitmapPaint = new Paint(Paint.DITHER_FLAG);
}
public void loadImage() {
String path = "/sdcard/image.jpg";
Bitmap b = BitmapFactory.decodeFile(path);
Drawable d = new BitmapDrawable(b);
Bitmap bitmap = Bitmap.createBitmap(320, 430,
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
d.setBounds(0, 0, 320, 430);
d.draw(canvas);
mBitmap = bitmap;
mCanvas = canvas;
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int
oldh) {
super.onSizeChanged(w, h, oldw, oldh);
}
@Override
protected void onDraw(Canvas canvas) {
Log.w("onDraw","");
canvas.drawColor(Color.parseColor("white"));
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
canvas.drawPath(mPath, mPaint);
}
private float mX, mY;
private static final float TOUCH_TOLERANCE = 4;
private void touch_start(float x, float y) {
mPath.reset();
mPath.moveTo(x, y);
mX = x;
mY = y;
}
private void touch_move(float x, float y) {
float dx = Math.abs(x - mX);
float dy = Math.abs(y - mY);
if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
mX = x;
mY = y;
}
}
private void touch_up() {
mPath.lineTo(mX, mY);
mCanvas.drawPath(mPath, mPaint);
mPath.reset();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
touch_start(x, y);
invalidate();
break;
case MotionEvent.ACTION_MOVE:
touch_move(x, y);
invalidate();
break;
case MotionEvent.ACTION_UP:
touch_up();
invalidate();
break;
}
return true;
}
public void clear() {
mBitmap = Bitmap.createBitmap(320, 480,
Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
invalidate();
}
}
////////////END OF PaintView////////////////////
////////////CONTINUE Paint/////////////////////
private void save()
{
Bitmap b = Bitmap.createBitmap( view.getWidth(),
view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas( b );
view.draw( c );
FileOutputStream fos = null;
try {
fos = new FileOutputStream( "/sdcard/image" + ".jpg" );
if ( fos != null )
{
b.compress(Bitmap.CompressFormat.PNG, 100, fos );
fos.close();
}
} catch( Exception e )
{
Log.e("Save View", "Exception: " + e.toString() );
}
}
public void colorChanged(int color) {
mPaint.setColor(color);
}
}
///////////////paint_layout.xml///////////////////
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/
android"
android:id="@+id/paint"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<View class="com.app.name.Paint$PaintView"
android:id="@+id/PaintView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<Button android:id="@+id/closePaint"
android:text="Close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left|bottom" />
<Button android:id="@+id/togglePaint"
android:text="Toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|bottom" />
</FrameLayout>
api demo에 있는 fingerpaint를 갖다 쓰려고 하는데요
xml에 custom view를 작성해놓고요 제가 setcontectview(view)했을때는 아무 문제가 없는데
setcontentview(paint_layout.xml 했을때는 안되네요 ㅜㅜ
검은화면에 버튼만 있구요..
- 원본 텍스트 보기 -