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>
Related
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
I want to add animation to my registration page. I have 6 EditTexts and I want them to come one by one from right to left. I'm new to android, someone help me please.
Screenshot registration page
You can create your custom animation xmls and place inside anim folder inside resources folder:
left_swipe.xml
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="500"
android:fromXDelta="100%"
android:toXDelta="0%" >
</translate>
right_swipe.xml
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="500"
android:fromXDelta="-100%"
android:toXDelta="0%" >
</translate>
You can start animation like :
RightAnim = AnimationUtils.loadAnimation(Screen.this, R.anim.right_swipe);
ScreenAnimation.startAnimation(RightAnim );
Plz make sure your animation xml file under res>anim
This is for slide_in_left
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="-50%p" android:toXDelta="0"
android:duration="2500"/>
<alpha android:fromAlpha="0.0" android:toAlpha="1.0"
android:duration="#android:integer/config_mediumAnimTime" />
</set>
and this is for slide_in_right
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="50%p" android:toXDelta="0"
android:duration="2500"/>
<alpha android:fromAlpha="0.0" android:toAlpha="1.0"
android:duration="#android:integer/config_mediumAnimTime" />
</set>
in your java code
Animation a = AnimationUtils.loadAnimation(RegisterActivity.this, R.anim.slide_in_left);
a.reset();
etusername.clearAnimation();
etusername.startAnimation(a);
Animation b = AnimationUtils.loadAnimation(RegisterActivity.this, R.anim.slide_in_right);
b.reset();
etpassword.clearAnimation();
etpassword.startAnimation(b);
left to right animation:
leftToRight.xml
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate android:fromXDelta="-100%" android:toXDelta="0%"
android:fromYDelta="0%" android:toYDelta="0%"
android:duration="700"/>
</set>
right to left animation:
rightToLeft
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:fromXDelta="0%" android:toXDelta="100%"
android:fromYDelta="0%" android:toYDelta="0%"
android:duration="700" />
</set>
write below code in your activity class
private Animation m_animationLeft;
private Animation m_animationRight;
m_animationLeft = AnimationUtils.loadAnimation(this, R.anim.leftToRight);
m_animationRight = AnimationUtils.loadAnimation(this, R.anim.rightToLeft);
editTextOne.startAnimation(m_animationLeft);
m_animationLeft.setAnimationListener(new AnimationListener()
{
#Override
public void onAnimationStart(Animation p_animation)
{
}
#Override
public void onAnimationRepeat(Animation p_animation)
{
}
#Override
public void onAnimationEnd(Animation p_animation)
{
// Start animation on another edittext . If you want to perform animation one by one edittext
}
});
}
}.start();
I would like to do bounce animation of layer.
I have done that layer comes from right to center, now I would like to move it a little back and then back to center. That would create bounce effect.
I was thinking that I can do it with a translate like this:
<translate
android:duration="900"
android:fromXDelta="100%p"
android:toXDelta="0%p" />
<translate
android:duration="900"
android:fromXDelta="0%p"
android:toXDelta="100%p" />
<translate
android:duration="900"
android:fromXDelta="70%p"
android:toXDelta="0%p" />
Well this code does not working, only thing I can achieve is that Layer comes from left to center, and then animation stops.
I cannot use this code: because it does not achieve what I want
setInterpolator(AnimationUtils.loadInterpolator(this,
android.R.anim.bounce_interpolator));
Any help would be appreciated.
You can use the BounceInterpolator to have this effect. The docs contain a very good description how to use it in XML. Just have a animation xml like this:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/bounce_interpolator">
<!-- Use your working translate animation here-->
<translate
android:duration="900"
android:fromXDelta="100%p"
android:toXDelta="0%p" />
</set>
use this xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"
android:interpolator="#android:anim/bounce_interpolator">
<scale
android:duration="600"
android:fromXScale="1.0"
android:fromYScale="0.0"
android:toXScale="1.0"
android:toYScale="1.0" />
</set>
This will show bounce effect with scale ,different from translate,(fits better in some situations),for more check THIS out..
Add code on button or image click
final Animation myAnim = AnimationUtils.loadAnimation(getActivity(), R.anim.bounce);
// Use bounce interpolator with amplitude 0.1 and frequency 15
MyBounceInterpolator interpolator = new MyBounceInterpolator(0.1, 15);
myAnim.setInterpolator(interpolator);
imgVoiceSearch.startAnimation(myAnim);
Add this class
public class MyBounceInterpolator implements android.view.animation.Interpolator {
private double mAmplitude = 1;
private double mFrequency = 10;
public MyBounceInterpolator(double amplitude, double frequency) {
mAmplitude = amplitude;
mFrequency = frequency;
}
public float getInterpolation(float time) {
return (float) (-1 * Math.pow(Math.E, -time / mAmplitude) *
Math.cos(mFrequency * time) + 1);
}
}
First, sorry for my english, i'm vietnamese and my english is bad so please don't be mad ok!
hi everyone, i have to create an app with using animation for layout (pushleft and pushright)
this is my code :
ArrayList<LinearLayout>linear=new ArrayList<LinearLayout>();
Animation animLeft,animRight;
for(int i=0;i<10;i++)
{
LinearLayout llayout=new LinearLayout(getApplicationContext());
LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(0, 0, 0, 0);
llayout.setLayoutParams(lp);
llayout.setId(i);
if(i%2==0)
{
llayout.setAnimation(animLeft);
Log.d("SetAnimation,Left","i:"+i);
llayout.startLayoutAnimation();
}
else
{
llayout.setAnimation(animRight);
Log.d("SetAnimation,Right","i:"+i);
llayout.startLayoutAnimation();
}
linear.add(llayout);
}
//code for animation :
+pushleft
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="100%p" android:toXDelta="0" android:duration="300"/>
<alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="300" />
</set>
+pushright
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="-100%p" android:toXDelta="0" android:duration="300"/>
<alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="300" />
</set>
public void CreateGame(int round)
{
///////////Load all method//////////////
tvarray=TaoTextView(round);
addTexttoLayoutbyRandom(round, tvarray);
for(int i=0;i<round;i++)
{
Linearcenter.addView(linear.get(i));
//linear.get(i).startLayoutAnimation();
}
////////////////////////////////////////
Linearcenter.startLayoutAnimation();
iResult=RandomRound(round);
iRound=round;
//Linearcenter.startLayoutAnimation();
}
I set CreateGame in onCreate(), pleaze ignore some param extraneous (like round,iRound,iResult ..)
when the application start i just see 1 child layout effect and it's a first layout with id = 0
What's wrong??plz tell me why ?
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;