android 简单涂鸦

部分代码借鉴网上。


看图先:


主界面Activity :

package com.mj.candraw;


import android.app.Activity;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;


public class CanDrawActivity extends Activity {
/** Called when the activity is first created. */


private MyDrawView mView = null;


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);


InitView();


setContentView(mView);
}


private void InitView() {
LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);


mView = new MyDrawView(CanDrawActivity.this);


mView.setLayoutParams(params);

mView.setFocusable(true);
}
}



自定义涂鸦控件,MyDrawView :

package com.mj.candraw;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;


public class MyDrawView extends View {


public static final String TAG = MyDrawView.class.getSimpleName();


private Context mContext = null;
private Path mPath = null;
private Paint mPaint = null;
private Paint mBitmapPaint = null;// 画布的画笔
private Canvas mCanvas = null;
private Bitmap mBitmap = null;
private int mWidth = 0;
private int mHeight = 0;


private int mX, mY;
private final int SPACE = 4;


private boolean mInit = false;


public MyDrawView(Context context) {
super(context);
// TODO Auto-generated constructor stub
// InitView(context);
mContext = context;
}


public MyDrawView(Context context, AttributeSet a) {
super(context, a);
// TODO Auto-generated constructor stub
// InitView(context);
mContext = context;
}


@Override
protected void onLayout(boolean changed, int left, int top, int right,
int bottom) {
// TODO Auto-generated method stub
Log.i(TAG, "onLayout ");
InitView(mContext);
super.onLayout(changed, left, top, right, bottom);
}


private void InitView(Context context) {


mWidth = this.getWidth();
mHeight = this.getHeight();
Log.i(TAG, "mWidth=" + mWidth + " mHeight=" + mHeight);


mBitmap = Bitmap.createBitmap(mWidth, mHeight, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);


mBitmapPaint = new Paint(Paint.DITHER_FLAG);


mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);// 设置外边缘
mPaint.setStrokeCap(Paint.Cap.SQUARE);// 形状
mPaint.setStrokeWidth(5);// 画笔宽度


mInit = true;
}


@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
int x = (int) event.getX();
int y = (int) event.getY();


switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
touch_down(x, y);
break;
}
case MotionEvent.ACTION_UP: {
touch_up(x, y);
break;
}
case MotionEvent.ACTION_MOVE: {
touch_move(x, y);
break;
}
}
return true;
}


private void touch_down(int x, int y) {
Log.i(TAG, "touch_down x=" + x + " y=" + y);
mPath = new Path();
mPath.moveTo(x, y);
mX = x;
mY = y;
this.postInvalidate();
}


private void touch_move(int x, int y) {
// Log.i(TAG, "touch_down x=" + x + " y=" + y);
int _x = Math.abs(x - mX);
int _y = Math.abs(y - mY);
if (_x > SPACE || _y > SPACE) {
mPath.lineTo(x, y);
mX = x;
mY = y;
this.postInvalidate();
}
}


private void touch_up(int x, int y) {
Log.i(TAG, "touch_up x=" + x + " y=" + y);
mPath.lineTo(x, y);
mCanvas.drawPath(mPath, mPaint);
this.postInvalidate();
}


@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
if (!mInit)
return;
canvas.drawColor(0xFFAAAAAA);
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);


if (mPath != null) {
canvas.drawPath(mPath, mPaint);
}
super.onDraw(canvas);
}


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值