简体   繁体   English

改变颜色会改变所有的圆形颜色

[英]Changing colour of paint changes all circle colurs

So im trying to make an adroid application that draws a stream of circles as touchevent takes place. 所以我试图制作一个adroid应用程序,在触发事件发生时绘制一个圆圈流。 If i draw on the left side of the screen it should draw a green circle, and if its on the right , it should draw a blue circle. 如果我在屏幕的左侧绘制它应绘制一个绿色圆圈,如果它在右侧,它应绘制一个蓝色圆圈。 The app is doing this, but it changes the color of all the circles already drawn. 应用程序正在执行此操作,但它会更改已绘制的所有圆圈的颜色。 SO i made a Draw circle Class and a array list of objects to treat each cricle as an individual objec, still not working Even after a Touchup event. 所以我制作了一个Draw circle Class和一个对象的数组列表,将每个cricle视为一个单独的目标,即使在Touchup事件之后仍然无效。 the code for it is given below 它的代码如下

 private class Drawcirlce  {

        public Drawcirlce(Canvas c) {
        // TODO Auto-generated constructor stub


        for (Point point : points) {

             if(flag==true)
                    c.drawCircle(point.x, point.y, 5, paint);
                    else
                    c.drawCircle(point.x, point.y, 5, p2);
        }
    invalidate();
    }
    /*for (Point point : points) {
     if(flag==true)
        mcan.drawCircle(point.x, point.y, 5, paint);
        else
        mcan.drawCircle(point.x, point.y, 5, p2);
}*/

}


public void onDraw(Canvas canvas) {


    i++;
     //Drawcirlce d=new Drawcirlce();
dc.add(new Drawcirlce(canvas));

Log.d(TAG, "i: " + i);invalidate();
}
public boolean onTouch(View view, MotionEvent event) {
    // if(event.getAction() != MotionEvent.ACTION_DOWN)
    // return super.onTouchEvent(event);
    Point point = new Point();
    point.x = event.getX();
    point.y = event.getY();
    if(point.x>200){
        paint.setColor(Color.BLUE);
    flag=true;
    }
    else{
        p2.setColor(Color.GREEN);
    flag=false;
    }

    points.add(point);
    //dc.add(new Drawcirlce(mcan));

    invalidate();
    Log.d(TAG, "point: " + point);
    return true;
}

public boolean onTouch(View view, MotionEvent event) {
    // if(event.getAction() != MotionEvent.ACTION_DOWN)
    // return super.onTouchEvent(event);
    Point point = new Point();
    point.x = event.getX();
    point.y = event.getY();
    if(point.x>200){
        paint.setColor(Color.BLUE);
    flag=true;
    }
    else{
        p2.setColor(Color.GREEN);
    flag=false;
    }

    points.add(point);
    //dc.add(new Drawcirlce(mcan));

    invalidate();
    Log.d(TAG, "point: " + point);
    return true;
}

Any one know aht im doing wrong, or if theres a way around this? 任何人都知道我做错了,或者如果有办法绕过这个?

Your Drawcirlce class needs to have a Paint object in it. 你的Drawcirlce类需要有一个Paint对象。 (You can make this public to the class, or give it getPaint() and setPaint() , whichever.) Then, when you call c.drawCircle(point.x, point.y, 5, paint); (你可以把它公开给这个类,或者给它getPaint()setPaint() ,无论哪个。)然后,当你调用c.drawCircle(point.x, point.y, 5, paint); , call it with the Paint object in the class instead. ,使用类中的Paint对象来调用它。

Something like this: 像这样的东西:

private class Drawcirlce {
    public Paint myPaint;

    public void draw(Canvas c) { // Don't use a constructor here...
        if (myPaint == null)
            return;

        for (Point point : points) {
            c.drawCircle(point.x, point.y, 5, myPaint);
            invalidate();
        }
    }
}

Then, in your onTouch event, you do something like this: 然后,在onTouch事件中,您执行以下操作:

Drawcirlce myCircle = new Drawcirlce();
if (flag == true)
    myCircle.myPaint = new Paint(paint); // Copies the current paint object.
else
    myCircle.myPaint = new Paint(p2); // Copies the current p2 object.
dc.add(); // Adds our circle with its own Paint object.

Lastly, you would have to change your onDraw event. 最后,您必须更改onDraw事件。

for (Drawcirlce d : dc) { // Loop through everything in the dc array.
    d.draw(canvas); // Draw it to this canvas!
}

This code, in effect, assigns a circle the ability to have its own Paint object. 实际上,这段代码赋予圆圈拥有自己的Paint对象的能力。 Then, when touching, you create a Paint to give it. 然后,在触摸时,您创建一个Paint来提供它。 Lastly, when drawing, you use that Paint instead of the current one. 最后,在绘图时,使用Paint而不是当前的Paint

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM