【问题标题】:Android MyView (FingerPaint Demo) Reset CanvasAndroid MyView (FingerPaint Demo) 重置画布
【发布时间】:2012-04-01 11:41:38
【问题描述】:

我使用了 FingerPaint API DEMO 中的 MyView 类(来自 Here),并希望能够清除任何绘制线条的视图。实际上将其重置为在活动开始时首次呈现时。我的 MyView 视图在我的 XML 布局中声明(未编程)。

使用按钮最简单的方法是什么?

非常感谢:)

【问题讨论】:

    标签: android canvas view reset


    【解决方案1】:

    我设法通过如下修改 MyView 类来实现这一点:

    import java.util.ArrayList;
    import java.util.List;
    
    import android.content.Context;
    import android.graphics.Bitmap;
    import android.graphics.Canvas;
    import android.graphics.Paint;
    import android.graphics.Path;
    import android.util.AttributeSet;
    import android.view.MotionEvent;
    import android.view.View;
    
    public class MyView extends View {
    
        private Bitmap  mBitmap;
        private Canvas  mCanvas;
        private Path    mPath;
        private Paint   mBitmapPaint;
        private Paint   mPaint;
        private List<Path>  mPathsArray = new ArrayList<Path>();
    
        public MyView(Context c, AttributeSet attrs) {
            super(c, attrs);
            mPaint = new Paint();
            mPaint.setColor(0xFF00F1F5);
            mPaint.setStyle(Paint.Style.STROKE);
            mPaint.setStrokeJoin(Paint.Join.ROUND);
            mPaint.setStrokeCap(Paint.Cap.ROUND);
            mPaint.setStrokeWidth(6);
    
            mPath = new Path();
            mBitmapPaint = new Paint(Paint.DITHER_FLAG);
        }
    
        @Override
        protected void onSizeChanged(int w, int h, int oldw, int oldh) {
            super.onSizeChanged(w, h, oldw, oldh);
            mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
            mCanvas = new Canvas(mBitmap);
        }
        public void clear(){
            mBitmap = Bitmap.createBitmap(480, 480, Bitmap.Config.ARGB_8888);
            mPath = new Path();
            invalidate();
        }
        @Override
        protected void onDraw(Canvas canvas) {
            canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
            canvas.drawPath(mPath, mPaint);
        }
    
        private float mX, mY;
        private static final float TOUCH_TOLERANCE = 4;
    
        private void touch_start(float x, float y) {
            mPath.moveTo(x, y);
            mX = x;
            mY = y;
        }
        private void touch_move(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 touch_up() {
            mPath.lineTo(mX, mY);
            // commit the path to our offscreen
            mCanvas.drawPath(mPath, mPaint);
            // kill this so we don't double draw
            mPathsArray.add(mPath);
        }
    
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            float x = event.getX();
            float y = event.getY();
    
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    touch_start(x, y);
                    invalidate();
                    break;
                case MotionEvent.ACTION_MOVE:
                    touch_move(x, y);
                    invalidate();
                    break;
                case MotionEvent.ACTION_UP:
                    touch_up();
                    invalidate();
                    break;
            }
            return true;
        }
    }
    

    以防其他人也想达到同样的效果。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-30
      • 2013-09-12
      • 2015-05-05
      相关资源
      最近更新 更多