如何绘制在不同颜色的线

如何绘制在不同颜色的线

本文介绍了如何绘制在不同颜色的线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开发出吸引。我想用不同颜色的线条在屏幕上线的应用程序,但所有的线都出现在相同的颜色。

我在我的code键更改颜色的颜色选择器,我想通过选择颜色每次绘制不同颜色的线,但截至目前,因为我构建线和渲染它。当我最初选择红色,画一条线,然后再选择蓝色和画一条线,现在老线也越来越变成蓝色,而不是红色的,但是我想红仍然为红色和蓝色这样任何一个可以帮助?

我的code以下

给出

MyDemo.java:

 公共类MyDemo扩展活动器具
        ColorPickerDialog.OnColorChangedListener {    私人的LinearLayout根;    私人按钮btnReset;
    私人按钮btnPickColor;
    公共静态INT selectedColor = Color.BLACK;
    MyImageView图。
    私有静态最后弦乐COLOR_ preFERENCE_KEY =颜色;    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_my_demo);        根=(的LinearLayout)findViewById(R.id.root);
        btnReset =(按钮)findViewById(R.id.reset);
        btnP​​ickColor =(按钮)findViewById(R.id.pickColor);        最后MyImageView视图=新MyImageView(本);
        view.setLayoutParams(新LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.FILL_PARENT,
                LinearLayout.LayoutParams.FILL_PARENT));
        root.addView(视图);        btnReset.setOnClickListener(新View.OnClickListener(){
            公共无效的onClick(视图v){
                view.reset();
            }
        });        btnP​​ickColor.setOnClickListener(新View.OnClickListener(){
            公共无效的onClick(视图v){
                //颜色选择器类调用
                INT颜色= preferenceManager.getDefaultShared preferences(
                        MyDemo.this).getInt(COLOR_ preFERENCE_KEY,Color.WHITE);
                新ColorPickerDialog(MyDemo.this,MyDemo.this,颜色).show();            }
        });
    }    公共无效colorChanged(INT颜色){
        preferenceManager.getDefaultShared preferences(本).edit()
                .putInt(COLOR_ preFERENCE_KEY,颜色).commit();
        selectedColor =颜色;
    }
}

MyImageView.java:

 公共类MyImageView扩展了ImageView的实现View.OnTouchListener {    私人INT ID = -1;
    私人路径路径;
    私人列表<路径和GT;路径=新的ArrayList<路径>();
    私人列表<&的PointF GT;点=新的ArrayList<&的PointF GT;();
    布尔多点触控= FALSE;
    MyDemo演示;    公共MyImageView(上下文的背景下){
        超级(上下文);
        this.setOnTouchListener(本);
        Log.d(构造,控制在构造函数);
    }    公共无效复位(){
        paths.clear();
        points.clear();
        路径= NULL;
        ID = -1;
        无效();
    }    @覆盖
    保护无效的onDraw(帆布油画){
        super.onDraw(画布);
        INT colorPicked = MyDemo.selectedColor;
        如果(colorPicked!= Color.BLACK){
            涂料粉刷=个CreatePen(colorPicked);
            paint.setStyle(Paint.Style.STROKE);
            对于(路径路径:路径){
                canvas.drawPath(路径,油漆);
            }
            的for(int i = 0; I< points.size();我++){
                的PointF p值= points.get(ⅰ);
                canvas.drawText(+ p.x,p.y,我,个CreatePen(colorPicked));
            }
        }其他{
            涂料粉刷=个CreatePen(colorPicked);
            paint.setStyle(Paint.Style.STROKE);
            对于(路径路径:路径){
                canvas.drawPath(路径,油漆);
            }}
            的for(int i = 0; I< points.size();我++){
                的PointF p值= points.get(ⅰ);
                canvas.drawText(+ p.x,p.y,我,个CreatePen(colorPicked));
            }
        }
    私人的PointF复制(的PointF P){
        副本的PointF =新的PointF();
        copy.set(P);
        返回副本;
    }    公共布尔onTouch(视图V,MotionEvent事件){
        INT行动= event.getAction();
        开关(动作&安培; MotionEvent.ACTION_MASK){
        案例MotionEvent.ACTION_DOWN:
            多点触控= FALSE;            ID = event.getPointerId(0);
            的PointF P =用GetPoint(事件ID);
            路径=新路径();
            path.moveTo(p.x,p.y);
            paths.add(路径);
            points.add(复印件(P));
            打破;
        案例MotionEvent.ACTION_POINTER_DOWN:
            多点触控=真;
            的for(int i = 0; I< event.getPointerCount();我++){
                INT TID = event.getPointerId(I)
                如果(TID!= ID){
                    points.add(用GetPoint(事件,I));
                }
            }
            打破;
        案例MotionEvent.ACTION_MOVE:
            如果(!多点触控){
                P =用GetPoint(事件ID);
                path.lineTo(p.x,p.y);
            }
            打破;
        }
        无效();
        返回true;
    }    私人的PointF用GetPoint(MotionEvent事件,int i)以{
        INT索引= 0;
        返回新的PointF(event.getX(指数),event.getY(指数));
    }    公共涂料个CreatePen(INT颜色){
        漆笔=新的油漆();
        pen.setColor(颜色);
        浮宽度= 3;
        pen.setStrokeWidth(宽);
        返回笔;
    }}

Col​​orPickerDialog.java:

 公共类ColorPickerDialog扩展对话框{    公共接口OnColorChangedListener {
        无效colorChanged(INT颜色);    }    私人最终OnColorChangedListener mListener;
    私人最终诠释mInitialColor;    私有静态类ColorPickerView扩展视图{
        私人最终涂料mPaint;
        私人最终涂料mCenterPaint;
        私人最终诠释[] mColors;
        私人最终OnColorChangedListener mListener;        ColorPickerView(上下文C,OnColorChangedListener L,INT颜色){
            超(C);
            mListener = 1;
            mColors = INT新[] {0xFFFF0000地址,0xFFFF00FF,0xFF0000FF,
                    0xFF00FFFF,0xFF00FF00,0xFFFFFF00,0xFFFF0000地址};
            着色器S =新SweepGradient(0,0,mColors,NULL);            mPaint =新的油漆(Paint.ANTI_ALIAS_FLAG);
            mPaint.setShader(多个);
            mPaint.setStyle(Paint.Style.STROKE);
            mPaint.setStrokeWidth(32);            mCenterPaint =新的油漆(Paint.ANTI_ALIAS_FLAG);
            mCenterPaint.setColor(颜色);
            mCenterPaint.setStrokeWidth(5);
        }        私人布尔mTrackingCenter;
        私人布尔mHighlightCenter;        @覆盖
        保护无效的onDraw(帆布油画){
            浮动R = CENTER_X - mPaint.getStrokeWidth()* 0.5F;            canvas.translate(CENTER_X,CENTER_X);            canvas.drawOval(新RectF(-r,-r,R,R),mPaint);
            canvas.drawCircle(0,0,CENTER_RADIUS,mCenterPaint);            如果(mTrackingCenter){
                INT C = mCenterPaint.getColor();
                mCenterPaint.setStyle(Paint.Style.STROKE);                如果(mHighlightCenter){
                    mCenterPaint.setAlpha(0xFF的);
                }其他{
                    mCenterPaint.setAlpha(0x80的);
                }
                canvas.drawCircle(0,0,CENTER_RADIUS
                        + mCenterPaint.getStrokeWidth(),mCenterPaint);                mCenterPaint.setStyle(Paint.Style.FILL);
                mCenterPaint.setColor(C);
            }
        }        @覆盖
        保护无效onMeasure(INT widthMeasureSpec,诠释heightMeasureSpec){
            setMeasuredDimension(CENTER_X * 2,CENTER_Y * 2);
        }        私有静态最终诠释CENTER_X = 100;
        私有静态最终诠释CENTER_Y = 100;
        私有静态最终诠释CENTER_RADIUS = 32;        私人诠释大道(int类型,INT D,浮动P){
            返回小号+ java.lang.Math.round(P *(D - S));
        }        私人诠释interpColor(INT颜色[],浮动单元){
            如果(单元下; = 0)
                返回颜色[0];
            如果(部> = 1)
                返回颜色[colors.length - 1];            浮动P =单位*(colors.length - 1);
            INT I =(INT)P;
            对 - =我;            //现在p是只是小数部分[0 ... 1)和i是索引
            INT C0 =颜色[I]
            INT C1 =颜色第[i + 1];
            诠释一个= AVE(Color.alpha(C0),Color.alpha(C1)中,p);
            INT R = AVE(Color.red(C0),Color.red(C1)中,p);
            INT G = AVE(Color.green(C0),Color.green(C1)中,p);
            INT B = AVE(Color.blue(C0),Color.blue(C1)中,p);            返回Color.argb(A,R,G,B);
        }        私有静态最终浮动PI = 3.1415926f;        @覆盖
        公共布尔onTouchEvent(MotionEvent事件){
            浮X = event.getX() - CENTER_X;
            浮Y = event.getY() - CENTER_Y;
            布尔内心= java.lang.Math.sqrt(X * X + Y * Y)< = CENTER_RADIUS;            开关(event.getAction()){
            案例MotionEvent.ACTION_DOWN:
                mTrackingCenter =内心;
                如果(内心){
                    mHighlightCenter =真;
                    无效();
                    打破;
                }
            案例MotionEvent.ACTION_MOVE:
                如果(mTrackingCenter){
                    如果(mHighlightCenter!=内心){
                        mHighlightCenter =内心;
                        无效();
                    }
                }其他{
                    浮动角=(浮点)java.lang.Math.atan2(Y,X);
                    //需要打开角度[-PI ... PI]为单位[0 .... 1]
                    浮单位=角度/(2 * PI);
                    如果(单位℃,){
                        单元+ = 1;
                    }
                    mCenterPaint.setColor(interpColor(mColors,股吧));
                    无效();
                }
                打破;
            案例MotionEvent.ACTION_UP:
                如果(mTrackingCenter){
                    如果(内心){
                        mListener.colorChanged(mCenterPaint.getColor());                    }
                    mTrackingCenter = FALSE; //所以我们得出的w / o晕
                    无效();
                }
                打破;
            }
            返回true;
        }
    }    公共ColorPickerDialog(上下文的背景下,OnColorChangedListener监听器,
            INT initialColor){
        超级(上下文);        mListener =侦听器;
        mInitialColor = initialColor;
    }    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        OnColorChangedListener L =新OnColorChangedListener(){
            公共无效colorChanged(INT颜色){
                mListener.colorChanged(颜色);
                解雇();
            }
        };        的LinearLayout布局=新的LinearLayout(的getContext());
        layout.setOrientation(LinearLayout.VERTICAL);
        layout.setGravity(Gravity.CENTER);
        layout.setPadding(10,10,10,10);
        layout.addView(新ColorPickerView(的getContext(),L,mInitialColor)
                新LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.WRAP_CONTENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT));
        的setContentView(布局);
        的setTitle(选择颜色);
    }
}


解决方案

试试这个,它会帮助你。

  @覆盖
  保护无效的onDraw(帆布油画){    canvas.drawBitmap(mBitmap,0,0,mBitmapPaint);
    INT colorPicked = MainActivity.selectedColor;    paint1 =个CreatePen(colorPicked);
     对于(路径P:路径){
         canvas.drawPath(对,paint1);
         paint1.setColor(colorPicked);
     }}民营涂料个CreatePen(INT颜色){
    // TODO自动生成方法存根    paint1 =新的油漆();
    paint1.setAntiAlias​​(真);
    paint1.setDither(真);
    paint1.setStyle(Paint.Style.STROKE);
    paint1.setStrokeJoin(Paint.Join.ROUND);
    paint1.setStrokeCap​​(Paint.Cap.ROUND);
    paint1.setStrokeWidth(3);
    返回paint1;
}

I have developed an application which draws lines on the screen .I want to use different colors for the lines but all the lines are appearing in same color.

I have a color picker in my code to change the colors and i want to draw lines of different colors each time by selecting colors, but as of now since i am constructing lines and rendering it.When i choose initially red and draw a line, and then later select blue and draw a line , now the older line is also getting changed to blue instead of red, However i want the red to remain as red, and blue as such can any one help?

My code is given below

MyDemo.java:

public class MyDemo extends Activity implements
        ColorPickerDialog.OnColorChangedListener {

    private LinearLayout root;

    private Button btnReset;
    private Button btnPickColor;
    public static int selectedColor = Color.BLACK;
    MyImageView view;
    private static final String COLOR_PREFERENCE_KEY = "color";



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_demo);

        root = (LinearLayout) findViewById(R.id.root);
        btnReset = (Button) findViewById(R.id.reset);
        btnPickColor = (Button) findViewById(R.id.pickColor);

        final MyImageView view = new MyImageView(this);
        view.setLayoutParams(new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.FILL_PARENT,
                LinearLayout.LayoutParams.FILL_PARENT));
        root.addView(view);

        btnReset.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                view.reset();
            }
        });

        btnPickColor.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // color picker class call
                int color = PreferenceManager.getDefaultSharedPreferences(
                        MyDemo.this).getInt(COLOR_PREFERENCE_KEY, Color.WHITE);
                new ColorPickerDialog(MyDemo.this, MyDemo.this, color).show();

            }
        });
    }

    public void colorChanged(int color) {
        PreferenceManager.getDefaultSharedPreferences(this).edit()
                .putInt(COLOR_PREFERENCE_KEY, color).commit();
        selectedColor = color;
    }


}

MyImageView.java:

public class MyImageView extends ImageView implements View.OnTouchListener {

    private int id = -1;
    private Path path;
    private List<Path> paths = new ArrayList<Path>();
    private List<PointF> points = new ArrayList<PointF>();
    boolean multiTouch = false;
    MyDemo demo;

    public MyImageView(Context context) {
        super(context);
        this.setOnTouchListener(this);
        Log.d("Constructor", " Control in constructor");
    }

    public void reset() {
        paths.clear();
        points.clear();
        path = null;
        id = -1;
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int colorPicked = MyDemo.selectedColor;
        if (colorPicked != Color.BLACK) {
            Paint paint = createPen(colorPicked);
            paint.setStyle(Paint.Style.STROKE);
            for (Path path : paths) {
                canvas.drawPath(path, paint);
            }
            for (int i = 0; i < points.size(); i++) {
                PointF p = points.get(i);
                canvas.drawText("" + p.x, p.y, i, createPen(colorPicked));
            }
        } else {
            Paint paint = createPen(colorPicked);
            paint.setStyle(Paint.Style.STROKE);
            for (Path path : paths) {
                canvas.drawPath(path, paint);
            }}
            for (int i = 0; i < points.size(); i++) {
                PointF p = points.get(i);
                canvas.drawText("" + p.x, p.y, i, createPen(colorPicked));
            }
        }


    private PointF copy(PointF p) {
        PointF copy = new PointF();
        copy.set(p);
        return copy;
    }

    public boolean onTouch(View v, MotionEvent event) {
        int action = event.getAction();
        switch (action & MotionEvent.ACTION_MASK) {
        case MotionEvent.ACTION_DOWN:
            multiTouch = false;

            id = event.getPointerId(0);
            PointF p = getPoint(event, id);
            path = new Path();
            path.moveTo(p.x, p.y);
            paths.add(path);
            points.add(copy(p));
            break;
        case MotionEvent.ACTION_POINTER_DOWN:
            multiTouch = true;
            for (int i = 0; i < event.getPointerCount(); i++) {
                int tId = event.getPointerId(i);
                if (tId != id) {
                    points.add(getPoint(event, i));
                }
            }
            break;
        case MotionEvent.ACTION_MOVE:
            if (!multiTouch) {
                p = getPoint(event, id);
                path.lineTo(p.x, p.y);
            }
            break;
        }
        invalidate();
        return true;
    }

    private PointF getPoint(MotionEvent event, int i) {
        int index = 0;
        return new PointF(event.getX(index), event.getY(index));
    }

    public Paint createPen(int color) {
        Paint pen = new Paint();
        pen.setColor(color);
        float width = 3;
        pen.setStrokeWidth(width);
        return pen;
    }

}

ColorPickerDialog.java:

public class ColorPickerDialog extends Dialog {

    public interface OnColorChangedListener {
        void colorChanged(int color);

    }

    private final OnColorChangedListener mListener;
    private final int mInitialColor;

    private static class ColorPickerView extends View {
        private final Paint mPaint;
        private final Paint mCenterPaint;
        private final int[] mColors;
        private final OnColorChangedListener mListener;

        ColorPickerView(Context c, OnColorChangedListener l, int color) {
            super(c);
            mListener = l;
            mColors = new int[] { 0xFFFF0000, 0xFFFF00FF, 0xFF0000FF,
                    0xFF00FFFF, 0xFF00FF00, 0xFFFFFF00, 0xFFFF0000 };
            Shader s = new SweepGradient(0, 0, mColors, null);

            mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
            mPaint.setShader(s);
            mPaint.setStyle(Paint.Style.STROKE);
            mPaint.setStrokeWidth(32);

            mCenterPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
            mCenterPaint.setColor(color);
            mCenterPaint.setStrokeWidth(5);
        }

        private boolean mTrackingCenter;
        private boolean mHighlightCenter;

        @Override
        protected void onDraw(Canvas canvas) {
            float r = CENTER_X - mPaint.getStrokeWidth() * 0.5f;

            canvas.translate(CENTER_X, CENTER_X);

            canvas.drawOval(new RectF(-r, -r, r, r), mPaint);
            canvas.drawCircle(0, 0, CENTER_RADIUS, mCenterPaint);

            if (mTrackingCenter) {
                int c = mCenterPaint.getColor();
                mCenterPaint.setStyle(Paint.Style.STROKE);

                if (mHighlightCenter) {
                    mCenterPaint.setAlpha(0xFF);
                } else {
                    mCenterPaint.setAlpha(0x80);
                }
                canvas.drawCircle(0, 0, CENTER_RADIUS
                        + mCenterPaint.getStrokeWidth(), mCenterPaint);

                mCenterPaint.setStyle(Paint.Style.FILL);
                mCenterPaint.setColor(c);
            }
        }

        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            setMeasuredDimension(CENTER_X * 2, CENTER_Y * 2);
        }

        private static final int CENTER_X = 100;
        private static final int CENTER_Y = 100;
        private static final int CENTER_RADIUS = 32;

        private int ave(int s, int d, float p) {
            return s + java.lang.Math.round(p * (d - s));
        }

        private int interpColor(int colors[], float unit) {
            if (unit <= 0)
                return colors[0];
            if (unit >= 1)
                return colors[colors.length - 1];

            float p = unit * (colors.length - 1);
            int i = (int) p;
            p -= i;

            // now p is just the fractional part [0...1) and i is the index
            int c0 = colors[i];
            int c1 = colors[i + 1];
            int a = ave(Color.alpha(c0), Color.alpha(c1), p);
            int r = ave(Color.red(c0), Color.red(c1), p);
            int g = ave(Color.green(c0), Color.green(c1), p);
            int b = ave(Color.blue(c0), Color.blue(c1), p);

            return Color.argb(a, r, g, b);
        }

        private static final float PI = 3.1415926f;

        @Override
        public boolean onTouchEvent(MotionEvent event) {
            float x = event.getX() - CENTER_X;
            float y = event.getY() - CENTER_Y;
            boolean inCenter = java.lang.Math.sqrt(x * x + y * y) <= CENTER_RADIUS;

            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mTrackingCenter = inCenter;
                if (inCenter) {
                    mHighlightCenter = true;
                    invalidate();
                    break;
                }
            case MotionEvent.ACTION_MOVE:
                if (mTrackingCenter) {
                    if (mHighlightCenter != inCenter) {
                        mHighlightCenter = inCenter;
                        invalidate();
                    }
                } else {
                    float angle = (float) java.lang.Math.atan2(y, x);
                    // need to turn angle [-PI ... PI] into unit [0....1]
                    float unit = angle / (2 * PI);
                    if (unit < 0) {
                        unit += 1;
                    }
                    mCenterPaint.setColor(interpColor(mColors, unit));
                    invalidate();
                }
                break;
            case MotionEvent.ACTION_UP:
                if (mTrackingCenter) {
                    if (inCenter) {
                        mListener.colorChanged(mCenterPaint.getColor());

                    }
                    mTrackingCenter = false; // so we draw w/o halo
                    invalidate();
                }
                break;
            }
            return true;
        }
    }

    public ColorPickerDialog(Context context, OnColorChangedListener listener,
            int initialColor) {
        super(context);

        mListener = listener;
        mInitialColor = initialColor;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        OnColorChangedListener l = new OnColorChangedListener() {
            public void colorChanged(int color) {
                mListener.colorChanged(color);
                dismiss();
            }
        };

        LinearLayout layout = new LinearLayout(getContext());
        layout.setOrientation(LinearLayout.VERTICAL);
        layout.setGravity(Gravity.CENTER);
        layout.setPadding(10, 10, 10, 10);
        layout.addView(new ColorPickerView(getContext(), l, mInitialColor),
                new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.WRAP_CONTENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT));
        setContentView(layout);
        setTitle("Pick a Color");
    }
}
解决方案

try this, it will help you

  @Override
  protected void onDraw(Canvas canvas) {

    canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
    int colorPicked = MainActivity.selectedColor;

    paint1 = createPen(colorPicked);
     for (Path p : paths){
         canvas.drawPath(p, paint1);
         paint1.setColor(colorPicked);
     }

}

private Paint createPen(int color) {
    // TODO Auto-generated method stub

    paint1 = new Paint();
    paint1.setAntiAlias(true);
    paint1.setDither(true);
    paint1.setStyle(Paint.Style.STROKE);
    paint1.setStrokeJoin(Paint.Join.ROUND);
    paint1.setStrokeCap(Paint.Cap.ROUND);
    paint1.setStrokeWidth(3);
    return paint1;
}

这篇关于如何绘制在不同颜色的线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 20:56