只能显示一种颜色 [英] only one color is displayed in time

查看:83
本文介绍了只能显示一种颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发绘画应用程序,问题是当我选择颜色和绘画然后选择不同的颜色时,整个绘画颜色变为新颜色的任何人都可以说出为什么会发生这种情况以及如何解决这个问题吗?以及如何为此添加橡皮擦?imageview DrawView在这里:

I'm developing paint app the problem is when i choose color and paint and then pick different color the whole paint color changes to the new color can any one tell why this is happening and how to solve this ? and how to add eraser to this? imageview DrawView here :

public class DrawView extends ImageView {
    private ArrayList<Point> mTouches;
    int paintColor;
    public int setcolor(int a){
        paintColor=a;
        return paintColor;}
    // Java constructor
    public DrawView(Context context) {
        super(context);
        init();}
    // XML constructor
    public DrawView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();}
    private void init() {
        mTouches = new ArrayList<Point>();}
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // Capture a reference to each touch for drawing
        float touchX = event.getX();
        float touchY = event.getY();
        mTouches.add(new Point(Math.round(touchX), Math.round(touchY)));
        return super.onTouchEvent(event);}
    @Override
    protected void onDraw(Canvas c) {
        // Let the image be drawn first
        super.onDraw(c);
        // Draw your custom points here
        Paint paint = new Paint();
        paint.setColor(paintColor);
        paint.setAntiAlias(true);
        paint.setStrokeWidth(5);
        paint.setStyle(Paint.Style.FILL);
        paint.setStrokeJoin(Paint.Join.ROUND);
        paint.setStrokeCap(Paint.Cap.ROUND);
        for (Point p : mTouches) {
            c.drawCircle( p.x, p.y,15,paint);}} }

在我的主屏幕上:

im.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                DrawView mcustomImagview = (DrawView) v;
                mcustomImagview.setcolor(color);
                mcustomImagview.invalidate();
                if (v.onTouchEvent(event)) {
// Do something with event.getX(), event.getY() }
                return true;}})

推荐答案

每次调用 onDraw 时,都会重新绘制整个画布.由于您仅在自定义类中存储一个paintColor,因此仅使用最后选择的一个.

Every time onDraw is called, the entire canvas is redraw. As you store only one paintColor in your custom class, only the last selected one is used.

您需要将颜色与关联的Point一起存储.然后,当您循环抛出您的 mTouches 数组时,您可以更改当前Point的颜色.

You need to store the color together with the associated Point. Then when you loop throw your mTouches array, you change the color for the color of the current Point.

Paint 对象可以更新,因此您不必每次都创建它,因为在 onDraw 中创建新对象是一种不好的做法.

编辑

Btw the Paint object can be updated, so you don't need to create it every time as it's a bad practice to create new objects in onDraw.

public class DrawView extends android.support.v7.widget.AppCompatImageView {
    private ArrayList<ColouredPoint> mTouches;
    // Current used colour
    private int mCurrColour;
    private Paint mPaint;

    public void setColor(int colour) {
        mCurrColour = colour;
    }

    public DrawView(Context context) {
        super(context);
        init();
    }

    // XML constructor
    public DrawView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        mTouches = new ArrayList<>();
        mPaint = new Paint();
        mPaint.setColor(mCurrColour);
        mPaint.setAntiAlias(true);
        mPaint.setStrokeWidth(5);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // Capture a reference to each touch for drawing
        float touchX = event.getX();
        float touchY = event.getY();
        mTouches.add(new ColouredPoint(Math.round(touchX), Math.round(touchY), mCurrColour));

        return super.onTouchEvent(event);
    }

    @Override
    protected void onDraw(Canvas c) {
        // Let the image be drawn first
        super.onDraw(c);
        // Draw your custom points here
        for (ColouredPoint p : mTouches) {
            mPaint.setColor(p.colour);
            c.drawCircle(p.x, p.y, 15, mPaint);
        }
    }

    /**
     * Class to store the coordinate and the colour of the point.
     */
    private class ColouredPoint {
        int x;
        int y;
        int colour;

        public ColouredPoint(int x, int y, int colour) {
            this.x = x;
            this.y = y;
            this.colour = colour;
        }
    }
}

这篇关于只能显示一种颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆