Example- Explain Translate Animation in android?
Translate Animation that controls positions. Initialize this animation with the dimension in the objects beings animated. show this example ..move image top to right bottom ..
TranslateAnimation animation = new
TranslateAnimation(float fromXDelta, float toXDelta,float fromYDelta,
float toYDelta) ;
animation.setDuration(5000);
you can assign all float value accordingly.
Example Code-
activity_main.xml.
............................................................................................................
xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:id="@+id/img_animation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="42dp"
android:src="@mipmap/ic_launcher" />
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:id="@+id/img_animation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="42dp"
android:src="@mipmap/ic_launcher" />
</RelativeLayout>
MainActivity.java
............................................................................................................
import
android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
Animation animTogether;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView img_animation = (ImageView) findViewById(R.id.img_animation);
TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)
TranslateAnimation animation = new TranslateAnimation(0.0f, 400.0f, 0.0f, 1000.0f);
animation.setDuration(5000);
animation.setRepeatCount(Animation.INFINITE);
animation.setRepeatMode(2);
animation.setFillAfter(true);
img_animation.startAnimation(animation);
}
}
import android.support.v7.app.AppCompatActivity;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
Animation animTogether;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView img_animation = (ImageView) findViewById(R.id.img_animation);
TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)
TranslateAnimation animation = new TranslateAnimation(0.0f, 400.0f, 0.0f, 1000.0f);
animation.setDuration(5000);
animation.setRepeatCount(Animation.INFINITE);
animation.setRepeatMode(2);
animation.setFillAfter(true);
img_animation.startAnimation(animation);
}
}
0 comments