80,359
社区成员
发帖
与我相关
我的任务
分享
private int width;
private int height;
private Bitmap mBitmap;
private Paint mPaintCircel;
private Paint mPaintRect;
private Canvas canvasBit;
private Bitmap mBitmapBackground;
private Bitmap mBitmapBackground1;
private Matrix matrix;
public boolean isfirstpaint = true;
public MyBitmapTwo(Context context) {
super(context);
}
public MyBitmapTwo(Context context, AttributeSet attrs) {
super(context, attrs);
path = new Path();
mBitmapBackground = ((BitmapDrawable) getBackground()).getBitmap();
matrix = new Matrix();
mPaintCircel = new Paint();
mPaintCircel.setColor(Color.parseColor("#00000000"));
mPaintCircel.setAlpha(0);
mPaintRect = new Paint();
mPaintRect.setColor(Color.parseColor("#666666"));
mPaintRect.setAlpha(80);
PorterDuffXfermode mode = new PorterDuffXfermode(PorterDuff.Mode.XOR);//交叉的部分显示背景
mPaintCircel.setXfermode(mode);
mPaintCircel.setStrokeJoin(Paint.Join.ROUND);//设置线段中间用圆形填充
mPaintCircel.setStrokeCap(Paint.Cap.ROUND);//设置线段开头是圆形
mPaintCircel.setStrokeWidth(100);
mPaintCircel.setStyle(Paint.Style.FILL_AND_STROKE);//非空心
mPaintCircel.setPathEffect(new CornerPathEffect(360));
mPaintCircel.setAntiAlias(true);//消锯齿
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
width = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec);
height = getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec);
setMeasuredDimension(width, height);//设置画布的大小,长和宽
mBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
canvasBit = new Canvas(mBitmap);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawBitmap(mBitmapBackground, new Rect(0, 0, mBitmapBackground.getWidth(), mBitmapBackground.getHeight()), new Rect(0, 0, width, height), null);//用图片做背景
canvasBit.drawPath(path, mPaintCircel);//按路径画图形,
canvasBit.drawRect(0, 0, width, height, mPaintRect);//跟view大小一样的透明背景
canvas.drawBitmap(mBitmap, 0, 0, null);
}
float x;
float y;
float old_x;
float old_y;
private Path path;
@Override//点击事件
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
x = event.getX();
y = event.getY();
path.moveTo(x, y);
invalidate();
old_x = x;
old_y = y;
return true;
case MotionEvent.ACTION_MOVE:
x = event.getX();
y = event.getY();
path.moveTo(old_x, old_y);
path.lineTo(x, y);
invalidate();
old_x = x;
old_y = y;
return true;
}
return super.onTouchEvent(event);
}