我怎样才能使用的TimerTask得到一个光滑的举动? [英] How can i get a smooth move using timertask?

查看:105
本文介绍了我怎样才能使用的TimerTask得到一个光滑的举动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用下面的code键移动它工作正常,图像一但在移动图像不进行平滑移动。任何人都可以找到自己code中的问题?

  =处理新的处理程序();
    T =新的Timer();
    t.scheduleAtFixedRate(新的TimerTask(){
            公共无效的run(){
                    handler.post(新的Runnable(){
                            公共无效的run(){                                如果(左< = 400){                                 左=左+ 1;                                     RelativeLayout.LayoutParams RP =新RelativeLayout.LayoutParams(
                                                新ViewGroup.MarginLayoutParams(
                                                        RelativeLayout.LayoutParams.WRAP_CONTENT,
                                                        RelativeLayout.LayoutParams.WRAP_CONTENT));
                                                 rp.setMargins(左,顶,0,0);
                                                 Train.setLayoutParams(RP);                                }其他{
                                    Toast.makeText(getApplicationContext(),面包已完成,Toast.LENGTH_SHORT).show();
                                t.cancel();
                                }                            }
                    });            }    },0,30);


解决方案

TimerTask的和线程的是不是一致的用户界面的变化很好的选择。

1。通过entending SurfaceView创建CustomView。

2。一些管理类变量的观点立场。


  1. 在的onDraw()创建视图和更新变量跟踪定位。

4.wherever要手动刷新。呼叫无效;

按照本教程获取更多的帮助。

I used following code to move a image it works fine but while moving the image not perform a smooth moving. Can anyone find the problem in my code?

 handler = new Handler(); 
    t = new Timer(); 
    t.scheduleAtFixedRate(new TimerTask() { 
            public void run() { 
                    handler.post(new Runnable() { 
                            public void run() { 

                                if(left<=400){

                                 left=left+1;

                                     RelativeLayout.LayoutParams rp = new RelativeLayout.LayoutParams(
                                                new ViewGroup.MarginLayoutParams(
                                                        RelativeLayout.LayoutParams.WRAP_CONTENT,
                                                        RelativeLayout.LayoutParams.WRAP_CONTENT));
                                                 rp.setMargins(left, top, 0, 0);
                                                 Train.setLayoutParams(rp);

                                }else{
                                    Toast.makeText(getApplicationContext(), "Toast completed", Toast.LENGTH_SHORT).show();
                                t.cancel();
                                }

                            } 
                    }); 

            } 

    }, 0,30); 

解决方案

TimerTask and Thread's are not good choice for consistent UI changes .

1 . Create CustomView by entending SurfaceView .

2 . manage some class variables for position of views.

  1. in onDraw() create view and update variables for next positioning .

4.wherever want to refresh manually . call invalidate ;

Follow this tutorial for more help .

这篇关于我怎样才能使用的TimerTask得到一个光滑的举动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆