类似于转硬币的那种,选两张相似的图片。作为开始和结束的图片:
<ImageView
android:id="@+id/flip_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
/>
在布局里面只设置一个imageView 分别装那两张图片。
具体代码:
private boolean mIsHeads;
private ObjectAnimator mFlipper;//定义ObjectAnimator 对象
private Bitmap mHeadsImage, mTailsImage;//定义用于显示图片的Bitmap对象
private ImageView mFlipImage;//显示的imageview的对象,
mHeadsImage = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); //获取到bitmap对象
mTailsImage = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
mFlipImage = (ImageView) findViewById(R.id.flip_image);
mFlipImage.setImageBitmap(mHeadsImage);
mIsHeads = true;
mFlipper = ObjectAnimator.ofFloat(mFlipImage, "rotationY", 0f, 360f);//设置绕Y轴的旋转,旋转为360
mFlipper.setDuration(500);
mFlipper.addUpdateListener(new AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
if (animation.getAnimatedFraction() >= 0.25f && mIsHeads) {
mFlipImage.setImageBitmap(mTailsImage);
mIsHeads = false;
}
if (animation.getAnimatedFraction() >= 0.75f && !mIsHeads) {
mFlipImage.setImageBitmap(mHeadsImage);
mIsHeads = true;
}
}
});
@Override
public boolean onTouchEvent(android.view.MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
mFlipper.start();//启动动画
return true;
}
return super.onTouchEvent(event);
}