10

我想在我自己的视图(R.id.view)上绘画,但这段代码似乎没有任何效果。它根本不允许我画任何东西。

public class MainActivity extends Activity implements OnTouchListener
        {
Path mPath;
Canvas canvas;
Paint mPaint;
MaskFilter mEmboss;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        View view=(View)findViewById(R.id.view1);
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
        mPaint.setColor(0xFFFF0000);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeWidth(12);

        canvas = null;
        //view.draw(canvas);
        mPath = new Path();
        Bitmap mBitmap;
        //Paint mBitmapPaint = new Paint(Paint.DITHER_FLAG);
        mBitmap = Bitmap.createBitmap(210, 170, Bitmap.Config.ARGB_8888);
        canvas = new Canvas(mBitmap);
        canvas.drawColor(0xFFAAAAAA);

        canvas.drawBitmap(mBitmap, 0, 0, mPaint);


        //canvas.drawPaint(mPaint);
        view.draw(canvas);
        canvas.drawPath(mPath, mPaint);


        mEmboss = new EmbossMaskFilter(new float[] { 1, 1, 1 },
                                       0.4f, 6, 3.5f);



        view.setOnTouchListener(new OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {
                float x = event.getX();
                float y = event.getY();
                Log.d("x", String.valueOf(x));
                Log.d("y", String.valueOf(y));

                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        Log.d("a","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;
            }
        });

       // mBlur = new BlurMaskFilter(8, BlurMaskFilter.Blur.NORMAL);
    }
    private float mX, mY;
    private static final float TOUCH_TOLERANCE = 4;

    private void touch_start(float x, float y) {
        mPath.reset();
        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
        canvas.drawPath(mPath, mPaint);
        // kill this so we don't double draw
        mPath.reset();
    }





}

我该怎么做才能在我自己的视图上使用 freehanf 手指画?

4

1 回答 1

9

最好创建一个扩展视图的类,并将其设置为内容视图。这样更有活力。

此外,Action_Move在触摸事件期间该参数会被多次调用,因此最好注意这一点,否则当您想要曲线时会得到一条直线。

你会从onCreate改变开始

View view = new MyView();
setContentView(view);

在您的MyView班级内部,您将拥有一个ArrayList对象Path。每次Action_UP被调用时,您将路径添加到ArrayList, use view.Invalidate();,然后该类将调用onDraw您视图类中的方法。所以你像这样覆盖它:

@Override
public void onDraw(Canvas c) {
    for (Path p : listOfPaths) {
        c.drawPath(p, paint);
    }
}

请注意,虽然这对于简单的绘图很有用,但不要指望使用这种方法在平板电脑或手机上绘制蒙娜丽莎,因为一旦有很多线条,您可能会注意到滞后。如果您想变得更复杂和专业,请查看该view.Invalidate(Rect r)方法,它不会使整个视图无效,而只会使您刚刚绘制的部分无效。

于 2012-12-13T02:26:58.503 回答