为什么我的球没有显示颜色?

时间:2014-04-09 08:36:59

标签: android android-canvas

在我的项目中,我已经为我的球添加了颜色,但它没有显示..为什么? 我只发布了那些对我的问题有意义的代码部分....是否存在setColor()方法或其他一些问题...提前感谢您的帮助......

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;
}
}

1 个答案:

答案 0 :(得分:3)

drawCircle获取Paint对象的颜色。以这种方式更改setColor

public void setColor(int col) {
   c=col;
   if (paint == null) 
      paint = new Paint();
   paint.setColor(col);
}