为什么我的onclick无法触发动画Imageview

时间:2017-12-08 02:51:50

标签: java android onclicklistener buttonclick

我想通过使用Onclick()使我的图像视图旋转到随机角度。但为什么Onlick()可以工作,但Imageview无法旋转?这是我的代码。

ImageView rotate_plate;
Button debug_button;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    debug_button=(Button) findViewById(R.id.test_button);
    debug_button.setOnClickListener(btngoOnClick);

}
private OnClickListener btngoOnClick = new OnClickListener() {
    public void onClick(View v) {

        debug_button.setText("Click");
        rotate_plate=(ImageView) findViewById(R.id.wheel);
        int stopturnnum = (int)(Math.random() *360);
        Animation am = new RotateAnimation(0, stopturnnum, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);

        am.setDuration( 3000 );
        am.setRepeatCount(1);
        am.setFillAfter(true);
        rotate_plate.setAnimation(am);
        am.startNow();
    }
};

1 个答案:

答案 0 :(得分:0)

尝试使用ViewPropertyAnimator API,这样会更方便:

rotate_plate.animate()
    .rotation(angle)
    .setDuration(3000L)