Android - 如何在 View 上绘制

标签 android view paint android-custom-view

我想在自己的 View (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();
    }





}

如何在我自己的 View 上使用自由手指绘画?

最佳答案

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

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

您可以在 onCreate 中开始,方法是更改​​

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

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

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

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

关于Android - 如何在 View 上绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13851728/

相关文章:

android - Firebase - 在不知道其成员的注册 ID 的情况下删除设备组

java - 如何防止 webview 在方向改变时改变文本大小?

iphone - 如何使补充 View 在 UICollectionView 中 float ,就像 UITableView 普通样式中的节标题一样

android - 将自定义布局(从文件)添加到另一个布局

java - Android ListView 未调用适配器的类 getView() 函数

android-manifest - Android - 是否可以抑制输出到 ddms

android - GridLayoutManager 跨度大小 RecycleView

java - PaintIcon 不再让我绘制图像

android - OSMDroid PathOverlay 绘图在高缩放级别时损坏

android - 是否可以在 Android 中触摸模糊部分图像?