1] Write a code in MainActivity.java File:
package com.example.transitiondrawabledemo;
import android.app.Activity;
import android.content.res.Resources;
import android.graphics.drawable.TransitionDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
public class MainActivity extends Activity {
private ImageView image;
private TransitionDrawable trans;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image = (ImageView)findViewById(R.id.imageView1);
Resources res = this.getResources();
trans = (TransitionDrawable)res.getDrawable(R.drawable.transition);
image.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
image.setImageDrawable(trans);
trans.reverseTransition(1000);
}
});
}
}
2] Create File transition.xml In res\drawable\transition.xml
<?xml version="1.0" encoding="utf-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/a"/>
<item android:drawable="@drawable/b"/>
</transition>
Note: @drawable/a" a is the Image Name That You Want To Put Image
3] Write a Code in main.xml File:
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="51dp"
android:src="@drawable/a" />
</RelativeLayout>
Output:
Simply Swip in your Screen Then Image is Smoothly Change the Image:
package com.example.transitiondrawabledemo;
import android.app.Activity;
import android.content.res.Resources;
import android.graphics.drawable.TransitionDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
public class MainActivity extends Activity {
private ImageView image;
private TransitionDrawable trans;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image = (ImageView)findViewById(R.id.imageView1);
Resources res = this.getResources();
trans = (TransitionDrawable)res.getDrawable(R.drawable.transition);
image.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
image.setImageDrawable(trans);
trans.reverseTransition(1000);
}
});
}
}
2] Create File transition.xml In res\drawable\transition.xml
<?xml version="1.0" encoding="utf-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/a"/>
<item android:drawable="@drawable/b"/>
</transition>
Note: @drawable/a" a is the Image Name That You Want To Put Image
3] Write a Code in main.xml File:
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="51dp"
android:src="@drawable/a" />
</RelativeLayout>
Output:
Simply Swip in your Screen Then Image is Smoothly Change the Image:

Comments
Post a Comment