Android_刮刮乐刮奖
1.效果图
2.代码实现
public class ErinieShow extends RelativeLayout { Context context; RelativeLayout bg; MyView MyView; int level; public ErinieShow(Context context, int level) { super(context); this.context = context; this.level = level; bg = new RelativeLayout(context); MyView = new MyView(context); bg.addView(MyView); addView(bg); int[] resolution = PhoneUtil.getResolution(context); RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams( resolution[0], PhoneUtil.getFitHeight(context, 125)); bg.setId(100001); bg.setLayoutParams(params); MyView.setLayoutParams(params); switch (level) { case 0: bg.setBackgroundResource(R.drawable.rewardlevel0); break; case 1: bg.setBackgroundResource(R.drawable.rewardlevel1); break; case 2: bg.setBackgroundResource(R.drawable.rewardlevel2); break; default: bg.setBackgroundResource(R.drawable.rewardlevel3); break; } MyView.init(Color.parseColor("#d3d3d3"), 30, 10); } }
public class MyView extends TextView { private float TOUCH_TOLERANCE; // 填充距离,使线条更自然,柔和,值越小,越柔和。 private Bitmap mBitmap; private Canvas mCanvas; private Paint mPaint; private Path mPath; private float mX, mY; private boolean isDraw = false; int time=0; public MyView(Context context) { super(context); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); if (isDraw) { mCanvas.drawPath(mPath, mPaint); canvas.drawBitmap(mBitmap, 0, 0, null); } } /** * 开启檫除功能 * * @param bgColor * 覆盖的背景颜色 * @param paintStrokeWidth * 触点(橡皮)宽度 * @param touchTolerance * 填充距离,值越小,越柔和。 */ public void init(final int bgColor, final int paintStrokeWidth, float touchTolerance) { TOUCH_TOLERANCE = touchTolerance; // 设置画笔 mPaint = new Paint(); // mPaint.setAlpha(0); // 画笔划过的痕迹就变成透明色了 mPaint.setColor(Color.BLACK); // 此处不能为透明色 mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT)); // 或者 // mPaint.setAlpha(0); // mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN)); mPaint.setAntiAlias(true); mPaint.setDither(true); mPaint.setStyle(Paint.Style.STROKE); mPaint.setStrokeJoin(Paint.Join.ROUND); // 前圆角 mPaint.setStrokeCap(Paint.Cap.ROUND); // 后圆角 mPaint.setStrokeWidth(paintStrokeWidth); // 笔宽 // 痕迹 mPath = new Path(); // 覆盖 LayoutParams layoutParams = getLayoutParams(); int height = layoutParams.height; int width; if (getLayoutParams().width == LayoutParams.MATCH_PARENT) { width = 700; } else { width = layoutParams.width; } mBitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888); mCanvas = new Canvas(mBitmap); mCanvas.drawColor(bgColor); isDraw = true; } @Override public boolean onTouchEvent(MotionEvent event) { if (!isDraw) { return true; } switch (event.getAction()) { case MotionEvent.ACTION_DOWN: // 触点按下 // touchDown(event.getRawX(),event.getRawY()); touchDown(event.getX(), event.getY()); invalidate(); break; case MotionEvent.ACTION_MOVE: // 触点移动 touchMove(event.getX(), event.getY()); invalidate(); break; case MotionEvent.ACTION_UP: // 触点弹起 touchUp(event.getX(), event.getY()); invalidate(); break; default: break; } return true; } private void touchDown(float x, float y) { mPath.reset(); mPath.moveTo(x, y); mX = x; mY = y; } private void touchMove(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 touchUp(float x, float y) { mPath.lineTo(x, y); mCanvas.drawPath(mPath, mPaint); mPath.reset(); } }
源码下载http://download.csdn.net/detail/strawberry2013/7682865
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。