How can i add animation to the splash screen? - java

I don't know why but animations for splash screen isn't working at all.
I guess my codes are correct!..
All I want is to perform the animation on the splash screen content.
public class Welcome extends AppCompatActivity {
Animation topAnim, bottomAnim;
ImageView imageView;
TextView textView;
private static int SPLASH_TIME_OUT=4000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setContentView(R.layout.activity_welcome);
topAnim = AnimationUtils.loadAnimation(this,R.anim.top_animation);
bottomAnim = AnimationUtils.loadAnimation(this,R.anim.bottom_animation);
imageView = findViewById(R.id.imageView3);
textView = findViewById(R.id.textView);
imageView.startAnimation(topAnim);
textView.startAnimation(bottomAnim);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_welcome);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent homeIntent =new Intent(Welcome.this,MainActivity.class);
startActivity(homeIntent);
finish();
}
},SPLASH_TIME_OUT);
}
}
These are the bottom and top animations .xml files respectively
<translate
android:fromXDelta="0%"
android:fromYDelta="100%"
android:duration="1500" />
<alpha
android:fromAlpha="0.1"
android:toAlpha="1.0"
android:duration="1500" />
<translate
android:fromXDelta="0%"
android:fromYDelta="-50%"
android:duration="2000" />
<alpha
android:fromAlpha="0.1"
android:toAlpha="1.0"
android:duration="1500" />

Replace this line
getWindow().setContentView(R.layout.activity_welcome);
With
setContentView(R.layout.activity_welcome);
And remove setContentView(R.layout.activity_welcome); from above
new Handler().postDelayed(new Runnable() {

Just remove the followimg line:
setContentView(R.layout.activity_welcome);
above this line: new Handler().postDelayed(new Runnable() {
And it would work fine.

Related

Android Studio - Animations between activities

I'm doing a "University Chat App". The first activity of the app is just the logo and the second activity is the sign in. I want to add a small fade from the first activity to the second activity.
This is the code for the fade in:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha android:fromAlpha="0.5"
android:toAlpha="1.0"
android:interpolator="#android:anim/accelerate_interpolator"
android:duration="9000" />
</set>e
This is the code for the fade out:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha android:fromAlpha="1.0"
android:toAlpha="0.0"
android:interpolator="#android:anim/accelerate_interpolator"
android:fillAfter="true"
android:duration="1000" />
</set>
MainActivity code:
public class MainActivity extends AppCompatActivity {
private static int WELCOME_TIMEOUT = 1500; // Sets time of the transition
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide(); // Hide Action Bar
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); // Hide the Status Bar
setContentView(R.layout.activity_main);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent welcome = new Intent(MainActivity.this, MainActivity2.class);
startActivity(welcome);
overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
finish();
}
}, WELCOME_TIMEOUT);
}
}
Everything works fine, but the fade out is quite slow and it creates this transparent gray screen before changing to the second activity. I'm assuming the gray screen comes from this line of the fade out android:toAlpha="0.0"
Is there a way to create a better fade between activities, without the transparent gray screen? Also, can different animations be done in between activities?

App crashes when using TransitionDrawable

I want to transition the color of my background when a button is clicked but my app seems to crash once I press the button. The button runs the TransitionDrawable code. I have set my background to the drawable folder containing the transition.Could anyone help me? Thank you very much!
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void colourChangeButton(View view) {
final TransitionDrawable transition = (TransitionDrawable) view.getBackground();
transition.startTransition(1000);
}
}
Here are my drawable files that define the colours:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<color android:color="#2575fc"></color>
</item>
</selector>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<color android:color="#ff0844"></color>
</item>
</selector>
Here is my transition drawable file :
<?xml version="1.0" encoding="utf-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/blue_background"></item>
<item android:drawable="#drawable/pink_background"></item>
</transition>
You are almost there!
You need to set the transition drawable as the background of your view.
Let's say your activity's layout xml is called activity_main.xml and your
transition drawable file is called transition.xml.
In the root layout of your activity_main.xml:
<LinearLayout
...
id="#+id/main_layout"
background="#drawable/transition">
<Button
id="#+id/start_transition_button"
... />
...
In MainActivity.java:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewGroup layout = (ViewGroup) findViewById(R.id.main_layout);
TransitionDrawable transition = (TransitionDrawable) layout.getBackground();
Button button = (Button) findViewById(R.id.start_transition_button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
transition.startTransition(500);
}
}
}
}
Here is a good post about it:
https://proandroiddev.com/transitiondrawable-small-gems-of-the-android-framework-4dcdd3c83319

Animate an imageview out of the activity and bring a fragment into the activity from the bottom: Android

I have a main activity, which has an imageview sliding in from the top.
I want to fade this imageview out with a delay of 2 seconds.
Then immediately have a fragment slide in from the bottom into the main activity.
How do I fade an imageview out with a delay?
Where do I define the fragment, in the xml or new java class?
And how do I animate the fragment?
MainActivity.java
public class MainActivity extends AppCompatActivity {
LinearLayout ml;
Animation uptodown;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
ml = (LinearLayout) findViewById(R.id.ml);
uptodown = AnimationUtils.loadAnimation(this,R.anim.uptodown);
ml.setAnimation(uptodown);
}
activity_main.xml
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="#drawable/backgroundcolour">
<LinearLayout
android:id="#+id/ml"
android:layout_width="match_parent"
android:layout_height="400dp"
android:orientation="vertical">
<ImageView
<!-- my imageview -->
/>
</LinearLayout>
uptodown.xml in res/anim
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:duration="800"
android:fromYDelta="-100%p"
android:fromXDelta="0%p"/>
</set>
add a FrameLayout at end of your xml like:
<FrameLayout
android:id="#+id/frame_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
and add your fragment to this frame layout like this:
FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
android.support.v4.app.Fragment newFragment;
newFragment = new YourFragment();
transaction.replace(R.id.frame_layout, newFragment);
transaction.commit();
add an slide animation like :
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="1000"
android:fromYDelta="100%"
android:toYDelta="0" />
</set>
and add this to your code:
FrameLayout frameLayout = findViewById(R.id.frame_layout);
Animation slide_up = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.slide_up);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
Animation fadeOut = new AlphaAnimation(1, 0);
fadeOut.setInterpolator(new AccelerateInterpolator()); //and this
fadeOut.setStartOffset(1000);
fadeOut.setDuration(1000);
ml.setAnimation(fadeOut);
frameLayout.setAnimation(slide_up);
}
},2000);

How to implement the splash screen image blink, without clicking?

SplashActivity.java
public class SplashActivity extends AppCompatActivity {
private static int SPLASH_TIME_OUT = 3000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
ImageView imageView = (ImageView) findViewById(R.id.imageView);
imageView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
blink(v);
//I am calling the animation only when the image is clicked.
//I want to perform animation as the page loads.
//Not on clicking the image.
}
});
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent i = new Intent(SplashActivity.this, LoginOptionsActivity.class);
startActivity(i);
finish();
}
}, SPLASH_TIME_OUT);
}
public void blink(View view){
ImageView image = (ImageView)findViewById(R.id.imageView);
Animation animation1 = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.blink);
image.startAnimation(animation1);
}
}
blink.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha android:fromAlpha="0.0"
android:toAlpha="1.0"
android:interpolator="#android:anim/accelerate_interpolator"
android:duration="600"
android:repeatMode="reverse"
android:repeatCount="infinite"/>
</set>
activity_splash.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="com.codeinks.luxehues.SplashActivity"
android:background="#drawable/splash_shirt">
<ImageView
android:layout_width="300dp"
android:layout_height="150dp"
android:id="#+id/imageView"
android:src="#drawable/fullsize"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
Please suggest a way to perform the animation
as the page loads and not while clicking the image.
Try this in onCreate method and comment that onClick method and give a try to below code,
ImageView imageView = (ImageView) findViewById(R.id.imageView);
Animation myAnim = AnimationUtils.loadAnimation(context,R.anim.blink);
imageView.startAnimation(myAnim);
Try below code:
Thread splashTread;
public void onAttachedToWindow() {
super.onAttachedToWindow();
Window window = getWindow();
window.setFormat(PixelFormat.RGBA_8888);
}
#Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
StartAnimations();
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
private void StartAnimations() {
Animation anim = AnimationUtils.loadAnimation(this, R.anim.alpha);
anim.reset();
LinearLayout l = (LinearLayout) findViewById(R.id.layoutAnimation);
l.clearAnimation();
l.startAnimation(anim);
anim = AnimationUtils.loadAnimation(this, R.anim.blink);
anim.reset();
ImageView iv = (ImageView) findViewById(R.id.imageSplash);
iv.clearAnimation();
iv.startAnimation(anim);
splashTread = new Thread() {
#Override
public void run() {
try {
int waited = 0;
// Splash screen pause time
while (waited < 3000) {
sleep(100);
waited += 100;
}
Intent intent = new Intent(SplashActivity.this, StartActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intent);
} catch (InterruptedException e) {
// do nothing
} finally {
SplashActivity.this.finish();
}
}
};
splashTread.start();
}
and corresponding xml layout is:
<!-- activity_splash.xml -->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/layoutAnimation"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activities.SplashActivity">
<ImageView
android:id="#+id/imageSplash"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="#string/splash"
android:scaleType="fitXY"
android:src="#drawable/splash" />
</LinearLayout>
If got any issue, plz bother me. Thinks will help You.

How can I show the fade in animation and countdowntimer one time for each textview?

I'm making a text-based game with Android Studio.
This is the Activity:
public class MainActivity extends Activity {
#Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView txt1 = (TextView)findViewById(R.id.textView5);
final TextView txt3 = (TextView)findViewById(R.id.textView6);
final TextView txt2 = (TextView)findViewById(R.id.textView7);
final TextView txt4 = (TextView)findViewById(R.id.textView8);
final ImageView img1 = (ImageView)findViewById(R.id.imageView2);
new CountDownTimer(2000, 1000) {
#Override
public void onTick(long millisUntilFinished) {}
#Override
public void onFinish() {
Animation fadeIn = AnimationUtils.loadAnimation(GameActivity.this, R.anim.fadein);
txt1.startAnimation(fadeIn);
Animation fadeIn2 = AnimationUtils.loadAnimation(GameActivity.this, R.anim.fadein);
txt2.startAnimation(fadeIn2);
txt1.setText("Text");
txt1.setVisibility(View.VISIBLE);
txt2.setText("Text");
txt2.setVisibility(View.VISIBLE);
}
}.start();
new CountDownTimer(5000, 1000){
#Override
public void onTick(long millisUntilFinished){}
#Override
public void onFinish(){
Animation fadeIn_img = AnimationUtils.loadAnimation(GameActivity.this, R.anim.fadein);
img1.startAnimation(fadeIn_img);
Animation fadeIn = AnimationUtils.loadAnimation(GameActivity.this, R.anim.fadein);
txt3.startAnimation(fadeIn);
Animation fadeIn2 = AnimationUtils.loadAnimation(GameActivity.this, R.anim.fadein);
txt4.startAnimation(fadeIn2);
img1.setImageResource(R.drawable.img);
txt3.setText("Text");
txt3.setVisibility(View.VISIBLE);
txt4.setText("Text");
txt4.setVisibility(View.VISIBLE);
}
}.start();
I want the animation and the countdowntimer to work only one time for each textview. When I restart the game the textviews have to display without anything.
It is like a list of textviews that display in succession every 2 seconds. When the user close and then restart the application, he has to find the state where he had finished.
How can I do that?
Thanks.
So it looks like a fainting textviews right? Heres an example
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
LinearLayout Container;
ArrayList<View> textList = new ArrayList<>();
int count = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Container = (LinearLayout) findViewById(R.id.Container);
for (int i = 0; i < Container.getChildCount(); i++) {
textList.add(Container.getChildAt(i));
}
fadeView(textList.get(0));
}
private void fadeView(final View view) {
final Animation anim = AnimationUtils.loadAnimation(this, R.anim.fadein);
count++;
anim.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) { }
#Override
public void onAnimationEnd(Animation animation) {
if (count == textList.size()) return;
fadeView(textList.get(count));
}
#Override
public void onAnimationRepeat(Animation animation) { }
});
view.startAnimation(anim);
}
}
fadein.xml
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fromAlpha="1.0"
android:repeatCount="0"
android:toAlpha="0.0" />
XML :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="#+id/Container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is first"
android:textSize="25dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is second"
android:textSize="25dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is third"
android:textSize="30dp" />
</LinearLayout>
Just make sure to put all your views (in this case textviews) inside an array to make it clean and proper.
Hope it helps. Cheers Happy codings.

Categories