Android Studio - Animations between activities - java

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?

Related

How Can I make a windows to pop up from the bottom of the screen?

I have a Map, with markers on the Gas Stations around my location. When I click on them, I want a window to raise from the bottom of the screen (and go only half through the Map Screen) where I want to display info about that gas station. How do I do this window coming from the bottom of the screen? Animation?
Declarations :
Animation slideup, slidedown;
LinearLayout bottomLay;
Initializations:
slideup = AnimationUtils.loadAnimation(this, R.anim.slide_up);
slidedown = AnimationUtils.loadAnimation(this, R.anim.slide_down);
bottomLay = findViewById(R.id.bottomLay); //your bottom view
start the animation :
public void startSlideDown() {
bottomLay.startAnimation(slidedown); // down
}
or
public void startSlideUp() {
bottomLay.startAnimation(slideup); // up
}
slide_down.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/accelerate_decelerate_interpolator">
<translate
android:duration="#android:integer/config_mediumAnimTime"
android:fromYDelta="0"
android:toYDelta="100%p" />
</set>
slide_up.xml
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="#android:integer/config_mediumAnimTime"
android:fromYDelta="100%p"
android:toYDelta="0%p" />

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

How can i add animation to the splash screen?

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.

Transition animation within the same activity after button press

I have an activity in which after I press a Button I change the visibility of Group A from visible to gone, and the visibility of Group B from gone to visible.
I would like to simulate the same default animation you get when you go from one activity to another after pressing my button (while I remain in the same activity).
I have been looking online, but I have only been able to find examples of overridePendingTransition, which doesn't apply to my situation.
Try this
final LinearLayout layout = (LinearLayout)findViewById(R.id.groupA);
button.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
Animation slideUp = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slideup);
Animation slideDown = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slidedown);
if(layout.getVisibility()==View.INVISIBLE){
layout.startAnimation(slideUp);
layout.setVisibility(View.VISIBLE);
}
});
slideup.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="0"
android:fromYDelta="500"
android:duration="500"/>
</set>
slidedown.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="0"
android:fromYDelta="0"
android:toYDelta="500"
android:duration="500"/>
</set>

Build an iOS like Transition on an Activity Change

So, I'm trying to build a transition between two Activities in my Android App, that looks similar to the transitions in iOS Apps. And my "Going in Transition" works perfectly fine, the way I want it to work, the new layout slides over the old one, while the old one moves just a bit to the left. But now I'm stuck with the reverse transition, going back to my first layout, cause I want it to look exactly like the first transition, but just reversed, which I'm not able to achieve, cause Android automatically layers the moving in transition on top of the moving out transition, as you can see in the attached gif.
So I would have two ideas, to work around that problem. Is there either a possibility to change the layers of the transition? If not, it would be also possible to animate a moving mask ok my moving in transition. But for neither of these possibilities, I know how to implement them.
My current code:
I just call the animations on the OnCreate of my two activities:
OnCreate of my Main Menu:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
overridePendingTransition(R.anim.slide_in_left,R.anim.slide_out_left);
setContentView(R.layout.activity_main);
}
On Create of my Settings:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
overridePendingTransition(R.anim.slide_in_right,R.anim.slide_out_right);
setContentView(R.layout.activity_settings);
}
And my xml files:
slide_in_left.xml
<?xml version="1.0" encoding="utf-8"?>
<translate
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="-30%p"
android:toXDelta="0"
android:duration="#android:integer/config_longAnimTime" />
slide-out-left.xml
<?xml version="1.0" encoding="utf-8"?>
<translate
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="0"
android:toXDelta="100%p"
android:duration="#android:integer/config_longAnimTime" />
slide_in_right.xml
<?xml version="1.0" encoding="utf-8"?>
<translate
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="100%p"
android:toXDelta="0"
android:duration="#android:integer/config_longAnimTime" />
slide_out_right.xml
<?xml version="1.0" encoding="utf-8"?>
<translate
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="0"
android:toXDelta="-30%p"
android:duration="#android:integer/config_longAnimTime" />
In your MainActivity
private boolean isReverseAnimation = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
isReverseAnimation = false;
overridePendingTransition(R.anim.anim_slide_in_left,R.anim.anim_slide_out_left);
//your other code
}
#Override
protected void onStart() {
super.onStart();
if (isReverseAnimation) {
this.overridePendingTransition(R.anim.anim_slide_in_right,
R.anim.anim_slide_out_right);
}else{
isReverseAnimation = true;
}
}
And Setting activity as it is.
Try and let me know if have any issue.
Happy Coding :)

Categories