Is there any way to reverse this animation? - android

I have a layout like this:
And I animated this buttons in linearlayouts with this code:
public void animation(){
ust.animate().x(0).y(-5000).setDuration(500).start(); //My linear layout in the top of screen.
alt.animate().x(0).y(4000).setDuration(500).start(); //My linear layout in the bottom of screen.
}
I want to reverse this animation.
I tried this way but my linear layout in the bottom of screen, went to top.
Tried This Way:
public void animation(){
ust.animate().x(0).y(0).setDuration(500).start(); //My relative layout in the top of screen.
alt.animate().x(0).y(0).setDuration(500).start(); //My relative layout in the bottom of screen.
}
When I want to try anything like this:
public void animation(){
ust.animate().x(0).y(0).setDuration(500).start(); //My relative layout in the top of screen.
alt.animate().x(0).y(500).setDuration(500).start(); //My relative layout in the bottom of screen.
}
"alt" view dont coming to bottom of the screen in all devices.
And I want to reverse this animation. How can I do this?

Before animation, remember the y locations of your views, then reapply the y locations when reversing:
float ustY;
float altY;
public void animation(){
ustY=ust.getY();
ust.animate().x(0).y(-5000).setDuration(500).start(); //My linear layout in the top of screen.
altY=alt.getY();
alt.animate().x(0).y(4000).setDuration(500).start(); //My linear layout in the bottom of screen.
}
public void reverseAnimation(){
ust.animate().x(0).y(ustY).setDuration(500).start(); //My linear layout in the top of screen.
alt.animate().x(0).y(altY).setDuration(500).start(); //My linear layout in the bottom of screen.
}

that's how you give animations/transitions to layouts, views or widgets
public class MainActivity extends Activity {
Animation RL1, RL2, LR1, LR2, fadein, fadeout;
private View view1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.film1).setVisibility(View.GONE);
// Define all animations
new AnimationUtils();
RL1 = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.slide_right_to_left_1);
RL2 = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.slide_right_to_left_2);
LR1 = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.slide_left_to_right_2);
LR2 = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.slide_left_to_right_1);
fadein = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.fadeout);
fadeout = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.fix);
}
// **//
public void show(View view) {
findViewById(R.id.film1).setVisibility(View.VISIBLE);
view1 = (View) findViewById(R.id.film1);
view1.setAnimation(LR1);
view1.setAnimation(LR2);
}
//
}
create these xml files in the "anim" folder , these contain your animations.
slide_left_to_right_1.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="600"
android:fromXDelta="-100%"
android:toXDelta="0%" >
</translate>
</set>
slide_left_to_right_2.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="600"
android:fromXDelta="0%"
android:toXDelta="100%" >
</translate>
</set>
slide_right_to_left_1.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="600"
android:fromXDelta="100%"
android:toXDelta="0%" >
</translate>
</set>
slide_right_to_left_2.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="600"
android:fromXDelta="0%"
android:toXDelta="-100%" >
</translate>
</set>
fadeout.xml
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="800"
android:fromAlpha="0.0"
android:interpolator="#android:anim/accelerate_interpolator"
android:toAlpha="1.0" />
fix.xml
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="800"
android:fromAlpha="1.0"
android:interpolator="#android:anim/accelerate_interpolator"
android:toAlpha="0.0" />

Assuming that the view originally had no x or y offsets and by reverse you mean restore the view to its original position, call yourView.animate().y(0).setDuration(500).start()

Related

Alpha animation on recyclerview problem - after fades to zero pops-up back

one more problem.
I am trying to make RecycleView items fade away, so I wrote an XML animation for each item, then a layer animation XML and added code in Java main activity. Everything works ok, views disappear, but the problem is, after they all disappear, they all of a sudden appear back again on their own! What can be the reason? How to keep alpha 0?
Codes:
Main activity method:
private void layoutDisappear(final RecyclerView recyclerView) {
if(recyclerView.isAnimating()){
return;
}
final Context context = recyclerView.getContext();
final LayoutAnimationController controller =
AnimationUtils.loadLayoutAnimation(context, R.anim.layout_recycleview_disappear);
recyclerView.setLayoutAnimation(controller);
recyclerView.setLayoutAnimationListener(new Animation.AnimationListener() {
public void onAnimationStart(Animation animation) {}
public void onAnimationRepeat(Animation animation) {}
public void onAnimationEnd(Animation animation) {
}
});
recyclerView.scheduleLayoutAnimation();
Item animation XML:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1400">
<alpha
android:fromAlpha="1"
android:interpolator="#android:anim/decelerate_interpolator"
android:toAlpha="0" />
<scale
android:fromXScale="100%"
android:fromYScale="100%"
android:interpolator="#android:anim/decelerate_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="102%"
android:toYScale="102%" />
</set>
Layer XML:
<?xml version="1.0" encoding="utf-8"?>
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
android:animation="#anim/item_recycleview_disappear"
android:animationOrder="reverse"
android:delay="7%" />
You need add android:fillAfter="true" property to Item animation XML to keep animation changes.
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"
android:duration="1400">
...
</set>

Animation not work properly (performs Only once) in Android

I'm showing issues with animation. Animation works fine when i clicked on button to show linear layout when i clicked buttton to close linear layout animation perform as i want but when i click second time to open linear layout animation doesn't work for both to show layout or close layout. I also want to inform you that button click perform properly. layout visibility GONE & VISIBLE works proper but animation didn't work second time.
public class AdminViewComplaintActivity extends AppCompatActivity implements View.OnClickListener {
ImageView btn_search, btn_close_search ;
LinearLayout ll_search;
Animation animationIn, animationOut;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_complaint);
btn_search = findViewById(R.id.btn_search);
btn_close_search = findViewById(R.id.btn_close_search);
ll_search = findViewById(R.id.ll_search);
animationIn= AnimationUtils.loadAnimation(getApplicationContext(),R.anim.view_in);
animationOut= AnimationUtils.loadAnimation(getApplicationContext(),R.anim.view_out);
}
#Override
public void onClick(View view) {
if (view==btn_search){
if(ll_search.getVisibility()==View.GONE){
// show linear layout with animation
ll_search.setAnimation(animationIn);
ll_search.setVisibility(View.VISIBLE);
}
}
else if(view==btn_close_search){
// close linear layout with animation
ll_search.setAnimation(animationOut);
ll_search.setVisibility(View.GONE);
}
}
}
xml for animation view_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
>
<translate
android:duration="1000"
android:fromXDelta="100%"
android:toXDelta="0%" />
</set>
xml for animation view_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
>
<translate
android:duration="1000"
android:fromXDelta="0%"
android:toXDelta="100%" />
</set>
add this code to view
anim.setFillEnabled(true);
anim.setFillAfter(true);
in programmatically use this :
Animation animation = AnimationUtils.loadAnimation(context, android.R.anim.nameYourAnim);
viewToAnimate.startAnimation(animation);
for example anim :
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="-50%p" android:toXDelta="0"
android:duration="#android:integer/config_mediumAnimTime"/>
<alpha android:fromAlpha="0.0" android:toAlpha="1.0"
android:duration="#android:integer/config_mediumAnimTime" />

Error in sliding up animation

I have my main activity in which on double click i need to slide up my button.
I am able to slide up the button but before it slide up from bottom, for a fraction of second it blinks.
#Override
public boolean onDoubleTap(MotionEvent motionEvent) {
Log.d("year", "onDoubleTap: ");
rl.setVisibility(View.INVISIBLE);
Animation slideUp = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_up);
rl.startAnimation(slideUp);
rl.setVisibility(View.VISIBLE);
i have initialy made it gone
rl = findViewById(R.id.rl_test);
rl.setVisibility(View.GONE);
this is my slide up xml
when i double tap on the screen the button should slideup from the bottom
I this this xml would be useful
Slide up
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="300"
android:fromYDelta="50%p"
android:toYDelta="0%p" />
Slide Down
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="300"
android:fromYDelta="0"
android:toYDelta="100%" />
Good luck

How to hide layout with right to left translation

I want to hide a layout on click button with right to left animation. I have already added animation to same layout while appearing the view. Now I want to hide that view with animation.
//on appearing view
Animation anim = AnimationUtils.loadAnimation(this, R.anim.left_to_right);
mylayout.startAnimation(anim1);
Now I want to hide same layout with rigth to left animation. And then I want to set visibility GONE.
You can make a animation resource and using on your startActivity
Activity
Intent intent = new Intent(this, ActivityB.class);
startActivity(intent);
overridePendingTransition(R.anim.right_left_in, R.anim.right_left_out);
finish();
animation res----> ../anim/right_left_in.xml
<?xml version="1.0" encoding="utf-8"?>
<translate
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="300"
android:fromXDelta="100%p"
android:toXDelta="0%p">
</translate>
animation res----> ../anim/right_left_out.xml
<?xml version="1.0" encoding="utf-8"?>
<translate
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="300"
android:fromXDelta="0%p"
android:toXDelta="100%p">
</translate>
==========================
OTHERS ANIMATION
../anim/move_left_in_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<translate
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="300"
android:fromXDelta="-100%p"
android:toXDelta="0%p">
</translate>
../anim/move_left_out_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<translate
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="300"
android:fromXDelta="0%p"
android:toXDelta="-100%p">
</translate>
../anim/slid_in.xml
<?xml version="1.0" encoding="utf-8"?>
<translate
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="#android:integer/config_longAnimTime"
android:fromXDelta="100%p"
android:toXDelta="0%p">
</translate>
../anim/slid_out.xml
<?xml version="1.0" encoding="utf-8"?>
<translate
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="#android:integer/config_longAnimTime"
android:fromXDelta="0%p"
android:toXDelta="-100%p">
</translate>
../anim/zoom_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" android:fillAfter="true" >
<scale
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fromXScale="1"
android:fromYScale="1"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="3"
android:toYScale="3" >
</scale>
</set>
You can make same XML for animation Right to left and then apply animation same as you did and add animation listener like below, and just set Visibility gone to your view in onAnimationEnd
anim .setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
Create "anim" Android resource directory below res folder :
Then create new anim file called slide_left_out.xml and write this code :
<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:ordering="together"
android:duration="350">
<translate
android:fromXDelta="0"
android:toXDelta="-100%p"
android:fromYDelta="0"
android:toYDelta="50%p"/>
<scale
android:fromXScale="1"
android:toXScale="0.5"
android:fromYScale="1"
android:toYScale="0.5"/>
<alpha
android:fromAlpha="1"
android:toAlpha="0"/>
</set>
Then apply the animation to the view
Animation anim = AnimationUtils.loadAnimation(MainActivity.this, R.anim.slide_left_out);
mylayout.startAnimation(anim);

How to flip the two LinearLayout from right to left and left to right

I have two LinearLayout in my screen.
The first one should be visible and the second should be invisible at the time of activity launching . By pressing a button in first Linearlayout the first LinearLayout should be invisible and the second one should be visible.
But *I want some animation at the time of invisible and invisible state.*like by the time when the 1st is invisible it should animate towards right side of the screen the will invisible and the 2nd one should come from the leftside of the screen by giving animating effect.
Ok this would be the answer for your question
first hide the second LinearLayout in the xml layout file by using the tag android:visibility="invisible"because you dont want it for the first time while activity is launching then create anim folder inside the res folder there create two animation xml files like flip_in_left,flip_in_right in
flip_in_left.xml
<translate
android:duration="500"
android:fromXDelta="100%"
android:toXDelta="0"
/>
then in flip_in_right.xml apply
<translate
android:duration="500"
android:fromXDelta="0"
android:toXDelta="100%"
/>
get the Ids of your two LinearLayout's
LinearLayout layout1 = (LinearLayout) findViewById(R.id.layout1);
LinearLayout layout2 = (LinearLayout) findViewById(R.id.layout2);
By clicking on the Button
inside the onClickListener
layout1.startAnimation(AnimationUtils.loadAnimation(this,flip_in_right));
layout.setVisible(View.GONE);
layout2.setVisible(View.VISIBLE)
layout1.startAnimation(AnimationUtils.loadAnimation(this,flip_in_left));
like this you can do
Try this:
TranslateAnimation animation = new TranslateAnimation(0, -viewWidth, 0, 0); // To animate to the left. To animate right, remove the "-".
animation.setDuration(500);
animation.setAnimationListener(new TranslateAnimation.AnimationListener()
{
#Override
public void onAnimationStart(Animation animation) { }
#Override
public void onAnimationRepeat(Animation animation) { }
#Override
public void onAnimationEnd(Animation animation)
{
myView.setVisibility(View.GONE);
}
});
myView.startAnimation(animation);
create anim folder in res and create xml like slie_out_left.xml
<translate
android:duration="300"
android:fromXDelta="0"
android:fromYDelta="0"
android:toXDelta="100%"
android:toYDelta="0" />
this is another xml file name is silde_in_right.xml
<translate
android:duration="300"
android:fromXDelta="100%"
android:fromYDelta="0%"
android:toXDelta="0%"
android:toYDelta="0%" />
and get the animation in your activity using this
Animation anim1=AnimationUtils.loadAnimation(this,R.anim.slide_out_left);
Animation anim2=AnimationUtils.loadAnimation(this,R.anim.slide_in_right);
apply this animations to respected layouts. and maintain the respective visibility functionalities.
holder.layout.setVisibility(View.VISIBLE);
Animation animation = AnimationUtils.loadAnimation(
_context, R.animator.left_anim);
animation.setDuration(500);
holder.layout.setAnimation(animation);
holder.layout.animate();
If you are not in activity class
here is the left_anim xml
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="500"
android:fromXDelta="100%"
android:toXDelta="0%" >
</translate>

Categories

Resources