TranslateAnimation means nothing more than a picture or other to reach another position from one position. Direct code analysis and relevant important attribute parameters are explained in the code.
1. Write the file first.
<RelativeLayout xmlns:andro xmlns:tools="/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <ImageView android: android:text="@string/hello_world" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/car_one1"/> </RelativeLayout>
2. Next write the file.
package ; import .; import ; import ; import ; import ; import ; import ; public class MainActivity extends AppCompatActivity { private ImageView image; @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(); image =(ImageView)findViewById(); (new OnClickListenerImpl()); } private class OnClickListenerImpl implements { @Override public void onClick(View v) { /* AnimationSet is equivalent to a collection of animations, true means using Animation's interpolator false is to use your own. Interpolator is used to modify animation effects, define the change rate of animation, and can make existing animation effects accelerated, decelerated, repeated, bounced, etc. */ AnimationSet animationSet = new AnimationSet(true); /* There are several other methods for Animation setFillAfter(boolean fillAfter) If the value of fillAfter is true, the control will remain in the state after the animation is finished setFillBefore(boolean fillBefore) If the value of fillBefore is true, the control will remain at the beginning of the animation after the animation is over. setStartOffset(long startOffset) Set the time to wait before the animation control is executed setRepeatCount(int repeatCount) Set the number of times the animation is repeated */ TranslateAnimation translateAnimation = new TranslateAnimation( //The initial position of the X-axis Animation.RELATIVE_TO_SELF, 0.0f, //The end position of the X-axis movement Animation.RELATIVE_TO_SELF,0.5f, //The y-axis start position Animation.RELATIVE_TO_SELF,0.0f, //The end position after the y-axis moves Animation.RELATIVE_TO_SELF,1.5f); // Complete the animation in 3 seconds (2000); //If the value of fillAfter is true, the control will remain in the state after the animation is finished. (true); //Add AlphaAnimation, the already set animation, to the AnimationSet (translateAnimation); //Start the animation (animationSet); } } }
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.