직접 가 져 온 Android 스 크 래 치 컨트롤

직접 효과 도
 
기능 특징:
 1、스 크 래 치 후 텍스트 나 그림 표시 설정 가능
 2、긁 힌 영역 이 차지 하 는 비율 을 통계 할 수 있 습 니 다 
데모 다운로드 주소: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;
 }
}

이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기