Scaling animation on different screen size - android

Hey I have the following problem whan I animate my floating buttons - namely they are scaling badly. First of all, fab buttons on the second Image are higher than they should. Event main floating button with "+" is not rotating. I anyone know how to deal with it please answer me.
Image left HTC One MAX, screen HD - SKD21 lolipop, emulator 768x1280 SKD 17 jelly bean
my code in Activity.PhotoGallery
private void showFabs(){
Animation show_fab_photo = AnimationUtils.loadAnimation(getApplication(), R.anim.fab_add_photo_show);
FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) fabAddPhoto.getLayoutParams();
layoutParams.bottomMargin += (int) (fabAddPhoto.getHeight() * 1.7);
fabAddPhoto.setLayoutParams(layoutParams);
fabAddPhoto.startAnimation(show_fab_photo);
fabAddPhoto.setClickable(true);
Animation show_fab_gallery = AnimationUtils.loadAnimation(getApplication(), R.anim.fab_add_from_gallery_show);
FrameLayout.LayoutParams layoutParamsGallery = (FrameLayout.LayoutParams) fabAddFromGallery.getLayoutParams();
layoutParamsGallery.bottomMargin += (int) (fabAddFromGallery.getHeight() * 3.4);
fabAddFromGallery.setLayoutParams(layoutParamsGallery);
fabAddFromGallery.startAnimation(show_fab_gallery);
fabAddFromGallery.setClickable(true);
Animation rotateFabAdd = AnimationUtils.loadAnimation(getApplication(), R.anim.fab_gallery_show);
fabAdd.startAnimation(rotateFabAdd);
}
private void hideFabs(){
Animation hide_fab_photo = AnimationUtils.loadAnimation(getApplication(), R.anim.fab_add_photo_hide);
FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) fabAddPhoto.getLayoutParams();
layoutParams.bottomMargin -= (int) (fabAddPhoto.getHeight() * 1.7);
fabAddPhoto.setLayoutParams(layoutParams);
fabAddPhoto.startAnimation(hide_fab_photo);
fabAddPhoto.setClickable(false);
Animation hide_fab_gallery = AnimationUtils.loadAnimation(getApplication(), R.anim.fab_add_from_gallery_hide);
FrameLayout.LayoutParams layoutParamsGallery = (FrameLayout.LayoutParams) fabAddFromGallery.getLayoutParams();
layoutParamsGallery.bottomMargin -= (int) (fabAddFromGallery.getHeight() * 3.4);
fabAddFromGallery.setLayoutParams(layoutParamsGallery);
fabAddFromGallery.startAnimation(hide_fab_gallery);
fabAddFromGallery.setClickable(false);
Animation rotateFabAdd = AnimationUtils.loadAnimation(getApplication(), R.anim.fab_gallery_hide);
fabAdd.startAnimation(rotateFabAdd);
}
And my animations XML files
Fab with camera
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true">
<!-- Rotate -->
<rotate
android:duration="500"
android:fromDegrees="30"
android:interpolator="#android:anim/linear_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="0"
android:repeatMode="reverse"
android:toDegrees="0"></rotate>
<!--Move-->
<translate
android:duration="800"
android:fromXDelta="0%"
android:fromYDelta="170%"
android:interpolator="#android:anim/linear_interpolator"
android:toXDelta="0%"
android:toYDelta="0%"></translate>
<!--Fade In-->
<alpha
android:duration="1500"
android:fromAlpha="0.0"
android:interpolator="#android:anim/decelerate_interpolator"
android:toAlpha="1.0"></alpha>
</set>
Fab with image
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true">
<!-- Rotate -->
<rotate
android:duration="500"
android:fromDegrees="30"
android:interpolator="#android:anim/linear_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="0"
android:repeatMode="reverse"
android:toDegrees="0"></rotate>
<!--Move-->
<translate
android:duration="800"
android:fromXDelta="0%"
android:fromYDelta="340%"
android:interpolator="#android:anim/linear_interpolator"
android:toXDelta="0%"
android:toYDelta="0%"></translate>
<!--Fade In-->
<alpha
android:duration="1500"
android:fromAlpha="0.0"
android:interpolator="#android:anim/decelerate_interpolator"
android:toAlpha="1.0"></alpha>
</set>

Related

How to add a sequential animation on an imageview to scale up and scale down repeatedly in android

I am trying to do an animation on an imageview, were i need to scale up and scale down the image so that it will have a shadow feel effect for another animation.
I tried adding two xml in anim folder -
scale_up.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/linear_interpolator"
android:shareInterpolator="false">
<scale
android:duration="1250"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="infinite"
android:repeatMode="restart"
android:toXScale="1.5"
android:toYScale="1.5" />
</set>
scale_down.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/linear_interpolator"
android:shareInterpolator="false">
<scale
android:duration="1250"
android:fromXScale="1.5"
android:fromYScale="1.5"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="infinite"
android:repeatMode="restart"
android:toXScale="1.0"
android:toYScale="1.0" />
</set>
and in my activity class
AnimationSet s = new AnimationSet(false);
View shadowView = view.findViewById(R.id.shadowView);
final Animation scaleDownAnimation = AnimationUtils.loadAnimation(context, R.anim.scale_down);
final Animation scaleUpAnimation = AnimationUtils.loadAnimation(context, R.anim.scale_up);
s.addAnimation(scaleUpAnimation);
s.addAnimation(scaleDownAnimation);
shadowView.startAnimation(s);
But animation is not happening. What i am doing wrong? Anyone please help. Thanks
AnimationSet performs animation simultanuously, not sequentaly by default. So it does zoom in and zoom out at the same time. That's why you see no effect. But you can set a start offset to the animation:
AnimationSet s = new AnimationSet(false);
View shadowView = view.findViewById(R.id.shadowView);
final Animation scaleDownAnimation = AnimationUtils.loadAnimation(context, R.anim.scale_down);
final Animation scaleUpAnimation = AnimationUtils.loadAnimation(context, R.anim.scale_up);
scaleDownAnimation.setStartOffset(1250); //this line
s.addAnimation(scaleUpAnimation);
s.addAnimation(scaleDownAnimation);
shadowView.startAnimation(s

Animation scale to fit screen width

I need to have an animation that scales a view to the screen width over 1500 seconds.
I haven't got a clue how to achieve this. I searched for an hour now on the internet...
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillEnabled="true"
android:fillAfter="true">
<scale
android:interpolator="#android:anim/accelerate_decelerate_interpolator"
android:fromXScale="0"
android:toXScale="?"
android:fromYScale="1.0"
android:toYScale="1.0"
android:fillAfter="false"
/>
</set>
The method where the animation gets called:
tab1.Click += delegate
{
ResetMenuItem();
animation = AnimationUtils.LoadAnimation(ApplicationContext, Resource.Animation.grow_anim1);
animation.Duration = duration;
// int width = Resources.DisplayMetrics.WidthPixels;
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams((int)ConvertDpToPix(3f),RelativeLayout.LayoutParams.MatchParent);
selector1.LayoutParameters = p;
selector1.StartAnimation(animation);
animation.AnimationEnd+=delegate
{
drawer.CloseDrawers();
};
tabIndex=1;
};
I ended up solving this by using a thread to edit the width of the view.

AnimationSet defined via XML does not repeat

I have this xml code, that should simulate a sort of "heart beat" animation. Image that use it should scale twice, and return to original size, then restart again:
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:repeatCount="-1"
android:repeatMode="restart"
android:shareInterpolator="true" >
<scale
android:duration="500"
android:fromXScale="1"
android:fromYScale="1"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="1.2"
android:toYScale="1.2" />
<scale
android:duration="500"
android:fromXScale="1.2"
android:fromYScale="1.2"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="1.4"
android:toYScale="1.4" />
<scale
android:duration="500"
android:fromXScale="1.4"
android:fromYScale="1.4"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="1.0"
android:toYScale="1.0" />
</set>
and this is how i add this animation set to my imageview:
AnimationSet heart_pulse = new AnimationSet(true);
heart_pulse.addAnimation(AnimationUtils.loadAnimation(activity,
R.anim.pulsexml));
logo.setAnimation(heart_pulse);
logo.startAnimation(heart_pulse);
but animation is executed only one time, then it stops. Why?
try this custom Interpolator:
final ImageView iv = new ImageView(this);
iv.setImageResource(R.drawable.heart);
setContentView(iv);
Runnable action = new Runnable() {
#Override
public void run() {
Interpolator i = new Interpolator() {
#Override
public float getInterpolation(float input) {
float x = input < 1/3f? 2 * input : (1 + input) / 2;
return (float) Math.sin(x * Math.PI);
}
};
ScaleAnimation anim = new ScaleAnimation(1, 1.2f, 1, 1.2f, iv.getWidth() / 2, iv.getHeight() / 2);
anim.setRepeatCount(Animation.INFINITE);
anim.setDuration(750);
anim.setInterpolator(i);
iv.startAnimation(anim);
}
};
iv.post(action);
Have you tried adding android:fillAfter="false"?
EDIT:
According to the documentation sintax of Animation, these properties must be set to an Animatior and not to the set, so let's have then this custom animation repeating with no code:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:duration="500"
android:fromXScale="1"
android:fromYScale="1"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="-1"
android:repeatMode="restart"
android:toXScale="1.2"
android:toYScale="1.2" />
<scale
android:duration="500"
android:fromXScale="1.2"
android:fromYScale="1.2"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="-1"
android:repeatMode="restart"
android:toXScale="1.4"
android:toYScale="1.4" />
<scale
android:duration="500"
android:fromXScale="1.4"
android:fromYScale="1.4"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="-1"
android:repeatMode="restart"
android:toXScale="1.0"
android:toYScale="1.0" />
</set>
Consider using a CycleInterpolator for your animation, e.g.:
final Animation animation = AnimationUtils.loadAnimation(context, R.anim.pulsexml);
animation.setInterpolator(new CycleInterpolator(3f));
logo.startAnimation(animation);

Animation using startOffset goes reversed

I created the following animation:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/anticipate_overshoot_interpolator"
>
<translate
android:fromXDelta="0"
android:toXDelta="100%p"
android:duration="500"
/>
<translate
android:fromXDelta="-100%p"
android:toXDelta="0"
android:startOffset="500"
android:duration="500"/>
</set>
I test it on an Android 2.3.6 phone and the animation goes sequentially but REVERSED.
First it goes from left to the middle then from the middle to the right. How can I play it in the correct order?
final ImageView iv = new ImageView(this);
iv.setScaleType(ScaleType.CENTER);
final Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.layer0);
iv.setImageBitmap(b);
OnClickListener l = new OnClickListener() {
#Override
public void onClick(View v) {
float x = (iv.getWidth() + b.getWidth()) / 2;
AnimationSet set = new AnimationSet(true);
set.setFillBefore(false);
Animation a;
a = new TranslateAnimation(0, x, 0, 0);
a.setDuration(500);
a.setFillAfter(false);
a.setFillBefore(false);
a.setFillEnabled(true);
set.addAnimation(a);
a = new TranslateAnimation(-x, 0, 0, 0);
a.setStartOffset(500);
a.setDuration(500);
a.setFillAfter(false);
a.setFillBefore(false);
a.setFillEnabled(true);
set.addAnimation(a);
iv.startAnimation(set);
}
};
iv.setOnClickListener(l);
setContentView(iv);
As I couldn't modify their orders, I eventually created two separate animations and used an AnimationListener to start the second one.
you can do it simply adding a set attribute element android:repeatMode="reverse". Thus your code should be..
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/anticipate_overshoot_interpolator"
android:repeatMode="reverse"
>
<translate
android:repeatCount="infinite"
android:fromXDelta="0"
android:toXDelta="100%p"
android:duration="2500"
/>
<!-- <translate
android:fromXDelta="-100%p"
android:toXDelta="0"
android:startOffset="500"
android:duration="500"/> -->
</set>

Fading in text in Android using AnimationUtils.loadAnimation

I would be very grateful if someone could explain to me why this works:
private void startAnimating() {
TextView logo1 = (TextView) findViewById(R.id.Shizzle);
final Animation fade1 = new AlphaAnimation(0.0f, 1.0f);
fade1.setDuration(3000);
logo1.startAnimation(fade1);
}
But this doesn't work at all for me:
private void startAnimating() {
TextView logo1 = (TextView) findViewById(R.id.Shizzle);
Animation fade1 = AnimationUtils.loadAnimation(this,R.anim.fade_in);
logo1.startAnimation(fade1);
}
The fade_in.xml associated with the above is:
<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/android"
android:shareInterpolator="false">
<alpha
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration="3000">
</alpha>
Thanks for your help!
Works for me:
Create two file in folder /res/anim - fadein.xml, fadeout.xml
fadein:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false" >
<alpha
android:duration="500"
android:fromAlpha="0.0"
android:toAlpha="1.0" >
</alpha>
</set>
fadeout:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false" >
<alpha
android:duration="200"
android:fromAlpha="1.0"
android:toAlpha="0.0" >
</alpha>
</set>
initialize code:
Animation animFadeIn, animFadeOut;
...
animFadeIn=AnimationUtils.loadAnimation(this, R.anim.fadein);
animFadeOut=AnimationUtils.loadAnimation(this, R.anim.fadeout);
...
using:
case R.id.imgBtnShowContent:
rlOrderBtns.startAnimation(animFadeIn);
rlOrderBtns.setVisibility(View.VISIBLE);
break;
case R.id.imgBtnHideContent:
rlOrderBtns.startAnimation(animFadeOut);
rlOrderBtns.setVisibility(View.INVISIBLE);
break;

Categories

Resources