Android 如何使用计时器移动画布形状

Android 如何使用计时器移动画布形状,android,canvas,timer,Android,Canvas,Timer,我的圆圈班 public class Circle extends View{ int y =0 ; public line(Context context) { super(context); // TODO Auto-generated constructor stub } protected void onDraw(Canvas c){ super.onDraw(c); Paint p=new Paint(Paint.ANTI_ALIAS_FLAG);

我的圆圈班

public class Circle extends View{
int y =0 ;


public line(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
}
protected void  onDraw(Canvas c){
    super.onDraw(c);
    Paint p=new Paint(Paint.ANTI_ALIAS_FLAG);
    p.setColor(Color.BLUE);
    p.setStrokeWidth(3);

    if (y==0){
        c.drawCircle(100,y+5,50,p);
    }
    else{
        c.drawCircle(100,y+5,50,p);
    }
}
public void invalidate(){
    invalidate();
}
主要活动

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    RelativeLayout rl=new RelativeLayout(this);
    setContentView(rl);
    rl.setBackgroundResource(R.drawable.ic_launcher);
    Circle c=new Circle(this);
    rl.addView(c);
    Timer t=new Timer();//here is Where I need help
    t.schedule(new MyNewTimer(),0,20);//here is Where I need help
    }



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
}

我从一些网站上得到了这个主意。我是android编程的初学者。我真的不知道如何用定时器移动这个形状。事实上,我不知道该把这次任务放在哪里,也不知道该把它放在哪里! 请帮帮我

我在onCreate()上添加了这个

我创建MyTimer类

public class MyTimer extends TimerTask{
@Override
public void run(){
    handler.post(new Runnable(){
        public void run(){
            //Here, I can't inherit circle.invalidate() 
        }
    }};

}

从后台线程,您需要调用
postInvalidate()
以查看
视图,而不是
invalidate()
(必须在UI线程上调用)。您可能可以这样做:

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        RelativeLayout rl=new RelativeLayout(this);
        setContentView(rl);
        rl.setBackgroundResource(R.drawable.ic_launcher);
        final Circle c = new Circle(this);
        rl.addView(c);
        Timer t=new Timer();
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                // update the y coordinate in c
                . . .
                // ask for the view to be redrawn
                c.postInvalidate();
            }
        };
        t.schedule(task, 0, 20);
    }
}
请注意,
c
必须在此代码中声明为
final
,以便可以从匿名
TimerTask
对象引用它。如果创建一个单独的类(例如
MyNewTimer
),则可以在构造函数中传递对
c
的引用,以便它可以访问视图


正如我在评论中指出的,您在
Circle
方面还有其他问题。首先,我要删除您编写的
invalidate
方法。

您的
onDraw
方法对于
有一个
if
语句,它绘制相同的东西,而不管
y
的值是多少;你应该解释一下你想要完成什么。另外,您的
invalidate()
方法将递归,直到堆栈溢出。最后,您需要告诉我们MyNewTimer类的功能。是的,是的。我早些时候创建了MyNewTimer类,但我不能从circle类继承y。MyNewTimer需要invalidate()是静态的,但是通过更改它,circle类中出现了一个错误。我不知道如何修复它。请给我建议,以满足正确的修复!非常感谢你,泰德·霍普!!我在onCreate()Timer=new Timer()上添加了这个;timer.schedule(新的MyTimer(),0,20);创建类MyTimer公共类MyTimer扩展TimerTask{@Override public void run(){handler.post(新建Runnable(){public void run(){//I cant call invalidate(),如何调用?}}};}您的代码非常有用!非常感谢您抽出时间和分享知识!我的圈子正在移动!!:)谢谢!
public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        RelativeLayout rl=new RelativeLayout(this);
        setContentView(rl);
        rl.setBackgroundResource(R.drawable.ic_launcher);
        final Circle c = new Circle(this);
        rl.addView(c);
        Timer t=new Timer();
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                // update the y coordinate in c
                . . .
                // ask for the view to be redrawn
                c.postInvalidate();
            }
        };
        t.schedule(task, 0, 20);
    }
}