I'm doing an image viewer that pass pictures to next or previous, by clicking right/left button.
I wanna to animate the transition between the pictures. I did it using ImageSwitcher, it is working, but I still have a problem.
The animation loaded is always the same, always sliding out right, making no difference if I click on right or left button.
QUESTION: How can I set which animation will run when I click my buttons?
I did my code based on this blog: http://saigeethamn.blogspot.com/2010/05/image-switcher-view-android-developer.html
Here same important parts of the code:
// OnCreate()
in = AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left);
out = AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right);
imageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher1);
imageSwitcher.setFactory(this);
imageSwitcher.setInAnimation(in);
imageSwitcher.setOutAnimation(out);
Now the click event (bm1 and bm2 are bitmaps):
private OnClickListener mClickListener = new OnClickListener() {
#Override
public void onClick(View v) {
if (v.getId()==R.id.buttonRight) {
Drawable d =new BitmapDrawable(getResources(),bm1);
imageSwitcher.setImageDrawable(d);
} else
if (v.getId()==R.id.buttonLeft) {
Drawable d =new BitmapDrawable(getResources(),bm2);
imageSwitcher.setImageDrawable(d);
}
}
};
How the ImageSwitcher know witch animation it will perform? If is In Left or Out Right?
EDITED FROM HERE ----------------------------------------------------
Solution:
private OnClickListener mClickListener = new OnClickListener() {
#Override
public void onClick(View v) {
if (v.getId()==R.id.buttonRight) {
imageSwitcher.setInAnimation(this, R.anim.slide_in_left); // added
imageSwitcher.setOutAnimation(this, R.anim.slide_out_left); // added
Drawable d =new BitmapDrawable(getResources(),bm1);
imageSwitcher.setImageDrawable(d);
} else
if (v.getId()==R.id.buttonLeft) {
imageSwitcher.setInAnimation(this, R.anim.slide_in_right); // added
imageSwitcher.setOutAnimation(this, R.anim.slide_out_right); // added
Drawable d =new BitmapDrawable(getResources(),bm2);
imageSwitcher.setImageDrawable(d);
}
}
};
Based on the answer of Rui Gaspar. Need to create the xmls he shows.
You can implement your own animation
slide_in_left.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="100%p" android:toXDelta="0"
android:duration="350"/>
</set>
slide_in_right.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="-100%p" android:toXDelta="0"
android:duration="350"/>
</set>
slide_out_left.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="0" android:toXDelta="-100%p"
android:duration="350"/>
</set>
slide_out_right.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="0" android:toXDelta="100%p"
android:duration="350"/>
</set>
You can use it like this:
//Switch Left
Intent myIntent = new Intent(m_context, MyActivity.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(myIntent);
overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_left);
//Switch Right
Intent myIntent = new Intent(m_context, MyActivity.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(myIntent);
overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_right);
Related
I would like to add an animation before the opening of a new Android Empty Activity. Something like a chroma-keyed video played on top of the current activity and at the end of it, the secondary activity opens up.
You create a splash activity that includes your animation, and you implement an AnimationListener. Inside the method onAnimationEnd() you create the intent that takes you to the second activity. Don't forget to set the splash activity as the Launcher activity on your manifest.
animationObject.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
Intent intent = new Intent (SplashActivity.this, MainActivity.class);
startActivity(intent); }
#Override
public void onAnimationRepeat(Animation animation) {
}
});
EDIT: if you want to play a video with media player instead you use a playback listener and run the same intent from onCompletion()
After your startActivity method use the overridePendingTranistion and put the animations in it
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(ActivityA.this, ActivityB.class));
overridePendingTransition(R.anim.enter, R.anim.exit);
}
});
The xml animations are as follows
enter.xml:
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:duration="500"
android:fromXDelta="100%"
android:fromYDelta="0%"
android:toXDelta="0%"
android:toYDelta="0%" />
</set>
exit.xml:
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:duration="500"
android:fromXDelta="0%"
android:fromYDelta="0%"
android:toXDelta="-100%"
android:toYDelta="0%" />
</set>
i have 5 to 6 activities in my application I have added left to right and right to left animation on activity start and on finish activity but its not working properly when I click for new activity its working fine it start from right to left but when i press back button its also start from right to left. but when I press hardware back button its work from left to right. here is my code
right to left animation
leftin.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="100%p"
android:toXDelta="0"
android:duration="500"/>
leftout.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="0"
android:toXDelta="-100%p"
android:duration="500"/>
rightin.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="-100%p"
android:toXDelta="0"
android:duration="500"/>
rightout.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="0"
android:toXDelta="100%p"
android:duration="500"/>
here is code oncreate activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sub__category);
overridePendingTransition(R.anim.left_in, R.anim.left_out);
}
and here is code of my_back button
btn_back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myint = new Intent(Sub_Category_Activity.this,Home.class);
startActivity(myint);
overridePendingTransition(R.anim.right_in, R.anim.right_out);
finish();
}
});
and here is code of my onbackpress override method.
#Override
public void onBackPressed() {
super.onBackPressed();
Intent myint = new Intent(Sub_Category_Activity.this,Home.class);
startActivity(myint);
overridePendingTransition(R.anim.right_in, R.anim.right_out);
finish();
}
here I am facing problem is that when I press hardware back button its work fine but when i press back button of my UI it not start from left to right but start new activity form right to left
Use this code:
overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
Slide in right xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false" >
<translate android:duration="800" android:fromXDelta="100%" android:toXDelta="0%" />
<alpha android:duration="800" android:fromAlpha="0.0" android:toAlpha="1.0" />
</set>
Slide out left xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false" >
<translate android:duration="800" android:fromXDelta="0%" android:toXDelta="-100%"/>
<alpha android:duration="800" android:fromAlpha="1.0" android:toAlpha="0.0" />
</set>
write overridePendingTransition(R.anim.right_in, R.anim.right_out);
after finish(); method.
I don't think you need to invoke overridependingtransitions() method in onCreate().
hi here is how i achieved this animation. i know my thinking of achieving this is not professional way but finally i achieved it by my way.
here is i have activity A,B,C so on activity B i check that is it clicked from Activity A or C and according to that i used if condition and implement animation some way like below code
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
pref = getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);
editor = pref.edit();
String fromhome =pref.getString("from_home_list_adapter","");
if(fromhome.equals("yes"))
{
editor.putString("from_home_list_adapter","no");
editor.commit();
overridePendingTransition(R.anim.left_in, R.anim.left_out);
}
again i say this is not professional way to handle animation but its works
I have set up the fade in functionality properly but its not working
MainActivity.java
setContentView(R.layout.activity_main);
ImageView iv = (ImageView) findViewById(R.id.sname);
final Animation animationFadeIn = AnimationUtils.loadAnimation(this, R.anim.fadein);
iv.startAnimation(animationFadeIn);
Thread timer = new Thread(){
public void run(){
try{
sleep(4000);
}catch(InterruptedException e)
{
e.printStackTrace();
}finally{
Intent in = new Intent(getApplicationContext(),HomescreenJ.class);
startActivity(in);
}
}
};
timer.start();
fadein.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/linear_interpolator">
<alpha
android:fromAlpha="0.1"
android:toAlpha="1.0"
android:duration="2000"
/>
</set>
Kindly its basically my splash screen. On which i want one image to be stationary and other one to be displayed with Fade-in effect.
How can i impliment slide etc.. animation transition when i open new activity by listview item click ?
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent intent = null;
// global string to class
selectedValue = String.valueOf(parent.getItemAtPosition(position));
if (selectedValue.equals("item1")) {
// ^^^ use any item value here you want
Intent myIntent = new Intent(view.getContext(), activity1.class);
startActivityForResult(myIntent,0);
}
else if (selectedValue.equals("item2")) {
Intent myIntent = new Intent(view.getContext(), aactivity4.class);
startActivityForResult(myIntent,0);
}
}
});
santoash kumar answer is correct. If you struggle on R.anim.layout resource here an animation code work like how other chat application work(slide animation) when you click on the listview item.
Add those into your anim resource
R.anim.push_left_in
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="100%" android:toXDelta="0" android:duration="#android:integer/config_mediumAnimTime"/>
</set>
R.anim.push_left_out
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="0" android:toXDelta="-20%" android:duration="#android:integer/config_mediumAnimTime"/>
</set>
R.anim.push_right_in
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="-20%" android:toXDelta="0" android:duration="#android:integer/config_mediumAnimTime"/>
</set>
R.anim.push_right_out
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="0" android:toXDelta="100%" android:duration="#android:integer/config_mediumAnimTime"/>
</set>
In the activity1 or any activity you will like to perform this animation while it launch,add this code after your set setContentView()
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
//rest of your code include setContentView();
}
Now you will find a problem when you try to navigation back to your listView activity either with pressing the back button or click on the view which represent the back button the animation still seem to be the default animation so do this when you try to end your current activity.
finish();
overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
For the back button pressed use this code.
#Override
public void onBackPressed() {
super.onBackPressed();
overridePendingTransition(R.anim.push_right_in,R.anim.push_right_out);
}
If the animation I provide doesn't suit the animation you desire you can try to make your own custom animation from this link.
startActivityForResult(myIntent,0);
overridePendingTransition(R.anim.hold, R.anim.fade_in);
hold and fade in will be animation xmls
I would like to switch the activities using animation..
I have a view with 5 images and with that i set my oncliclk listener.
My main activity is
private View.OnClickListener onClickListener = new View.OnClickListener()
{
public void onClick(View testView)
{
if (testView == Main.this.myinput)
{
Intent i = new Intent(Main.this, Activity1.class);
startActivity(i);
overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
}
if (testView == Main.this.myinput)
{
Intent i = new Intent(Main.this, Activity2.class);
startActivity(i);
overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
}
}
};
and my animation files are
fade_in:
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/accelerate_interpolator"
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration="500" />
fade_out:
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/accelerate_interpolator"
android:fromAlpha="1.0" android:toAlpha="0.0"
android:fillAfter="true"
android:duration="500" />
Now my problem is when switching between activities, animation has no effects on real devices but which is working on emulator.... I referred to stack overflow and googled but couldn't find why animation is not working..
I tried this with various type of animations such as slide_in_left,right,top,bottom...
but animation is not working. Help me in resolving this issue.Thanks in advance..
You should add overridePendingTransition(R.anim.fade_in, R.anim.fade_out); in your second Activity. For example :
public class SecondActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Activity open animation
overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
setContentView(R.layout.activity_fragment_container);
}
#Override
public void onPause() {
super.onPause();
// Activity closing animation
overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
}
}
Doing this you can control your Activity's start and close animation.
I had the same problem. Then i find decision. You should enable animation on your phone.
Go to : Settings > Display > Animation(Path can vary from device to device). Enable animation here. Hope this helps.