Android 스크레이퍼 예시 분석 실현

13761 단어 android스크레이퍼
위 챗 공식 번 호 는 모두 스 크 래 치 활동 을 하 는데 본 고 는 스 크 래 치 효 과 를 실현 했다.구체 적 인 코드 는 다음 과 같다.
우선 지우개 같은 것 을 만들어 야만 종이 위의 필적 을 지 울 수 있다
 

/** 
   * 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(); 
      ; 
      //    
      // if (getLayoutParams().width == LayoutParams.FILL_PARENT) { 
      // 
      // } 
      mBitmap = Bitmap.createBitmap(getLayoutParams().width, 
          getLayoutParams().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(); 
    } 
   
  } 
이제 지우개 로 닦 아 요.
 

  /** 
   * FileName: RubblerAct.java 
   * @Desc        Text_Rubbler     Activity           ,         
   * @author HTP 
   * @Date  20140312 
   * @version 1.00 
   */ 
   
   
  public class RubblerAct extends Activity { 
    //         
    private TextView tv_rubbler; 
    //          
    private Sentence mSentence; 
    //     
    private TextView tv_next; 
   
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
   
      // setContentView(new Rubble(this,"    ",new Rect(100, 200, 
      // 300,250),2,1f,14)); 
   
      // ///////////////////////////////////////// 
      setContentView(R.layout.rubbler); 
      //             。 
      ((Text_Rubbler) findViewById(R.id.rubbler)).beginRubbler(0xFFFFFFFF, 20, 
          1f);//           
      mSentence = new Sentence(); 
      //         
      tv_rubbler = (TextView) findViewById(R.id.rubbler); 
      String str = mSentence.getSentence(); 
      tv_rubbler.setText(str); 
   
      tv_next = (TextView) findViewById(R.id.tv_next); 
   
      //       
      tv_next.setOnClickListener(new OnClickListener() { 
   
        @Override 
        public void onClick(View v) { 
          // TODO Auto-generated method stub 
          String str = mSentence.getSentence(); 
          tv_rubbler.setText(str); 
          ((Text_Rubbler) findViewById(R.id.rubbler))//       
              .beginRubbler(0xFFFFFFFF, 20, 1f); 
   
        } 
      }); 
   
    } 
   
    class Rubble extends View { 
   
      private final int PAINT_STROKE_WIDTH; 
      private final float TOUCH_TOLERANCE; //     ,      ,  ,   ,   。 
      private final int TEXT_SIZE; 
   
      private Bitmap mBitmap; 
      //    
      private Canvas mCanvas; 
      //    
      private Paint mPaint; 
      private Path mPath; 
      private float mX, mY; 
      private final int X, Y, W, H; 
   
      private final Rect touchRect; 
   
      public Rubble(Context context, String bgText, Rect rect, 
          int paintStrokeWidth, float touchTolerance, int textSize) { 
        super(context); 
        setFocusable(true); 
        touchRect = rect; 
        W = rect.right - rect.left; 
        H = rect.bottom - rect.top; 
        X = rect.left; 
        Y = rect.top; 
        TEXT_SIZE = textSize; 
        PAINT_STROKE_WIDTH = paintStrokeWidth; 
        TOUCH_TOLERANCE = touchTolerance; 
        setBackground(touchRect, bgText); 
        initDrowTools(); 
   
      } 
   
      private void setBackground(Rect rect, String bgText) { 
        DisplayMetrics dm = new DisplayMetrics(); 
        dm = this.getResources().getDisplayMetrics(); 
   
        Bitmap bitmap = Bitmap.createBitmap(dm.widthPixels, 
            dm.heightPixels, Config.ARGB_8888); 
        Canvas canvas = new Canvas(bitmap); 
   
        Paint paint = new Paint(); 
        paint.setColor(0x88000000); 
        // paint.setStyle(Style.STROKE); 
        // paint.setTextAlign(Align.CENTER); 
        paint.setTextSize(TEXT_SIZE); 
   
        // paint.setTextScaleX(1.5f); 
        canvas.drawColor(Color.WHITE); 
        //           
        int x = rect.left 
            + (rect.right - rect.left - bgText.length() * TEXT_SIZE) 
            / 2; 
        int y = rect.top + (rect.bottom - rect.top - TEXT_SIZE) / 2; 
        // int y = 218+25; 
        canvas.drawText(bgText, x, y, paint); 
        Drawable drawable = new BitmapDrawable(bitmap); 
        setBackgroundDrawable(drawable); 
      } 
   
      private void initDrowTools() { 
        //      
        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(PAINT_STROKE_WIDTH); //    
   
        //    
        mPath = new Path(); 
        ; 
   
        //    
        mBitmap = Bitmap.createBitmap(W, H, Config.ARGB_8888); 
        mCanvas = new Canvas(mBitmap); 
        mCanvas.drawColor(0x88000000); 
   
      } 
   
      @Override 
      protected void onDraw(Canvas canvas) { 
        super.onDraw(canvas); 
        mCanvas.drawPath(mPath, mPaint); 
        // mCanvas.drawPoint(mX, mY, mPaint); 
        canvas.drawBitmap(mBitmap, X, Y, null); 
      } 
   
      @Override 
      public boolean onTouchEvent(MotionEvent event) { 
        System.out.print("X--" + event.getX()); 
        System.out.println("Y--" + event.getY()); 
        if (!touchRect.contains((int) event.getX(), (int) event.getY())) { 
          return false; 
        } 
   
        switch (event.getAction()) { 
        //      
        case MotionEvent.ACTION_DOWN: { 
          touchDown(event.getRawX(), event.getRawY()); 
          touchDown(event.getX() - touchRect.left, event.getY() 
              - touchRect.top); 
          invalidate(); 
          break; 
        } 
           
        case MotionEvent.ACTION_MOVE: //      
          touchMove(event.getX() - touchRect.left, event.getY() 
              - touchRect.top); 
   
          invalidate(); 
          break; 
        case MotionEvent.ACTION_UP: //      
          touchUp(event.getX() - touchRect.left, event.getY() 
              - touchRect.top); 
          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(); 
      } 
   
    } 
   
    /** 
     *     ,   back                 
     */ 
    //      
    private long exitTime = 0; 
    @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) { 
      if (keyCode == KeyEvent.KEYCODE_BACK 
          && event.getAction() == KeyEvent.ACTION_DOWN) { 
        if ((System.currentTimeMillis() - exitTime) > 2000) { 
          Toast.makeText(getApplicationContext(), "        ", 
              Toast.LENGTH_SHORT).show(); 
          exitTime = System.currentTimeMillis(); 
        } else { 
          finish(); 
          System.exit(0); 
   
        } 
        return true; 
      } 
      return super.onKeyDown(keyCode, event); 
    } 
   
  } 
실현 효 과 는 다음 과 같다.

읽 어 주 셔 서 감사합니다. 여러분 에 게 도움 이 되 기 를 바 랍 니 다.본 사이트 에 대한 여러분 의 지지 에 감 사 드 립 니 다!

좋은 웹페이지 즐겨찾기