我想在ImageView上设置动画

时间:2017-07-22 07:55:33

标签: android

我想做一个折腾应用程序..它一直在旋转,并且不会停止显示头部或尾部..我希望当我点击硬币然后它应该旋转2 3秒然后停止到随机选择显示头部或尾部......但它会不断旋转。 这是代码......

public class MainActivity extends AppCompatActivity {
ImageView Coin;
@Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Coin=(ImageView) findViewById(R.id.ImgViewcoin);
    Coin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Animation animation = new AlphaAnimation(1, 0);
            animation.setInterpolator(new DecelerateInterpolator());
            animation.setDuration(3000);
            Coin.startAnimation(animation);
            animation.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {
                    final int[] photos = {R.drawable.heads, R.drawable.tails};
                    final ImageView image = (ImageView) findViewById(R.id.ImgViewcoin);
                    final Random ran = new Random();
                    int i = ran.nextInt(photos.length);
                    image.setImageResource(photos[i]);
                    image.setOnClickListener(new View.OnClickListener() {
                                                 public void onClick(View v) {
                                                     int k = ran.nextInt(photos.length);
                                                     image.setImageResource(photos[k]);
                                                 }
                                             }
                    );
                }
                @Override
                public void onAnimationEnd(Animation animation) {
                }
                @Override
                public void onAnimationRepeat(Animation animation) {
                }
            });
            Coin.startAnimation(animation);
         }
      });
  }
  }

1 个答案:

答案 0 :(得分:0)

从onclick中删除findviewbyid并使用你拥有的Coin实例。