Animate resize of view without resizing button inside view - android

I have a Linear Layout which I want to be animated to resize when I click on it. Once it is resized you should be able to see buttons and text. I've tried animating using this:
container.animate().scaleX(scale);
container.animate().scaleY(scale);
Also I tried this:
container.startAnimation(anim);
where anim is (for making the view bigger):
<scale
android:interpolator="#android:anim/accelerate_decelerate_interpolator"
android:fromXScale="1.0"
android:toXScale="10.0"
android:fromYScale="1.0"
android:toYScale="10.0"
android:pivotX="0%"
android:pivotY="0%"
android:fillAfter="false"
android:duration="700" />
But the problem is that each time I resize the LinearLayout the children inside get resized as well. This should be compatible to AndroidSDK 16 and above. How to achieve this without resizing the children inside?
Thanks

Hello you can override onMeasure() method of the children and make its dimensions as constants for example.

float scale = 1.2f;
ValueAnimator animator = ValueAnimator.ofInt(0,100);
animator.setTarget(container);
animator.setDuration(1000).start();
animator.addUpdateListener(new AnimatorUpdateListener()
{
#Override
public void onAnimationUpdate(ValueAnimator animation)
{
LayoutParams layoutparams = container.getLayoutParams();
layoutparams.height = (int)(layoutparams.height* animation.getAnimatedValue() *(scale -1f));
layoutparams.width= (int)(layoutparams.width* animation.getAnimatedValue() *(scale -1f));
container.setLayoutParams(layoutparams);
}
});

Related

Click on view during animation doesn't react

I have a simple animation:
<rotate android:fromDegrees="0"
android:toDegrees="180"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="4"
android:repeatMode="reverse"
android:duration="1000"
android:interpolator="#android:anim/linear_interpolator" />
<translate
android:duration="2000"
android:fromXDelta="10%p"
android:toXDelta="90%p"
android:repeatCount="1"
android:repeatMode="reverse"
android:fillAfter="true"/>
and I run it on an ImageView. I have an onclick event setup on the ImageView:
imgCorrect1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG, "explode ");
}
});
Here is how I start the animation:
Animation anim = AnimationUtils.loadAnimation(this, R.anim.rottrans);
imgCorrect1.startAnimation(Anim1);
The problem is that while the animation is going, the onclick doesn't get called while the animation is running if I tap on the image while it's animating.
I have tried to search on this issue, but the posts are all to animate an ImageView -when- the onclick is called, not the opposite.
I hope I have been clear here in the description. I have tried to provide all of the relevant code to explain the issue.
You are using Animation API, which animates the matrix of the view. Thus you see a ghost of the original view being animated, whereas the view still is positioned in the original place.
Instead, use Animator API. Here's how it may look like:
ImageView imageView = ...;
ObjectAnimator rotate = ObjectAnimator.ofFloat(imageView, View.ROTATION, 0, 180);
rotate.setRepeatCount(4);
rotate.setRepeatMode(ValueAnimator.REVERSE);
rotate.setDuration(1000);
rotate.setInterpolator(new LinearInterpolator());
ObjectAnimator translate = ObjectAnimator.ofFloat(imageView, View.TRANSLATION_X, 0, 100);
translate.setRepeatCount(1);
translate.setRepeatMode(ValueAnimator.REVERSE);
translate.setDuration(2000);
AnimatorSet set = new AnimatorSet();
// Play either sequentially or together
set.playSequentially(rotate, translate);
// set.playTogether(rotate, translate);
set.start();

Animate image view current position to top - left corner

I have Relative layout, in that layout I have image views which altogether looks like one image. I want animate the 6 image views. 3 are right_top,right_center,right_bottom and another 3 are left-top,left-center,left-bottom. and all these are within its parent layout only...
I want to implement exactly like this image.
Hope THIS may help you.
And if you want to add scaling to tour animation add this scale inside your each animation set
<scale
android:fromYScale="1.0"
android:toYScale="0.5"
android:startOffset="0"
android:duration="1200"
android:fromXScale="1.0"
android:toXScale="0.5"
android:fillAfter="true" />
siddhu,sorry for late.
#Override
public void onClick(View v) {
TranslateAnimation mAnimation1 = new TranslateAnimation(0, mImageViewX.getX(),
mImageView1.getY(), mImageViewX.getY());
mAnimation1.setDuration(500);
mAnimation1.setFillAfter(true);
mImageView1.startAnimation(mAnimation1);
TranslateAnimation mAnimation2 = new TranslateAnimation(0,
- mImageViewX.getX(), mImageView2.getY(), mImageViewX.getY());
mAnimation2.setDuration(500);
mAnimation2.setFillAfter(true);
mImageView2.startAnimation(mAnimation2);
TranslateAnimation mAnimation3 = new TranslateAnimation(0, mImageViewX.getX(),
mImageView3.getX(), -mImageViewX.getY());
mAnimation3.setDuration(500);
mAnimation3.setFillAfter(true);
mImageView3.startAnimation(mAnimation3);
}
The xml is this :
you should learn animation first..it's not hard.
android.view.animation.TranslateAnimation.TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)
```

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

Android -> how to animate to the new position

Here is simple xml android animation:
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="0" android:fromYDelta="0" android:toXDelta="-110"
android:toYDelta="-110" android:duration="1000" android:fillAfter="true" />
I want to move animated object from the center of the screen to 0, 0 positions. How cat I make it (it should work at all screen resolutions)
My Answer:
Thank you guys for your help. But I have fix my problem by other way, dynamically, without xml. Here's full code of this method:
public void replace(int xTo, int yTo, float xScale, float yScale) {
// create set of animations
replaceAnimation = new AnimationSet(false);
// animations should be applied on the finish line
replaceAnimation.setFillAfter(true);
// create scale animation
ScaleAnimation scale = new ScaleAnimation(1.0f, xScale, 1.0f, yScale);
scale.setDuration(1000);
// create translation animation
TranslateAnimation trans = new TranslateAnimation(0, 0,
TranslateAnimation.ABSOLUTE, xTo - getLeft(), 0, 0,
TranslateAnimation.ABSOLUTE, yTo - getTop());
trans.setDuration(1000);
// add new animations to the set
replaceAnimation.addAnimation(scale);
replaceAnimation.addAnimation(trans);
// start our animation
startAnimation(replaceAnimation);
}
My solution:
Thank you guys for your help. But I have fix my problem by other way, dynamically, without xml. Here's full code of this method:
public void replace(int xTo, int yTo, float xScale, float yScale) {
// create set of animations
replaceAnimation = new AnimationSet(false);
// animations should be applied on the finish line
replaceAnimation.setFillAfter(true);
// create scale animation
ScaleAnimation scale = new ScaleAnimation(1.0f, xScale, 1.0f, yScale);
scale.setDuration(1000);
// create translation animation
TranslateAnimation trans = new TranslateAnimation(0, 0,
TranslateAnimation.ABSOLUTE, xTo - getLeft(), 0, 0,
TranslateAnimation.ABSOLUTE, yTo - getTop());
trans.setDuration(1000);
// add new animations to the set
replaceAnimation.addAnimation(scale);
replaceAnimation.addAnimation(trans);
// start our animation
startAnimation(replaceAnimation);
}
First, insert object in container that occupies entire screen. Then modify you animation xml like this
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="50%p" android:fromYDelta="50%p"
android:toXDelta="0%p" android:toYDelta="0%p"
android:duration="1000"
android:fillAfter="true" />
You can also check this android API reference http://developer.android.com/guide/topics/resources/animation-resource.html#translate-element
%p suffix translates element "in percentage relative to the parent size".
If you want it to work on all screens you aren't going to be able to hardcode x,y values. Animations allow you to use percents. This animation would move your view from 0% x and y(relative to itself. In other words where ever it is before the animation it will start from there) It will move to 0%p x and y (which means 0% relative to the parent) This should take you to the top left. Depending on how far into the corner you want it you may try 5%p or 10%p to get some extra margin.
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="0%" android:fromYDelta="0%" android:toXDelta="0%p"
android:toYDelta="0%p" android:duration="1000" android:fillAfter="true" />
In my case I used an ImageView, randomly moved it using setY() and setX() and then animated it on the new position with a Scale animation in xml.
That didn't work. It always started from the initial position and scaled out to the new position.
The way I solved it was to wrap the animated View in a RelativeLayout and then move the RelativeLayout with the ImageView inside it.
PivotX and PivotY always uses the center of the parent view.

Categories

Resources