为什么我的动画创建java代码不能在我的android项目中工作?

时间:2014-07-14 15:34:32

标签: java android

事实上,我的按钮立即变为蓝色,然后我看到x&在Toast消息中的y值! 我怎么能看到改变规模的动画?

    Button B = (Button) findViewById(R.id.button1);
    B.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            view.setBackgroundColor(Color.RED);
            // I want to create a scale animation for a button on this for loop
            float x , y;
            for(x = (float) 1.0 ,  y = (float) 1.0 ; x < (float) 2.0 && y < (float) 2.0 ; x += (float) 0.1 , y += (float) 0.1){
                view.setScaleX(x);
                view.setScaleY(y);
                Toast.makeText(MainActivity.this, Float.toString(x) + " " + Float.toString(y), 5).show();
                final Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {}
                }, 100);
            }
            view.setBackgroundColor(Color.BLUE);
            view.setScaleX((float)1.0);
            view.setScaleY((float)1.0);
        }
    });

感谢之前的

1 个答案:

答案 0 :(得分:2)

此解决方案不正确。

对于动画,您需要使用用于动画的类。 例如缩放动画如下所示:

ScaleAnimation scaleAnimation = new ScaleAnimation(1, 0.5, 1, 0.5);
scaleAnimation.setDuration(3000);
scaleAnimation.setFillAfter(true)
view.startAnimation(scaleAnimation);