문제

Here in my project I have added colour to my ball but it is not showing..why? I have posted only that part of code which makes sense to my question....Is there problem with setColor() method or some other problem...thanks in advance for ur help...

AnimationView.java

public class AnimationView extends View{
      public AnimationView(Context context, AttributeSet attrs) {
            super(context, attrs);
            // TODO Auto-generated constructor stub
        }
    private final int FRAME_RATE=15;
      private Paint paint;
          private Handler h;
          Ball myball;

        int value;
        public void setString(int value)
        {
       this.value = value;
       System.out.println("value="+value);
       Animation();
    }
        public void Animation()
         { 
             h=new Handler();
                paint=new Paint();
                paint.setColor(Color.BLUE);
        myball=new Ball(100,100,Color.BLUE,50);
                    myball.setDX(10);
                    myball.setDY(10);
                    myball.setColor(Color.BLUE);
        }


        }
protected void onDraw(Canvas c)
    {

c.drawCircle(myball.getX(), myball.getY(),myball.getRadius(), myball.getPaint());
h.postDelayed(r, FRAME_RATE);

    }
    private Runnable r=new Runnable()
    { public void run()
    { invalidate();
    }
    };

Ball.java

 public class Ball {

private Point p;
private int c;
private int r;
private int dx;
private int dy;
private Paint paint;
public static int count = 0;
public Ball(int x,int y,int col,int radius)
{
    p=new Point(x,y);
    c=col;
    r=radius;
    paint=new Paint();
    dx=0;
    dy=0;
}

public Paint getPaint()
{return paint;
}
public void setColor(int col)
{c=col;
}
}
도움이 되었습니까?

해결책

drawCircle takes the color from the Paint object. Change your setColor this way:

public void setColor(int col) {
   c=col;
   if (paint == null) 
      paint = new Paint();
   paint.setColor(col);
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top