I made a sort of index activity with my personal logo.
I want that after 5 seconds change activity.
So I prepared 2 layout, I run the launcher.
In launcher activity I set:
Intent intent = new Intent(Home.this, IndexActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
startActivity(intent);
finish();
The problem is that the activity stay blocked in a white activity and after 5 seconds change activity.
Did I make a mistake?
How can I create a fade animation? And how can I run it? Thanks
You should never block the main thread. It is responsible for updating the UI and if you block it, the user interface will freeze.
Instead, use a Handler and its postDelayed method.
Thread.sleep() degrades the performance. You can use the Handler class' postDelayed() method to perform this:
Handler mHandler = new Handler();
mHandler.postDelayed(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(Home.this, IndexActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intent);
finish();
}
}, 5000L);
First declare this in your animation folder.
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true" >
<alpha
android:duration="1000"
android:fromAlpha="0.0"
android:interpolator="#android:anim/accelerate_interpolator"
android:toAlpha="1.0" />
</set>
Then declare an animation in acitivity's java file from where you are going :
Animation animfadein,animFadeout;
animFadein = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.fade_in);
animFadeout = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.fade_out);
Then after startActivity(Intent);
write overridePendingTransition(animFadein, animFadeout);
here'sfadeout :
fade_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true" >
<alpha
android:duration="1000"
android:fromAlpha="1.0"
android:interpolator="#android:anim/accelerate_interpolator"
android:toAlpha="0.0" />
</set>
Related
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.
I have an Activity that has a handler. The handler does some work then launches an intent to another Activity. I use overridePendingTransition after I launch the intent and call finish().
In Android 4.2 the zoom animation works fine but in android 5 it doesn't.
Are there any differences between the two versions of Android I need to know about with regards to animations?
public void initHandler(){
handler = new Handler();
runnable = new Runnable() {
public void run() {
processTag();
}
private void processTag() {
Log.e(TAG, "about to process tag");
.....
process some info
.......
handler.post(new Runnable(){
public void run() {
setContentView(R.layout.successfulnfc);
}
});
Intent processPayloadIntent = new Intent(NfcActivity.this, NfcscannerActivity.class);
processPayloadIntent.putExtra("payload", payload);
processPayloadIntent.putExtra("tagid", tagId);
processPayloadIntent.setAction("NFC");
processPayloadIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//processPayloadIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(processPayloadIntent);
finish();
overridePendingTransition(0, R.anim.activity_animation_zoom_in);
}
};
}
..
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<scale
android:duration="2000"
android:fillAfter="false"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:interpolator="#android:anim/linear_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:startOffset="0"
android:toXScale="0.55"
android:toYScale="0.55" />
</set>
I have a few controls that I don't want the user to see all the time, so they need to:
Fade out 5 seconds after activity is created
Fade in when user taps the screen
Fade out after 5 seconds of visibility or when user taps screen again (whichever comes first)
I've seen a lot of implementations of animations out there (including Thread, Handler, and Animation). Which method would work best in this situation?
Here I included the code which i had done in my project. Please check this out. It may help you.
Animation animationFadeIn = AnimationUtils.loadAnimation(this, R.anim.fade_in);
Animation animationFadeOut = AnimationUtils.loadAnimation(this, R.anim.fade_out);
handler = new Handle(this);
animator = new Thread() {
public void run() {
try {
handler.sendMessage(handler.obtainMessage(1));
sleep(2000);
handler.sendMessage(handler.obtainMessage(2));
sleep(2000);
handler.sendMessage(handler.obtainMessage(3));
} catch (Exception e) {
e.printStackTrace();
}
}
};
animator.start();
static class Handle extends Handler {
#Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
if (msg.what == 1) {
iv.startAnimation(animationFadeIn);
} else if (msg.what == 2) {
} else if (msg.what == 3) {
iv.startAnimation(animationFadeOut);
}
super.handleMessage(msg);
}
}
Use following code for fade in and fade out.
fade_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true" >
<alpha
android:duration="1000"
android:fromAlpha="0.0"
android:interpolator="#android:anim/accelerate_interpolator"
android:toAlpha="1.0" />
</set>
fade_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true" >
<alpha
android:duration="1000"
android:fromAlpha="1.0"
android:interpolator="#android:anim/accelerate_interpolator"
android:toAlpha="0.0" />
</set>
happy coding !!
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.