Error in sliding up animation - android

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

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" />

Is there any way to reverse this animation?

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()

Apply two alternate rotations to a View in Android

I'd like to apply 2 alternate rotation animations to a View. Each rotation should start after the click on the same View.
The result I'm looking for is:
the user clicks on the View and it rotates from 0° to 135°
the user clicks again on the View and it rotates from 135° to 0° (to the initial state)
The problem is that, when the user performs the second click, the Button resets to the initial aspect before starting the animation correctly.
I'm targeting Android APIs<11 so I'm using the startAnimation() method. The animations are applied to a Button view defined as follows:
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
The animations are the following
rotate_cw.xml (0° to 135° rotation):
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true">
<rotate
android:duration="300"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="135" />
</set>
rotate_ccw.xml (135° to 0° rotation):
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
android:duration="300"
android:fromDegrees="135"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="0"/>
</set>
The animations are then applied in this way, where flag is a boolean global variable:
Button b = (Button) findViewById(R.id.button);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (flag) {
Animation a = AnimationUtils.loadAnimation(this, R.anim.rotate_cw);
b.startAnimation(a);
} else {
Animation a = AnimationUtils.loadAnimation(this, R.anim.rotate_ccw);
b.startAnimation(a);
}
flag = !flag;
}
});
Am I missing anything?
Thanks in advance for your help.

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