How to show part of the image on the screen at the end of the animation

I am creating an animation in which the image is transferred from one direction to another, the animation moves half of the image on the side of the screen, but the image is fully displayed when the animation ends. I just want to show half the image after the animation.

I am currently using the full image in the image view when the animation starts, and replace it with the half image when the animation ends, but it shows a reflection of the image, which looks uncomfortable, which is my real problem.

Below is my xml and class file.

animation code

<translate xmlns:android="http://schemas.android.com/apk/res/android" android:duration = "2000" android:fillBefore="true" android:fillEnabled="true" android:fromXDelta = "-300%" android:fromYDelta="200%" android:toXDelta="60%"> </translate> animimv5.setAnimationListener(new AnimationListener() { @Override public void onAnimationStart(Animation animation) { imv5.setBackgroundResource(R.drawable.img5); } @Override public void onAnimationRepeat(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { imv5.setBackgroundResource(R.drawable.img5_2); } }); 
+7
android animation
source share
2 answers

You can try the following:

in xml (add it to the installation parameter) android:fillAfter or using the associated method in the source code setFillAfter(boolean)

If set to true, animation transformation is applied after the animation is finished. According to the documentation here When set to true, animation transformation is applied after the animation is finished. The default value is incorrect. If fillEnabled is not set to true and the animation will not be set in the view, fillAfter is considered true.

For more tips and clarifications, check out the Chet Blog Post.

Hope this helps ..;)

+1
source share

Here you go sir:

 MainActivity : public class MainActivity extends Activity { private View animatedView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); animatedView = findViewById(R.id.view); } public void animate(View view) { int targetX = ((View) animatedView.getParent()).getWidth() - animatedView.getWidth() / 2; ObjectAnimator.ofFloat(animatedView, "x", 0, targetX).setDuration(1000).start(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } } 

XML format:

  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:id="@+id/view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#CC0000" android:padding="30dp" android:textColor="@android:color/white" android:text="@string/hello_world"/> <Button style="?android:attr/buttonStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Animate" android:onClick="animate" android:id="@+id/button" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true"/> </RelativeLayout> 
0
source share

All Articles