android计时器动画挂机很少时间

时间:2014-10-31 12:32:34

标签: java android

我对Android很新,我想设置一个按钮动画。我所做的就是在100毫秒内快速改变它在两种颜色之间的背景。但它并没有很好地工作.button& #39; s的背景变了,但是在它停留至少5,6秒之后它就会卡住,我无法看到动画和ui冻结......

这是我的相关代码..

    int c = Color.parseColor("#00FF00");
    int c2 = Color.parseColor("#FFFF00");

    EditText t1;
    Button b5;
    Button b6;
    boolean btf=false;


    final Handler handler = new Handler();
    Runnable runable = new Runnable() {

        @Override
        public void run() {
            try{
                //do your code here
                //also call the same runnable

                if (btf){
                   b6.setBackgroundColor(c);
                    btf=false;
                }else {
                   b6.setBackgroundColor(c2);
                    btf=true;
                }
                handler.postDelayed(this, 100);
            }
            catch (Exception e) {
                // TODO: handle exception
            }
            finally{
                //also call the same runnable
                handler.postDelayed(this, 100);
            }
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.boad);

         b5= (Button) findViewById(R.id.button5);
         b6= (Button) findViewById(R.id.button6);

          b5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                   handler.postDelayed(runable, 100);
            }
        });
   }

1 个答案:

答案 0 :(得分:2)

请通过此代码

更新处理程序代码
 final Handler handler = new Handler();
    Runnable runable = new Runnable() {

        @Override
        public void run() {
            try{
                //do your code here
                //also call the same runnable

                if (btf){
                   b6.setBackgroundColor(c);
                    btf=false;
                }else {
                   b6.setBackgroundColor(c2);
                    btf=true;
                }
            }
            catch (Exception e) {
                // TODO: handle exception
            }

    };