3、自定义控件-----刮刮奖

  1 import android.content.Context;

  2 import android.graphics.Bitmap;

  3 import android.graphics.Bitmap.Config;

  4 import android.graphics.Canvas;

  5 import android.graphics.Color;

  6 import android.graphics.Paint;

  7 import android.graphics.Path;

  8 import android.graphics.PorterDuff;

  9 import android.graphics.PorterDuffXfermode;

 10 import android.util.AttributeSet;

 11 import android.view.MotionEvent;

 12 import android.widget.TextView;

 13 

 14 /**  15  * 橡皮檫功能,类似刮刮乐效果  16 */  17 public class Text_Rubbler extends TextView {  18  19 // 填充距离,使线条更自然,柔和,值越小,越柔和。  20 private float TOUCH_TOLERANCE;  21  22 private Bitmap mBitmap;  23 private Canvas mCanvas;  24 private Paint mPaint;  25 private Path mPath;  26 private float mX, mY;  27  28 private boolean isDraw = false;  29  30 public Text_Rubbler(Context context) {  31 super(context);  32  }  33  34 public Text_Rubbler(Context context, AttributeSet attrs, int defStyle) {  35 super(context, attrs, defStyle);  36  }  37  38 public Text_Rubbler(Context context, AttributeSet attrs) {  39 super(context, attrs);  40  }  41  42  @Override  43 protected void onDraw(Canvas canvas) {  44 super.onDraw(canvas);  45 if (isDraw) {  46  mCanvas.drawPath(mPath, mPaint);  47 // mCanvas.drawPoint(mX, mY, mPaint);  48 canvas.drawBitmap(mBitmap, 0, 0, null);  49  }  50  }  51  52 /**  53  * 开启檫除功能  54  *  55  * @param bgColor  56  * 覆盖的背景颜色  57  * @param paintStrokeWidth  58  * 触点(橡皮)宽度  59  * @param touchTolerance  60  * 填充距离,值越小,越柔和。  61 */  62 public void beginRubbler(final int bgColor, final int paintStrokeWidth,  63 float touchTolerance) {  64 TOUCH_TOLERANCE = touchTolerance;  65 // 设置画笔  66 mPaint = new Paint();  67 mPaint.setAlpha(0);  68 // 画笔划过的痕迹就变成透明色了  69 mPaint.setColor(Color.BLACK); // 此处不能为透明色  70 mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));  71 // 或者  72 // mPaint.setAlpha(0);  73 // mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));  74  75 mPaint.setAntiAlias(true);  76 mPaint.setDither(true);  77  mPaint.setStyle(Paint.Style.STROKE);  78 mPaint.setStrokeJoin(Paint.Join.ROUND); // 前圆角  79 mPaint.setStrokeCap(Paint.Cap.ROUND); // 后圆角  80 mPaint.setStrokeWidth(paintStrokeWidth); // 笔宽  81  82 // 痕迹  83 mPath = new Path();  84  ;  85 // 覆盖  86 // if (getLayoutParams().width == LayoutParams.FILL_PARENT) {  87 //  88 // }  89 mBitmap = Bitmap.createBitmap(getLayoutParams().width,  90  getLayoutParams().height, Config.ARGB_8888);  91 mCanvas = new Canvas(mBitmap);  92  93  mCanvas.drawColor(bgColor);  94 isDraw = true;  95  }  96  97  @Override  98 public boolean onTouchEvent(MotionEvent event) {  99 if (!isDraw) { 100 return true; 101  } 102 switch (event.getAction()) { 103 case MotionEvent.ACTION_DOWN: // 触点按下 104 // touchDown(event.getRawX(),event.getRawY()); 105  touchDown(event.getX(), event.getY()); 106  invalidate(); 107 break; 108 case MotionEvent.ACTION_MOVE: // 触点移动 109  touchMove(event.getX(), event.getY()); 110  invalidate(); 111 break; 112 case MotionEvent.ACTION_UP: // 触点弹起 113  touchUp(event.getX(), event.getY()); 114  invalidate(); 115 break; 116 default: 117 break; 118  } 119 return true; 120  } 121 122 private void touchDown(float x, float y) { 123  mPath.reset(); 124  mPath.moveTo(x, y); 125 mX = x; 126 mY = y; 127  } 128 129 private void touchMove(float x, float y) { 130 float dx = Math.abs(x - mX); 131 float dy = Math.abs(y - mY); 132 if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) { 133 mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2); 134 mX = x; 135 mY = y; 136  } 137  } 138 139 private void touchUp(float x, float y) { 140  mPath.lineTo(x, y); 141  mCanvas.drawPath(mPath, mPaint); 142  mPath.reset(); 143  } 144 145 }

 

 1 <?xml version="1.0" encoding="utf-8"?>

 2 <RelativeLayout 

 3     xmlns:android="http://schemas.android.com/apk/res/android"

 4     android:layout_width="fill_parent"

 5     android:layout_height="fill_parent"

 6     android:background="@drawable/make_bg"

 7     android:orientation="vertical" >

 8 

 9     <RelativeLayout

10         android:id="@+id/prize_title"

11         android:layout_width="wrap_content"

12         android:layout_height="wrap_content"

13         android:layout_centerHorizontal="true"

14         android:layout_centerVertical="true"

15         android:layout_margin="10dp" >

16 

17         <TextView

18             android:id="@+id/prize_to1"

19             android:layout_width="wrap_content"

20             android:layout_height="wrap_content"

21             android:text="活动规则"

22             android:textColor="#ffd249"

23             android:textSize="20sp" />

24 

25         <TextView

26             android:id="@+id/prize_to2"

27             android:layout_width="wrap_content"

28             android:layout_height="wrap_content"

29             android:layout_below="@+id/prize_to1"

30             android:text="1.活动时间\n   2013年11月21日起~2014年1月1日止\n2.活动内容\n   活动期间,登陆游戏即有机会抽取4999红包大奖"

31             android:textColor="#fff" />

32     </RelativeLayout>

33 

34     <ImageView

35         android:id="@+id/ima"

36         android:layout_width="wrap_content"

37         android:layout_height="wrap_content"

38         android:layout_above="@+id/prize_title"

39         android:layout_centerHorizontal="true"

40         android:background="@drawable/mm_gold"

41         android:padding="10dp" />

42 

43     <!-- 44  必须设置 45  android:layout_width 46  android:layout_height 47  的值为常量 48 -->

49 

50     <org.delmore.rubble.Text_Rubbler 51 android:id="@+id/rubbler" 52  android:layout_width="235dip" 53  android:layout_height="60dip" 54  android:layout_below="@+id/prize_title" 55  android:layout_centerHorizontal="true" 56  android:gravity="center" 57  android:padding="10dp" 58  android:text="怎么可能\n会中奖!" 59  android:textColor="#FFF" /> 

61 </RelativeLayout>
  1 import android.content.Context;

  2 import android.graphics.Bitmap;

  3 import android.graphics.Bitmap.Config;

  4 import android.graphics.Canvas;

  5 import android.graphics.Color;

  6 import android.graphics.Paint;

  7 import android.graphics.Path;

  8 import android.graphics.PorterDuff;

  9 import android.graphics.PorterDuffXfermode;

 10 import android.util.AttributeSet;

 11 import android.view.MotionEvent;

 12 import android.widget.TextView;

 13 

 14 /**  15  * 橡皮檫功能,类似刮刮乐效果  16 */  17 public class Text_Rubbler extends TextView {  19 // 填充距离,使线条更自然,柔和,值越小,越柔和。  20 private float TOUCH_TOLERANCE;  22 private Bitmap mBitmap;  23 private Canvas mCanvas;  24 private Paint mPaint;  25 private Path mPath;  26 private float mX, mY;  28 private boolean isDraw = false;  29  30 public Text_Rubbler(Context context) {  31 super(context);  32  }  33  34 public Text_Rubbler(Context context, AttributeSet attrs, int defStyle) {  35 super(context, attrs, defStyle);  36  }  37  38 public Text_Rubbler(Context context, AttributeSet attrs) {  39 super(context, attrs);  40  }  41  42  @Override  43 protected void onDraw(Canvas canvas) {  44 super.onDraw(canvas);  45 if (isDraw) {  46  mCanvas.drawPath(mPath, mPaint);  47 // mCanvas.drawPoint(mX, mY, mPaint);  48 canvas.drawBitmap(mBitmap, 0, 0, null);  49  }  50  }  51  52 /**  53  * 开启檫除功能  54  *  55  * @param bgColor  56  * 覆盖的背景颜色  57  * @param paintStrokeWidth  58  * 触点(橡皮)宽度  59  * @param touchTolerance  60  * 填充距离,值越小,越柔和。  61 */  62 public void beginRubbler(final int bgColor, 
final int paintStrokeWidth,float touchTolerance) { 64 TOUCH_TOLERANCE = touchTolerance; 65 // 设置画笔 66 mPaint = new Paint(); 67 mPaint.setAlpha(0); 68 // 画笔划过的痕迹就变成透明色了 69 mPaint.setColor(Color.BLACK); // 此处不能为透明色 70 mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT)); 71 // 或者 72 // mPaint.setAlpha(0); 73 // mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN)); 74 75 mPaint.setAntiAlias(true); 76 mPaint.setDither(true); 77 mPaint.setStyle(Paint.Style.STROKE); 78 mPaint.setStrokeJoin(Paint.Join.ROUND); // 前圆角 79 mPaint.setStrokeCap(Paint.Cap.ROUND); // 后圆角 80 mPaint.setStrokeWidth(paintStrokeWidth); // 笔宽 81 82 // 痕迹 83 mPath = new Path(); 84 ; 85 // 覆盖 86 // if (getLayoutParams().width == LayoutParams.FILL_PARENT) { 87 // 88 // } 89 mBitmap = Bitmap.createBitmap(getLayoutParams().width, 90 getLayoutParams().height, Config.ARGB_8888); 91 mCanvas = new Canvas(mBitmap); 92 93 mCanvas.drawColor(bgColor); 94 isDraw = true; 95 } 96 97 @Override 98 public boolean onTouchEvent(MotionEvent event) { 99 if (!isDraw) { 100 return true; 101 } 102 switch (event.getAction()) { 103 case MotionEvent.ACTION_DOWN: // 触点按下 104 // touchDown(event.getRawX(),event.getRawY()); 105 touchDown(event.getX(), event.getY()); 106 invalidate(); 107 break; 108 case MotionEvent.ACTION_MOVE: // 触点移动 109 touchMove(event.getX(), event.getY()); 110 invalidate(); 111 break; 112 case MotionEvent.ACTION_UP: // 触点弹起 113 touchUp(event.getX(), event.getY()); 114 invalidate(); 115 break; 116 default: 117 break; 118 } 119 return true; 120 } 121 122 private void touchDown(float x, float y) { 123 mPath.reset(); 124 mPath.moveTo(x, y); 125 mX = x; 126 mY = y; 127 } 128 129 private void touchMove(float x, float y) { 130 float dx = Math.abs(x - mX); 131 float dy = Math.abs(y - mY); 132 if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) { 133 mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2); 134 mX = x; 135 mY = y; 136 } 137 } 138 139 private void touchUp(float x, float y) { 140 mPath.lineTo(x, y); 141 mCanvas.drawPath(mPath, mPaint); 142 mPath.reset(); 143 } 144 145 }

 

你可能感兴趣的:(自定义控件)