【问题标题】:Why my onclick can't trigger Animation Imageview为什么我的 onclick 不能触发 Animation Imageview
【发布时间】:2017-12-08 12:51:59
【问题描述】:

我想通过使用 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();
    }
};

【问题讨论】:

标签: java android onclicklistener buttonclick


【解决方案1】:

试试ViewPropertyAnimator API,更方便:

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

【讨论】:

  • 它有效!谢谢你!但是如何控制顺时针或逆时针呢?
  • 每次点击后如何刷新图片?
  • 我猜逆时针应该有负角,例如-20。我不明白你的第二个问题,刷新图片是什么意思?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多