Make View Clickable after Animation in Android - android

After animation I'm not able to click on view. I search alot about this and read answers on stackoverflow and found something that animation only draw raw pixels. To make view clickable you must need to move view. So I write some code but this is not working fine.
Here is my code.
anim.xml
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/linear_interpolator"
android:fillAfter="true">
<translate
android:fromXDelta="0%p"
android:toXDelta="-50%p"
android:duration="500" />
MainActivity
Animation anim= AnimationUtils.loadAnimation(this, R.anim.anim);
anim.setAnimationListener(animLintener);
imageView.startAnimation(anim);
private Animation.AnimationListener animLintener= new Animation.AnimationListener() {
#Override
public void onAnimationEnd(Animation animation) {
int widthHeight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 30, context.getResources().getDisplayMetrics());
int marginBottom = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 36, context.getResources().getDisplayMetrics());
int marginLeft = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 36, context.getResources().getDisplayMetrics());
deactivateParams = new RelativeLayout.LayoutParams(widthHeight, widthHeight);
deactivateParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
deactivateParams.bottomMargin = marginBottom;
deactivateParams.leftMargin = marginLeft;
imageView.setLayoutParams(deactivateParams);
}
}
Problem with this code is that after running onAnimationEnd animation changed from it's position. Why this is happening. I think once animation complete it will move any further, Right ?
Please let me know what I'm missing.

Related

How do i apply fade out animation from left to right in android

I am trying to make a fade out animation to a View, but I don't want the standard one. I want the fade out to start from left to right (or right to left no matter). Any idea on how to do it? I didn't really found anything for android on SO. This is my code for the moment:
val fadeOut = AlphaAnimation(1f, 0f)
fadeOut.interpolator = AccelerateInterpolator()
fadeOut.duration = 3000L
fadeOut.setAnimationListener(object : Animation.AnimationListener {
override fun onAnimationStart(animation: Animation) {
}
override fun onAnimationEnd(animation: Animation) {
mTextView.visibility = View.VISIBLE
}
override fun onAnimationRepeat(animation: Animation) {}
})
mTextView.startAnimation(fadeOut)
From what I can understand, it sounds like you want a reveal animation rather than a fade in/out animation. Your best bet would be to write your own reveal Animation, but if you're ready to settle for a simpler hack, then you could use the circular reveal animation that's available in ViewAnimationUtils. Just start the circular far off to the left/right so that the circular reveal feels like a linear reveal.
final View myView = findViewById(R.id.animatedImageView);
int cx = myView.getMeasuredWidth() * 2;
int cy = myView.getMeasuredHeight() / 2;
int finalRadius = (int) Math.hypot(myView.getWidth()*2, myView.getHeight());
Animator anim;
// for reveal
anim = ViewAnimationUtils.createCircularReveal(myView, cx, cy, 0, finalRadius);
// for hiding
// anim = ViewAnimationUtils.createCircularReveal(myView, cx, cy, finalRadius, 0);
// Do note that you'll need to factor in the 'reveal' time taken in by the extra empty space.
// You could probably get around this by creating your own interpolator,
// but in which case, you might as well just create your own reveal animation.
anim.setDuration(3000);
// make the view visible and start the animation
myView.setVisibility(View.VISIBLE);
anim.start();
Try this:
<?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" />
<translate
android:fromXDelta="100%p" android:toXDelta="0"
android:duration="500" />
</set>

How can i create left to right and then inverse animation in android?

How can I create one animation which always translates from left to right and then if animation stops it turns the other way and translates from right to left. I can create Translate animation in xml and programatically too.
Create this xml, it will translate from left to right and from right to left.
/res/anim/translate.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillEnabled="true"
android:fillAfter="true" >
<translate
android:interpolator="#android:anim/linear_interpolator"
android:fromXDelta="0%p"
android:toXDelta="10%p"
android:duration="2000"
android:startOffset="0" />
<translate
android:interpolator="#android:anim/linear_interpolator"
android:fromXDelta="10%p"
android:toXDelta="-10%p"
android:duration="2000"
android:startOffset="2000" />
</set>
Suppose you want to apply this animation to an image then write the code below in your class file.
ImageView image = (ImageView)findViewById(R.id.imageView1);
Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.translate);
image.startAnimation(animation);
Edit:
If you want your animation to repeat infinitely add the following attributes to the translate tag.
android:repeatCount="infinite"
android:repeatMode="restart"
Depends on your min API level actually. Essentially You should look at ViewPropertyAnimator class.
Let's say you have view to animate, and parent - parent of this view. Your code would look like this:
final float startX = 0; //start position
final float endX = parent.getWidth() - view.getWidth(); //end position - right edge of the parent
API 12+:
view.animate().translationX(endX).setListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationEnd(Animator animation) {
view.animate().translationX(startX).start();
}
}).start();
API 16+:
view.animate().translationX(endX).withEndAction(new Runnable() {
#Override
public void run() {
view.animate().translationX(startX).start();
}
}).start();
Your startX and endX might be different - depending on your needs.
Please note that start() method call is optional.
Also I have not mentioned solution for API <12 since I personally think that nobody should support those legacy APIs :)

Animation Android

I want create a splash screen, So i using animation. Now i want Anim scroll from left to mid ( change % become center_vertical or center_horizontal ).
Sorry ! I speak English is not good.
Code xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate android:fromXDelta="-220%" android:toXDelta="70%"<!--I want change 70% -> center_vertical-->
android:fromYDelta="0%" android:toYDelta="0%"
android:duration="1200"/>
</set>
If you try with android:toXDelta="0%", it will make the animation stop right where the View is placed on the layout by default. For example, if the View has android:layout_centerHorizontal="true" attribute in layout, the animation will stop in the center of the screen.
public void desaparecer(final LinearLayout parent, View hijo, int espaciofinal) {
parent.removeView(hijo);
final LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)parent.getChildAt(0).getLayoutParams();
ValueAnimator animator = ValueAnimator.ofInt(params.height, espaciofinal);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator valueAnimator)
{
params.height= (Integer) valueAnimator.getAnimatedValue();
parent.getChildAt(0).requestLayout();
}
});
animator.setDuration(300);
animator.start();
}
This are my code, and work... well, a little bad.
i'm searching the way to don't start above 0pd, because the idea it's that start respect the original height.
but if like you, you can use it

animation of TextView's text size

I want to be able to do a text animation and change the size of the text in a TextView. I read that there are property animations in android but if someone knows a simple code that can do this for me or an example somewhere I will deeply appreciate it. Thank u in advance!
scale.xml
<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:fromXScale="1.0"
android:fromYScale="1.0"
android:toXScale="2.0"
android:toYScale="2.0"
android:duration="3000"></scale>
</set>
A function into an Activity:
private void RunAnimation()
{
Animation a = AnimationUtils.loadAnimation(this, R.anim.scale);
a.reset();
TextView tv = (TextView) findViewById(R.id.firstTextView);
tv.clearAnimation();
tv.startAnimation(a);
}
extracted and modified from here
Animation animation=new TranslateAnimation(0,480,0,0);
animation.setDuration(5000);
animation.setRepeatMode(Animation.RESTART);
animation.setRepeatCount(Animation.INFINITE);
text.startAnimation(animation);
// applying animation to textview object..
If you are using button event to show animation then put the code inside onClick() otherwise use override method onWindowFocusChanged(boolean hasFocus) to start animation
Use ValueAnimator class in the android
final float startSize = o; // Size in pixels
final float endSize = 30;
final int animationDuration = 1000; // Animation duration in ms
ValueAnimator animator = ValueAnimator.ofFloat(startSize, endSize);
animator.setDuration(animationDuration);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
float animatedValue = (float) valueAnimator.getAnimatedValue();
tv.setTextSize(animatedValue);
}
});
animator.start();
refer this link ValueAnimator
Another solution is that apply scale animation on Textview or its parent layout
ScaleAnimation scaleAnimation = new ScaleAnimation(0.7f, 1.1f, 0.7f, 1.1f, ScaleAnimation.RELATIVE_TO_SELF, 0.5f,
ScaleAnimation.RELATIVE_TO_SELF, 0.5f);
scaleAnimation.setDuration(600);
viewZoom.startAnimation(scaleAnimation);

Android: Updating a LinearLayout position after animation is complete

Any help with the following problem would be greatly appreciated. I know what I need to do and I have checked the Developer docs, but the exact syntax I need to use eludes me (I'm a noob).
Here's what I'm doing. I have a translate.xml file in res/anim that works fine. It looks like this:
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="0%"
android:toXDelta="0%"
android:fromYDelta="0%"
android:toYDelta="10%"
android:repeatCount="0"
android:duration="1000"
android:fillEnabled="true"
android:fillAfter="true"/>
I'm executing my code like this:
l = (LinearLayout) findViewById(R.id.linearLayout1);
a = AnimationUtils.loadAnimation(this, R.anim.translate);
a.setAnimationListener(new AnimationListener() {
public void onAnimationStart(Animation anim){};
public void onAnimationRepeat(Animation anim){};
public void onAnimationEnd(Animation anim){
//l.setLayoutParams(params);
};
});
l.startAnimation(a);
When the animation is done I would like the LinearLayout it animated to move to it's new position (where the animation moved it to). The reason for this is the LinearLayout contains several form elements the user can interact with.
This is a real simple animation for a fairly simple project. It simply moves the element about 30 pixels down the screen. I wouldn't ask for help except I've been struggling with this for several hours now. I know I need to update the LinearLayout's parameters on the end of the animation, but how exactly? I've read of several different ways and they're all a bit confusing.
Thanks in advance.
Instead of creating a new AnimationListener, try implementing the AnimationListener interface to your current (or a new) class. Then just override the onAnimationEnd() method.
Example:
public class ThisClass implements AnimationListener {
private otherMethod() {
l.getAnimation().setAnimationListener(this);
}
public void onAnimationStart(Animation anim){};
public void onAnimationRepeat(Animation anim){};
public void onAnimationEnd(Animation anim){
l.setLayoutParams(params);
};
}
Finally got a way to work around,the right way to do this is setFillAfter(true),
if you want to define your animation in xml then you should do some thing like this
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/decelerate_interpolator"
android:fillAfter="true">
<translate
android:fromXDelta="0%"
android:toXDelta="-100%"
android:duration="1000"/>
</set>
you can see that i have defined filterAfter="true" in the set tag,if you try to define it in translate tag it won't work,might be a bug in the framework!!
and then in the Code
Animation anim = AnimationUtils.loadAnimation(this, R.anim.slide_out);
someView.startAnimation(anim);
OR
TranslateAnimation animation = new TranslateAnimation(-90, 150, 0, 0);
animation.setFillAfter(true);
animation.setDuration(1800);
someView.startAnimation(animation);
then it will surely work!!
Now this is a bit tricky it seems like the view is actually move to the new position but actually the pixels of the view are moved,i.e your view is actually at its initial position but not visible,you can test it if have you some button or clickable view in your view(in my case in layout),to fix that you have to manually move your view/layout to the new position
public TranslateAnimation (float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)
new TranslateAnimation(-90, 150, 0, 0);
now as we can see that our animation will starts from -90 x-axis to 150 x-axis
so what we do is set
someView.setAnimationListener(this);
and in
public void onAnimationEnd(Animation animation)
{
someView.layout(150, 0, someView.getWidth() + 150, someView.getHeight());
}
now let me explain public void layout (int left, int top, int right, int botton)
it moves your layout to new position first argument define the left,which we is 150,because translate animation has animated our view to 150 x-axis, top is 0 because we haven't animated y-axis,now in right we have done someView.getWidth() + 150 basically we get the width of our view and added 150 because we our left is now move to 150 x-axis to make the view width to its originall one, and bottom is equals to the height of our view.
I hope you people now understood the concept of translating, and still you have any questions you can ask right away in comment section,i feel pleasure to help :)
EDIT Don't use layout() method as it can be called by the framework when ever view is invalidated and your changes won't presist, use LayoutParams to set your layout parameters according to your requirement

Categories

Resources