In the below example I have created a Accelerate image function. Here I have added Button and ImageView in activity_main.xml layout then I have created new directory (anim). In anim directory I have created accelerate_decelerate.xml layout and in MainActivity I have used AnimationUtils.loadAnimation function setOnClickListener method. You can see below program it will clearly describe you to Accelerate image in android.
Step(1)activity_main.xml-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="bottom"
android:layout_margin="30dp">
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/one"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<Button
android:id="@+id/acceleratedecelerate"
android:layout_marginTop="20dp"
android:layout_width="190dp"
android:layout_height="wrap_content"
android:background="@color/colorAccent"
android:text="Click button"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
Step(2)-MainActivity-
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Animation animAccelerateDecelerate = AnimationUtils.loadAnimation(this, R.anim.accelerate_decelerate);
final ImageView image = (ImageView)findViewById(R.id.image);
Button btnAccelerateDecelerate = (Button)findViewById(R.id.acceleratedecelerate);
btnAccelerateDecelerate.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
image.startAnimation(animAccelerateDecelerate);
}});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Step(3)-In anim directory I have created accelerate_decelerate.xml layout-
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator">
<translate
android:fromYDelta="-100%p"
android:toYDelta="0"
android:duration="2000"/>
</set>
0 Comment(s)