Android picture flip animation simple implementation code

  • 2020-05-07 20:23:18
  • OfStack

Here is an interesting animation for you to share: here is more suitable for the flip of a picture, if it is more than one picture, you can refer to the example in APIDemo, is to add an ArrayAdapter, or simple, you can also play their own modification, to achieve what you want. The code here is basically ready to run the project directly.
Add an ImageView to main.xml, e.g

 
<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/container" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 
<ImageView 
android:id="@+id/image" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:text="Rotate" 
android:textSize="50px" 
android:layout_x="150px" 
android:layout_y="30px" 
android:src="@drawable/ro"> 
></ImageView> 
</FrameLayout> 

You don't need to explain this, you can understand it
Finally, you need one activity class
Such as:
 
public class TestRotate extends Activity implements OnClickListener{ 
private mageView imageview; 
private ViewGroup mContainer; 
/** 
* This variable sets the image, if more than one image, then you can use an array, such as  
*private static final int IMAGE = new int[]{ 
* R.drawable.ro, 
* R.drawable.icon 
*}; 
* I'm going to put as many pictures as I have, and that's all I'm doing here 1 Flip a picture  
* 
*/ 
private static final int IMAGE = R.drawable.ro; 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.main); 
imageview = (ImageView) findViewById(R.id.image); 
mContainer = (ViewGroup) findViewById(R.id.container); 
/** 
*  Set the latest displayed image  
*  If it's an array, then you can write this as an array IMAGE[int] 
* 
*/ 
imageview.setImageResource(IMAGE); 
/** 
* 
*  Set up the ImageView the OnClickListener 
* 
*/ 
imageview.setClickable(true); 
imageview.setFocusable(true); 
imageview.setOnClickListener(this); 
} 
private void applyRotation(int position, float start, float end) { 
// Find the center of the container 
final float centerX = mContainer.getWidth() / 2.0f; 
final float centerY = mContainer.getHeight() / 2.0f; 
final Rotate3d rotation = 
new Rotate3d(start, end, centerX, centerY, 310.0f, true); 
rotation.setDuration(500); 
rotation.setFillAfter(true); 
rotation.setInterpolator(new AccelerateInterpolator()); 
rotation.setAnimationListener(new DisplayNextView(position)); 
mContainer.startAnimation(rotation); 
} 
@Override 
public void onClick(View v) { 
// TODO Auto-generated method stub 
/** 
* 
*  To call this method, I'm going to flip the image  
*  The parameters are so simple that you should be able to read them  
*  Briefly, no 1 One is location, one 2 It's the starting Angle, number 3 It's the end Angle  
*  And what I want to show you here is that if I want to go back up 1 zhang  
*  The first 1 Is set to -1 We have to do is  
* 
*/ 
applyRotation(0,0,90); 
} 
private final class DisplayNextView implements Animation.AnimationListener { 
private final int mPosition; 
private DisplayNextView(int position) { 
mPosition = position; 
} 
public void onAnimationStart(Animation animation) { 
} 
public void onAnimationEnd(Animation animation) { 
mContainer.post(new SwapViews(mPosition)); 
} 
public void onAnimationRepeat(Animation animation) { 
} 
} 
/** 
* This class is responsible for swapping the views and start the second 
* half of the animation. 
*/ 
private final class SwapViews implements Runnable { 
private final int mPosition; 
public SwapViews(int position) { 
mPosition = position; 
} 
public void run() { 
final float centerX = mContainer.getWidth() / 2.0f; 
final float centerY = mContainer.getHeight() / 2.0f; 
Rotate3d rotation; 
if (mPosition > -1) { 
imageview.setVisibility(View.VISIBLE); 
imageview.requestFocus(); 
rotation = new Rotate3d(90, 180, centerX, centerY, 310.0f, false); 
} else { 
imageview.setVisibility(View.GONE); 
rotation = new Rotate3d(90, 0, centerX, centerY, 310.0f, false); 
} 
rotation.setDuration(500); 
rotation.setFillAfter(true); 
rotation.setInterpolator(new DecelerateInterpolator()); 
mContainer.startAnimation(rotation); 
} 
} 
} 


Related articles: