自定义View2 刮刮卡效果

本文介绍了一种在Android中实现刮刮卡效果的方法,并提供了一个自定义View的示例代码。通过使用PorterDuffXfermode实现XOR效果,可以达到刮刮卡的视觉效果。文中还包含了如何保存生成的Bitmap到本地。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

效果如下图:


这里需要用到上一节中PorterDuffXfermode(把传递改头换面的服务员) xfermode
内容请参考http://note.youdao.com/share/?id=3f61125240ed4553df90532d42192ccb&type=note

刮刮需要用到的是Xor的效果。

刮刮卡的自定义view

public class GuaGuaView extends View{
private Bitmap mBitmap;
private Paint mPaintRect;
private Paint mPaintLine;
private int width;
private int height;
private Canvas mCanvasBitmap;
private Bitmap mBitMapBackground;
private float x;
private float y;
private Path mPhth;
private int mLastX;
private int mLastY;
public GuaGuaView(Context context) {
super(context);
}

public GuaGuaView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.GuaGuaView);
int paintwidth = a.getDimensionPixelOffset(R.styleable.GuaGuaView_guaguaview_paintwidth, 30);
BitmapDrawable dra = (BitmapDrawable) a.getDrawable(R.styleable.GuaGuaView_guaguaview_background);
if(dra!=null){
mBitMapBackground = dra.getBitmap();
}else {
mBitMapBackground = BitmapFactory.decodeResource(getResources(), R.mipmap.aa);
}
mPaintRect = new Paint();
mPaintRect.setColor(Color.GREEN);
mPaintLine = new Paint();
mPaintLine.setColor(Color.YELLOW);
mPaintLine.setStrokeWidth(paintwidth);
PorterDuffXfermode xfermode = new PorterDuffXfermode(PorterDuff.Mode.XOR);
mPaintLine.setAntiAlias(true);
mPaintLine.setDither(true);
mPaintLine.setStyle(Paint.Style.STROKE);
mPaintLine.setStrokeJoin(Paint.Join.ROUND); // 圆角 连接点使用的样式
mPaintLine.setStrokeCap(Paint.Cap.ROUND); // 圆角 覆盖时使用的样式
mPaintLine.setXfermode(xfermode);
mPhth= new Path();
}

@Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
int x = (int) event.getX();
int y = (int) event.getY();
switch (action)
{
case MotionEvent.ACTION_DOWN:
mLastX = x;
mLastY = y;
mPhth.moveTo(mLastX, mLastY);
break;
case MotionEvent.ACTION_MOVE:
int dx = Math.abs(x - mLastX);
int dy = Math.abs(y - mLastY);
if (dx > 5 || dy > 5)
mPhth.lineTo(x, y);

mLastX = x;
mLastY = y;
break;
}

invalidate();
return true;
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
width = mBitMapBackground.getWidth();
height = mBitMapBackground.getHeight();
mBitmap = Bitmap.createBitmap(width,height, Bitmap.Config.ARGB_8888);
setMeasuredDimension(width, height); //设置view的宽和高
mCanvasBitmap = 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, mBitMapBackground.getWidth(), mBitMapBackground.getHeight()), null);
mCanvasBitmap.drawRect(0, 0, mBitMapBackground.getWidth(), mBitMapBackground.getHeight(), mPaintRect);
mCanvasBitmap.drawPath(mPhth,mPaintLine);
canvas.drawBitmap(mBitmap,0,0,null);
}
}

如何保存Bitmap

mButtonSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mGuaGuaView.setDrawingCacheEnabled(true);
Bitmap bitmap = mGuaGuaView.getDrawingCache(true);
File file = new File(Environment.getExternalStorageDirectory(),System.currentTimeMillis()+".jpg");
if (!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
bitmap.compress(Bitmap.CompressFormat.JPEG,100,new FileOutputStream(file));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
});


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值