I want to change image when its flipping Animation android - android

It is possible to make keep changing image until it set final image.
i want to change image while its flipping.
so it look smooth while flipping
private ImageView image;
private void images() {
int[] ps={R.drawable.img1,R.drawable.img2};
Random r = new Random();
int n=r.nextInt(2);
image.setImageResource(ps[n]);
ObjectAnimator animation = ObjectAnimator.ofFloat(coin, "rotationY", 0f, 360f);
animation.setDuration(500);
animation.setInterpolator(new AccelerateDecelerateInterpolator());
animation.start();
}

yes you can do this for that you should use Flip animation, you can find code for this from Here
create XML file in res/animator with help of above link
than in java file
ObjectAnimator anim = (ObjectAnimator)
AnimatorInflater.loadAnimator(mContext, R.animator.[FILE NAME]);
int[] ps={R.drawable.img1,R.drawable.img2};
Random r = new Random();
int n=r.nextInt(2);
image.setImageResource(ps[n]);
anim.setTarget(image);
anim.setDuration(500);
anim.start();

Related

create ball fall animation on given path android

I want to create animation like this image
I create point to point animation and beizier path animation but I am not able to develop animation like this image.
Please help me out.
You can put 2 translate on the same object like this :
AnimationSet set = new AnimationSet(true);
set.addAnimation(translateX);
set.addAnimation(translateY);
image.startAnimation(set);
In your case it's like gravity. So i would suggest to use accelerate interpolator on the translateY animation and a linear Interpolator on translateX.
Here is the code :
TranslateAnimation animationX = new TranslateAnimation(fromX,toX,0,0);
animationX.setInterpolator(new LinearInterpolator());
TranslateAnimation animationY = new TranslateAnimation(0,0,fromY,toY);
animationX.setInterpolator(new AccelerateInterpolator());
AnimationSet set = new AnimationSet(true);
set.addAnimation(animationX);
set.addAnimation(animationY);
image.startAnimation(set);
Set the duration by using:
translationX.setDuration(duration);
Hope this helps.

ValueAnimator.REVERSE create for REVERSE different behavior

I have simple animation that moves the object in Y axis. I want change the behavior for the REVERSE.
//My Code:
ImageView iv = ... //my view
ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "y", 300);
oa.setDuration(100);
oa.setRepeatCount(1);
oa.setRepeatMode(ValueAnimator.REVERSE);
oa.start();
You cannot change the behavior of the reverse mode. Instead, you will need to create an AnimatorSet and play them sequentially.
ImageView iv = ... //my view
ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "y", 300);
oa.setDuration(100);
ObjectAnimator oa2 = ObjectAnimator.ofFloat(/* code here */)
// Add any other code for oa2
AnimatorSet set = new AnimatorSet();
set.playSequentially(oa, oa2);
set.start()

Fade out animation works but opposite fade in animation does not

int imgSize = 30;
final ShapeDrawable redDot = new ShapeDrawable(new OvalShape());
redDot.getPaint().setColor(Color.RED);
redDot.setIntrinsicHeight(imgSize);
redDot.setIntrinsicWidth(imgSize);
final Bitmap bitmap = Bitmap.createBitmap(imgSize, imgSize, Bitmap.Config.ARGB_8888);
final Canvas canvas = new Canvas(bitmap);
redDot.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
redDot.draw(canvas);
ImageView imgAnimArea = new ImageView(getActivity());
imgAnimArea.setImageBitmap(bitmap);
imgAnimArea.setScaleType(ImageView.ScaleType.CENTER);
animationsView.addView(imgAnimArea);
final AnimationSet animSetRedDot = new AnimationSet(true);
// animSetRedDot.setFillAfter(true);
// animSetRedDot.setFillEnabled(true);
Animation aniRepeatFadeIn = null;
Animation aniRepatFadeOut = null;
// fade out
aniRepatFadeOut = new AlphaAnimation(1, 0);
aniRepatFadeOut.setStartOffset(3000);
aniRepatFadeOut.setDuration(300);
animSetRedDot.addAnimation(aniRepatFadeOut);
// fade out animation works only if remove this part
aniRepeatFadeIn = new AlphaAnimation(0, 1);
aniRepeatFadeIn.setStartOffset(6000);
aniRepeatFadeIn.setDuration(300);
animSetRedDot.addAnimation(aniRepeatFadeIn);
imgAnimArea.startAnimation(animSetRedDot);
Its simple code (at least the animation part) but has very strange behavior. Basically before animation it creates a shape (small red circle) converts it to a bitmap and adds it to animationsView(FrameLayout) as ImageView's source (imgAnimArea).
So my red dot fades out but never appears back and it works only in case the fade in part is removed even thou the fade in fires later than fade out. I was trying also to set fade out to .5f instead of 0. In this case it fades out a half of visibility.
Also I had tried to animate animationsView but result is the same - only fade out part works if no fade in part added and if fade in part added then whole animation doesn't work at all.
I could see the shape with no animation added at all. Also I could see it after animation finishes in any case. Enabling or disabling FillAfter has no effect at all.
So the question is whats wrong here? Why fade in animation does not work? Why the whole animation does not work if fade in animation added?
here you have two ways how to do it (see if statement inside onClick() method)
one is preferred one not, the choice is yours
final TextView tv = new TextView(this);
tv.setText("click me");
tv.setTextSize(40);
tv.setTextColor(0xffeeeeee);
tv.setBackgroundColor(0xaa00ff00);
tv.setGravity(Gravity.CENTER);
OnClickListener l = new OnClickListener() {
#Override
public void onClick(View v) {
Animation a;
boolean preferred = true;
if (preferred) {
Log.d(TAG, "onClick using custom Interpolator, preferred way");
// this is a preferred way: custom Interpolator
a = new AlphaAnimation(0, 1);
Interpolator i = new Interpolator() {
#Override
public float getInterpolation(float input) {
return (float) (1 - Math.sin(input * Math.PI));
}
};
a.setInterpolator(i);
} else {
Log.d(TAG, "onClick using AnimationSet, NOT preferred way");
AnimationSet set = new AnimationSet(true);
AlphaAnimation alpha0 = new AlphaAnimation(1, 0);
alpha0.setDuration(1000);
alpha0.setFillEnabled(true);
alpha0.setFillBefore(false);
alpha0.setFillAfter(false);
set.addAnimation(alpha0);
AlphaAnimation alpha1 = new AlphaAnimation(0, 1);
alpha1.setDuration(1000);
alpha1.setFillEnabled(true);
alpha1.setFillBefore(false);
alpha1.setFillAfter(false);
alpha1.setStartOffset(1000);
set.addAnimation(alpha1);
a = set;
}
a.setDuration(2000);
tv.startAnimation(a);
}
};
tv.setOnClickListener(l);
setContentView(tv);

Image disappears in animation

I am creating an animation where my image will translate to a different spot on the screen and then fade in gradually. I completed the translation part(see below) but now when I start the fade in animation it disappears for the duration and then reapears after. I want to show the image being faded in gradually....Any ideas why this is happening?
public static int moveTwo(AnimationListener activity, View apa, int animationmove)
Log.v("MOVETWO", "Started move2");
AnimationSet picMov2 = new AnimationSet(true);
picMov2.setAnimationListener(activity);
RotateAnimation rotate2 = new RotateAnimation(0, 0,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
// rotate1.setStartOffset(50);
rotate2.setDuration(2000);
picMov2.addAnimation(rotate2);
TranslateAnimation trans2 = new TranslateAnimation(-200, -400, 0, 0);
trans2.setDuration(2000);
picMov2.setFillAfter(true);
picMov2.addAnimation(trans2);
apa.startAnimation(picMov2);
animationmove = 3;
return animationmove;
public static int moveThree(AnimationListener activity, View apa, int animationmove)
AlphaAnimation fadein = new AlphaAnimation((float) 0.3, 1);//HERE THE IMAGE IS DISAPPEARING
fadein.setAnimationListener(activity);
fadein.setDuration(2000);
fadein.setFillAfter(true);
apa.startAnimation(fadein);
animationmove=4;
return animationmove;
Just needed to create a translate action in the same place so it doesn't go back to its original position
public static int moveThree(AnimationListener activity, View apa, int animationmove)
{
Log.v("MOVETHREE", "Started move3");
AnimationSet picMov3 = new AnimationSet(true);
picMov3.setAnimationListener(activity);
AlphaAnimation fadein = new AlphaAnimation((float) 0.4, 1);
// rotate1.setStartOffset(50);
fadein.setDuration(duration);
picMov3.addAnimation(fadein);
TranslateAnimation trans1 = new TranslateAnimation(-400, -400, 0, 0);
trans1.setDuration(duration);
picMov3.setFillAfter(true);
picMov3.addAnimation(trans1);
apa.startAnimation(picMov3);

Animation with animationSet() in android

OK here's the problem
i have an ImageView in my activity, here's what it looks in main.xml:
<ImageView
android:id="#+id/ic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon"
android:layout_gravity="center_horizontal"/>
I want this image to move -200(left) and then to 100(right) and then back to 0 with bouncing effect.
I've implement this with my code:
as = new AnimationSet(true);
as.setFillEnabled(true);
as.setInterpolator(new BounceInterpolator());
TranslateAnimation ta = new TranslateAnimation(-300, 100, 0, 0);
ta.setDuration(2000);
as.addAnimation(ta);
AnimationSet sa = new AnimationSet(true);
sa.setFillEnabled(true);
sa.setInterpolator(new DecelerateInterpolator());
TranslateAnimation ta2 = new TranslateAnimation(100, 0, 0, 0);
ta2.setDuration(2000);
sa.addAnimation(ta2);
as.addAnimation(sa);
you can see at the code the X transition that i want (-300,100) then (100, 0)
however, the image doesn't move like it should, instead it just stop at 100 and then bouncing...
hmmm...., do you guys know what is wrong or what should i do to accomplish this?
If I'm not mistaking, you're shooting for a sequence of animations.
Interestingly, once you start an AnimationSet, all the animations added are ran simultaneously and not sequentially; therefore you need to setStartOffset(long offSet) for each animation that follows the first animation.
Maybe something like this will work...
as = new AnimationSet(true);
as.setFillEnabled(true);
as.setInterpolator(new BounceInterpolator());
TranslateAnimation ta = new TranslateAnimation(-300, 100, 0, 0);
ta.setDuration(2000);
as.addAnimation(ta);
TranslateAnimation ta2 = new TranslateAnimation(100, 0, 0, 0);
ta2.setDuration(2000);
ta2.setStartOffset(2000); // allowing 2000 milliseconds for ta to finish
as.addAnimation(ta2);
I suggest you to use ObjectAnimator. It is very easy to implement your case. Your animation may look like this:
ObjectAnimator animator1 = ObjectAnimator.ofFloat(targetView, "translationX", -200f);
animator1.setRepeatCount(0);
animator1.setDuration(1000);
ObjectAnimator animator2 = ObjectAnimator.ofFloat(targetView, "translationX", 100f);
animator2.setRepeatCount(0);
animator2.setDuration(1000);
ObjectAnimator animator3 = ObjectAnimator.ofFloat(targetView, "translationX", 0f);
animator3.setRepeatCount(0);
animator3.setDuration(1000);
//sequencial animation
AnimatorSet set = new AnimatorSet();
set.play(animator1).before(animator2);
set.play(animator2).before(animator3);
set.start();
If you are not familior with ObjectAnimator, you can check this android example tutorial:
Android View Animation Example
Something like this is very easy in 3.0 and above. Here are two links that I used to accomplish something similar.
http://android-developers.blogspot.com/2011/02/animation-in-honeycomb.html
http://developer.android.com/reference/android/animation/AnimatorSet.Builder.html

Categories

Resources