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.
Related
I have some troubles with Interpolator...
I tried to make fadeIN/OUT but this function does not work. I have set delay 1000ms, but fading effect is not shown. Could you help me out? I checked some tutorials and all should be set correctly except layout width/height which should not be presented in XML. When i remove them there is message that those fields are mandatory:(
XMLs:
FADEIN
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/accelerate_interpolator"
android:duration="1000"
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
---------------------
FADEOUT
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:interpolator="#android:anim/decelerate_interpolator"
android:duration="1000"
android:fromAlpha="1.0"
android:toAlpha="0.0">
</alpha>
---------
Main Activity
public class WelcomePage extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome_page);
new Handler().postDelayed(new Thread(){
public void run(){
Intent mainMenu = new Intent(WelcomePage.this,mainMenu.class);
WelcomePage.this.startActivity(mainMenu);
WelcomePage.this.finish();
overridePendingTransition(R.layout.fadein,R.layout.fadeout);
}
},GCEngine.GAME_THREAD_DELAY);
}
}
Set this to fade_out:
android:fillAfter="true"
Than use this code:
new Handler().postDelayed(new Thread(){
public void run(){
Intent mainMenu = new Intent(WelcomePage.this,mainMenu.class);
WelcomePage.this.startActivity(mainMenu);
overridePendingTransition(R.layout.fadein,R.layout.fadeout);
WelcomePage.this.finish();
}
},GCEngine.GAME_THREAD_DELAY);
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>
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 tried in so many ways to get start up screen while opening the app but unable to get as like Linked-in android mobile app home screen.Here i need to show one part of big image in first slide then after 2 seconds it automatically moves right to left and shows second part that image with text and click-able button. How to achieve it please help me.
Here is the linked in mobile app link: https://play.google.com/store/apps/details?id=com.linkedin.android&hl=en
I tried this but not getting as exactly i want
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.first);
//display the logo during 2 seconds,
new CountDownTimer(2*1000,1000){
#Override
public void onTick(long millisUntilFinished){}
#Override
public void onFinish(){
//set the new Content of your activity
YourActivity.this.setContentView(R.layout.second);
}
}.start();
}
As per my understanding you require splash screen ..
Splash.java
---------------
public class Splash extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_splash);
Thread logoTimer = new Thread() {
public void run() {
try {
int logoTimer = 0;
while (logoTimer < 3000) {
sleep(100);
logoTimer = logoTimer + 100;
}
Intent intent = new Intent(Splash.this, NewClassToOpen.class);
startActivity(intent);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
finish();
}
}
};
logoTimer.start();
}
}
activity_splash.xml
-------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="#drawable/splash"
>
</LinearLayout>
#drawable/splash --- > Place image to drawable folders with name "splash / or as you need"
This is for left to right animation:
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate android:fromXDelta="-100%" android:toXDelta="0%"
android:fromYDelta="0%" android:toYDelta="0%"
android:duration="700"/>
</set>
This is for right to left animation:
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:fromXDelta="0%" android:toXDelta="100%"
android:fromYDelta="0%" android:toYDelta="0%"
android:duration="700" />
</set>
Enjoy .. Cheers !
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);