| 关键词: nbsp mPaint Context bgColor Android private attrs public Canvas 画笔 |
前几个月刚接触Android的时候做了一个小项目,其中也用到了类似刮刮乐的效果,现在把代码贴出来首先要做一个类似橡皮擦的东西吧,然后才能把纸上的笔迹擦除[java] view plaincopy/** * FileName: SplashActivity.java * * @desc 橡皮擦功能,类似刮刮乐效果 * @author HTP * @Date 20140311 * @version 1.00 */ public class Text_Rubbler extends TextView { private float TOUCH_TOLERANCE; // 填充距离,使线条更自然,柔和,值越小,越柔和。 // private final int bgColor; // 位图 private Bitmap mBitmap; // 画布 private Canvas mCanvas; // 画笔 private Paint mPaint; private Path mPath; private float mX, mY; private boolean isDraw = false; public Text_Rubbler(Context context) { /** * @param context 上下文 */ super(context); } public Text_Rubbler(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); // bgColor = // attrs.getAttributeIntValue("http://schemas.android.com/apk/res/android", // "textColor", 0xFFFFFF); // System.out.println("Color:"+bgColor); } public Text_Rubbler(Context context, AttributeSet attrs) { super(context, attrs); // bgColor = // attrs.getAttributeIntValue("http://schemas.android.com/apk/res/android", // "textColor", 0xFFFFFF); // System.out.println(bgColor); // System.out.println(attrs.getAttributeValue("http://schemas.android.com/apk/res/android", // "layout_width")); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); if (isDraw) { mCanvas.drawPath(mPath, mPaint); // mCanvas.drawPoint(mX, mY, mPaint); canvas.drawBitmap(mBitmap, 0, 0, null); } } /** * 开启檫除功能 * * @param bgColor * 覆盖的背景颜色 * @param paintStrokeWidth * 触点(橡皮)宽度 * @param touchTolerance * 填充距离,值越小,越柔和。 */ public void beginRubbler(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(); ; |
|
声明:文章版权归原作者所有 部分文章转自互联网 如有侵权请联系
[邮箱地址] 删除
|