国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

直接拿來用的Android刮獎控件

2019-12-12 05:23:50
字體:
來源:轉載
供稿:網友

直接上效果圖

 

功能特色:
 1、可以設置刮開后顯示文字或圖片
 2、可以統計已刮開區域所占百分比 

Demo下載地址:RubberDemo.rar 

下面是源碼: 

@SuppressLint("HandlerLeak")public class RubberView extends TextView { private static final int W = 480; private static final int H = 800; private static final int MV = 1; private static final int SW = 50; private static final int MC = 0xFFD6D6D6; private int mWidth; private int mHeight; private int mMaskColor; private int mStrokeWidth; private float mX; private float mY; private boolean mRun; private boolean caculate; private Path mPath; private Paint mPaint; private Paint mBitmapPaint; private Canvas mCanvas; private Bitmap mBitmap; private int[] mPixels; private Thread mThread; private onWipeListener mWipeListener; public RubberView(Context context) { super(context); init(context); } public RubberView(Context context, AttributeSet attrs) { super(context, attrs); init(context); } private final void init(Context context) { mMaskColor = MC; mStrokeWidth = SW; mPath = new Path(); mBitmapPaint = new Paint(); mPaint = new Paint(); mPaint.setAntiAlias(true);// 抗鋸齒 mPaint.setDither(true);// 遞色 mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR)); mPaint.setStyle(Paint.Style.STROKE); mPaint.setStrokeJoin(Paint.Join.ROUND); // 前圓角 mPaint.setStrokeCap(Paint.Cap.ROUND); // 后圓角 mPaint.setStrokeWidth(mStrokeWidth); // 筆寬 mBitmap = Bitmap.createBitmap(W, H, Config.ARGB_8888); mCanvas = new Canvas(mBitmap); mCanvas.drawColor(mMaskColor); mRun = true; mThread = new Thread(mRunnable); mThread.start(); setGravity(Gravity.CENTER); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); mCanvas.drawPath(mPath, mPaint); canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int w = MeasureSpec.getSize(widthMeasureSpec); int h = MeasureSpec.getSize(heightMeasureSpec); if (w > 0 && h > 0) {  mWidth = w;  mHeight = h; } } public void reset() { mPath.reset(); mCanvas.drawPaint(mPaint); mCanvas.drawColor(mMaskColor); invalidate(); } public void setOnWipeListener(onWipeListener listerer) { this.mWipeListener = listerer; } public void setStrokeWidth(int width) { this.mStrokeWidth = width; mPaint.setStrokeWidth(width); } public void setMaskColor(int color) { this.mMaskColor = color; reset(); } @Override public boolean onTouchEvent(MotionEvent event) { boolean invalidate = false; boolean consume = false; int action = event.getAction(); switch (action) { case MotionEvent.ACTION_DOWN:  consume = true;  touchDown(event);  break; case MotionEvent.ACTION_MOVE:  consume = true;  invalidate = touchMove(event);  break; case MotionEvent.ACTION_UP:  consume = true;  touchUp(event);  break; } if (invalidate) {  invalidate(); } if (consume) {  return true; } return super.onTouchEvent(event); } // 手指點下屏幕時調用 private void touchDown(MotionEvent event) { caculate = false; // 重置繪制路線,即隱藏之前繪制的軌跡 mPath.reset(); float x = event.getX(); float y = event.getY(); mX = x; mY = y; // mPath繪制的繪制起點 mPath.moveTo(x, y); } // 手指在屏幕上滑動時調用 private boolean touchMove(MotionEvent event) { caculate = false; final float x = event.getX(); final float y = event.getY(); final float previousX = mX; final float previousY = mY; // 設置貝塞爾曲線的操作點為起點和終點的一半 float cX = (x + previousX) / 2; float cY = (y + previousY) / 2; final float dx = Math.abs(x - previousX); final float dy = Math.abs(y - previousY); boolean move = false; if (dx >= MV || dy >= MV) {  // 二次貝塞爾,實現平滑曲線;cX, cY為操作點 x,y為終點  mPath.quadTo(cX, cY, x, y);  // 第二次執行時,第一次結束調用的坐標值將作為第二次調用的初始坐標值  mX = x;  mY = y;  move = true; } return move; } private void touchUp(MotionEvent event) { caculate = true; mRun = true; } private Runnable mRunnable = new Runnable() { @Override public void run() {  while (mRun) {  SystemClock.sleep(100);  // 收到計算命令,立即開始計算  if (caculate) {   caculate = false;   int w = mWidth;   int h = mHeight;   float wipeArea = 0;   float totalArea = w * h;   // 計算耗時100毫秒左右   Bitmap bitmap = mBitmap;   if (mPixels == null) {   mPixels = new int[w * h];   }   bitmap.getPixels(mPixels, 0, w, 0, 0, w, h);   for (int i = 0; i < w; i++) {   for (int j = 0; j < h; j++) {    int index = i + j * w;    if (mPixels[index] == 0) {    wipeArea++;    }   }   }   if (wipeArea > 0 && totalArea > 0) {   int percent = (int) (wipeArea * 100 / totalArea);   Message msg = mHandler.obtainMessage();   msg.what = 0x1;   msg.arg1 = percent;   mHandler.sendMessage(msg);   }  }  } } }; private Handler mHandler = new Handler() { public void handleMessage(Message msg) {  if (mWipeListener != null) {  int percent = msg.arg1;  mWipeListener.onWipe(percent);  } }; }; public interface onWipeListener { public void onWipe(int percent); }  @Override protected void onDetachedFromWindow() { super.onDetachedFromWindow(); mRun = false; }}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 尼勒克县| 炉霍县| 定襄县| 金塔县| 台南市| 清涧县| 布尔津县| 巍山| 锡林浩特市| 肃宁县| 如东县| 新津县| 门头沟区| 天台县| 昌宁县| 西吉县| 光山县| 内江市| 仁布县| 云龙县| 南开区| 黑河市| 正镶白旗| 南安市| 集安市| 铜鼓县| 刚察县| 仙游县| 桐城市| 呼图壁县| 紫云| 买车| 平南县| 萨嘎县| 堆龙德庆县| 长兴县| 蓬莱市| 洪江市| 伊通| 绩溪县| 驻马店市|