I would like to rotate an imageView 10 times, every times, the imageView will load randomly an image in drawable resources. For example: there are 6 images like img1 to img6. I did the code like this, but it doesn't work
public void clockwise(View view){
for (int i=1; i<=6; i++){
ImageView image = (ImageView)findViewById(R.id.imageView);
Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.clockwise);
animation.setRepeatCount(10);
int max=6, min=1;
int randNum = min + (int)(Math.random()*((max-min)+1));//
if (randNum==1) image.setImageDrawable(getResources().getDrawable(R.drawable.die1));
else if (randNum==2) image.setImageDrawable(getResources().getDrawable(R.drawable.die2));
else if (randNum==3) image.setImageDrawable(getResources().getDrawable(R.drawable.die3));
else if (randNum==4) image.setImageDrawable(getResources().getDrawable(R.drawable.die4));
else if (randNum==5) image.setImageDrawable(getResources().getDrawable(R.drawable.die5));
else if (randNum==6) image.setImageDrawable(getResources().getDrawable(R.drawable.die6));
image.startAnimation(animation);
}
It just load only one image that I set in xml file for 10 times of repeating
you can use animation listener like below
animation.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});)
So in onAnimationRepeat method you can change your image.
instead of loop use Animation Listener.
ImageView image = (ImageView)findViewById(R.id.imageView);
Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.clockwise);
animation.setRepeatCount(10);
image.startAnimation(animation);
Overrided methods.
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
int max=6, min=1;
int randNum = min + (int)(Math.random()*((max-min)+1));//
if (randNum==1) image.setImageDrawable(getResources().getDrawable(R.drawable.die1));
else if (randNum==2) image.setImageDrawable(getResources().getDrawable(R.drawable.die2));
else if (randNum==3) image.setImageDrawable(getResources().getDrawable(R.drawable.die3));
else if (randNum==4) image.setImageDrawable(getResources().getDrawable(R.drawable.die4));
else if (randNum==5) image.setImageDrawable(getResources().getDrawable(R.drawable.die5));
else if (randNum==6) image.setImageDrawable(getResources().getDrawable(R.drawable.die6));
}
Thanks KDeogharkar and Ragesh for your suggestion.
I've tried to used your way that is changing image onAnimationRepeat(), but it still doesn't work. I guess that is caused because the image is still being used by the animation, so it doesn't allow to change the image. Therefore, I write a recursive function that waits for the end of the animation to change image, and call the same animation, so finally it works. Here is my code
public void rotate(int count){
//
final int nextCount = count - 1;
ImageView image = (ImageView)findViewById(R.id.imageView);
int max = 6, min = 1;
int randNum = min + (int) (Math.random() * ((max - min) + 1));//
if (randNum == 1)
image.setImageDrawable(getResources().getDrawable(R.drawable.die1));
else if (randNum == 2)
image.setImageDrawable(getResources().getDrawable(R.drawable.die2));
else if (randNum == 3)
image.setImageDrawable(getResources().getDrawable(R.drawable.die3));
else if (randNum == 4)
image.setImageDrawable(getResources().getDrawable(R.drawable.die4));
else if (randNum == 5)
image.setImageDrawable(getResources().getDrawable(R.drawable.die5));
else if (randNum == 6)
image.setImageDrawable(getResources().getDrawable(R.drawable.die6));
Animation animation;
animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.clockwise);
animation.setRepeatCount(1);
animation.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
if (nextCount >=0) rotate(nextCount);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
image.startAnimation(animation);
}
After that, I just need to call rotate(animationTimes) in main() function.
Related
I have repeatedly defined animation in this code. Is not there a simpler way to do this? the code works the way I want it, but I want to simplify my encoding.can you help me. How should I follow a method. How to add wait time?
I made a code that I could not do in this title but it came to be written much shorter
resimyukle();
hediye=getDrawable(R.drawable.hediye);
final ImageView[] imajlar={img1,img2,img3,img4,img5,img6,img7,img8};
Animation fadein=AnimationUtils.loadAnimation(getApplicationContext(),R.anim.fadein);
imajlar[0].setBackground(hediye);
imajlar[0].startAnimation(fadein);
Animation res=AnimationUtils.loadAnimation(getApplicationContext(),R.anim.fadein);
Animation res1=AnimationUtils.loadAnimation(getApplicationContext(),R.anim.fadein);
Animation res2=AnimationUtils.loadAnimation(getApplicationContext(),R.anim.fadein);
Animation res3=AnimationUtils.loadAnimation(getApplicationContext(),R.anim.fadein);
Animation res4=AnimationUtils.loadAnimation(getApplicationContext(),R.anim.fadein);
Animation res5=AnimationUtils.loadAnimation(getApplicationContext(),R.anim.fadein);
Animation res6=AnimationUtils.loadAnimation(getApplicationContext(),R.anim.fadein);
anima(fadein,res,imajlar[1]);
anima(res,res1,imajlar[2]);
anima(res1,res2,imajlar[3]);
anima(res2,res3,imajlar[4]);
anima(res3,res4,imajlar[5]);
anima(res4,res5,imajlar[6]);
anima(res5,res6,imajlar[7]);
}
public void anima(final Animation aa, final Animation bb, final ImageView img)
{
aa.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
animation.cancel();
img.setBackground(hediye);
img.startAnimation(bb);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
}
hediye=getDrawable(R.drawable.hediye);
final ImageView[] imajlar={img1,img2,img3,img4,img5,img6,img7,img8};
Animation res=AnimationUtils.loadAnimation(getApplicationContext(),R.anim.fadein);
for (int i = 0; i < imajlar.size; i++) {
anima(res, imajlar[i], i);
}
And your method:
public void anima(Animation aa, ImageView img, int id)
{
aa.setStartOffset(id * 500);
aa.setDuration(1000);
img.startAnimation(aa);
img.setBackground(hediye);
}
I'm trying to run a for loop with a translateAnimation() of an imageview in it. For every iteration, the imageview has to move i*20 times in front(in a curve if possible). But the for loop executes fully and doesn't wait for each animation. So the animation executes just once(the last ith time) and not i times. Please help me!!!
`
for(int i=m;i<=y;i++) {
int w=20*i
TranslateAnimation translate = new TranslateAnimation(0,w,0,0);
myAnimation1.setVisibility(View.VISIBLE);
translate.setDuration(1000);
translate.setFillAfter(false);
myAnimation1.startAnimation(translate);
myAnimation1.setVisibility(View.INVISIBLE);
}'
Have you tried adding a animation listener?
http://developer.android.com/reference/android/animation/Animator.AnimatorListener.html
Something like
public void startNextAnimation(int count) {
if (count != 0)
//setup next animation
else
//done
myAnimation1.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
startNextAnimation(countdown);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
I can't get the circular reveal animation to work.
I think I checked the most obvious things:
It starts, width and height are > 0 and it is visible, no Exception..
I load some data from the internet and display it in the view(fab)
The animation should only play after the download finishes.
TmdbHelper helper = new TmdbHelper();
helper.getMovieById(id, "en", new TmdbHelper.ResultListener() {
#Override
public void onResultReceived(JSONObject result) {
// called when finished downloading
try {
String rating = result.getString("vote_average");
AnimationHelper.circularReveal(fab, 500, 0);
fab.setText(rating);
} catch (JSONException e) {
e.printStackTrace();
}
}
});
AnimationHelper:
public static void circularReveal(final View view, final long duration, long startDelay) {
// get the center for the clipping circle
int cx = (view.getLeft() + view.getRight()) / 2;
int cy = (view.getTop() + view.getBottom()) / 2;
// get the final radius for the clipping circle
int finalRadius = Math.max(view.getWidth(), view.getHeight());
// create the animator for this view (the start radius is zero)
Animator anim =
ViewAnimationUtils.createCircularReveal(view, cx, cy, 0, finalRadius);
anim.setDuration(duration);
anim.setStartDelay(startDelay);
anim.addListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animation) {
view.setVisibility(View.VISIBLE);
}
#Override
public void onAnimationEnd(Animator animation) {
}
#Override
public void onAnimationCancel(Animator animation) {
}
#Override
public void onAnimationRepeat(Animator animation) {}
});
// make the view visible and start the animation
anim.start();
}
I use the circular reveal animation in other parts like this to make sure the view is attached, and it works:
headerContainer.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
headerContainer.getViewTreeObserver().removeOnGlobalLayoutListener(this);
AnimationHelper.circularReveal(headerContainer, 500, 200);
}
});
Perhaps you should erase this line inside your onResultRecieved():
fab.setVisibility(View.VISIBLE);
My assumption is that your circular reveal method is working just fine. It is because of you have made the FAB visible before the animation even begin, you can't see it in action.
As an addition, those lines you've shown which is working doesn't have fab.setVisibility(View.VISIBLE) called anywhere in it.
1st Approach:
Try Transition Listener.
getWindow().getSharedElementExitTransition().addListener(new Transition.TransitionListener() {
#Override
public void onTransitionStart(Transition transition) {
}
#Override
public void onTransitionEnd(Transition transition) {
}
#Override
public void onTransitionCancel(Transition transition) {
}
#Override
public void onTransitionPause(Transition transition) {
}
#Override
public void onTransitionResume(Transition transition) {
}
});
2nd Approach: Try setting start delay and listener to the reveal animation and when animation starts then set the view visible
if (Build.VERSION.SDK_INT >= 21) {
Animator anim = ViewAnimationUtils.createCircularReveal(viewRoot, cx, cy, 0, finalRadius);
anim.setStartDelay(300);
anim.setDuration(1000);
anim.setInterpolator(new DecelerateInterpolator());
anim.addListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animation) {
viewRoot.setVisibility(View.VISIBLE);
}
#Override
public void onAnimationEnd(Animator animation) {
}
#Override
public void onAnimationCancel(Animator animation) {
}
#Override
public void onAnimationRepeat(Animator animation) {
}
});
anim.start();
}
}
#Override
public void onAnimationCancel(Animator animation) {
}
#Override
public void onAnimationRepeat(Animator animation) {
}
});
anim.start();
im trying to rotate an imageview in certain degrees and at the same time expand this imageview's height !
my code so far is :
public class TongueHandler {
ImageView tongue;
RelativeLayout.LayoutParams tongue_params;
ObjectAnimator rotate_tongue;
ScaleAnimation scale_tongue;
AnimationSet as;
ImageView frog;
Button btn;
public TongueHandler(Button btn,ImageView frog,ImageView tongue){
this.btn=btn;
this.frog=frog;
this.tongue=tongue;
}
public void Initialize()
{
tongue.setBackgroundColor(Color.RED);
tongue_params=new RelativeLayout.LayoutParams(10,100);
tongue_params.leftMargin=frog.getLeft()+frog.getWidth()/2;
tongue_params.topMargin=frog.getTop()+frog.getHeight()/2;
tongue.setLayoutParams(tongue_params);
Animate();
}
public void Animate()
{
as=new AnimationSet(true);
rotate_tongue=ObjectAnimator.ofFloat(tongue, "rotationX", 90.0f, 90+CalcAngle(frog,btn));
rotate_tongue.setDuration(1);
rotate_tongue.start();
System.out.println("This is Angle: "+CalcAngle(frog,btn));
/* scale_tongue=new ScaleAnimation(1,1,30,btn.getY(),1,2);
scale_tongue.setDuration(500); */
// as.addAnimation(scale_tongue);
tongue.startAnimation(as);
as.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
/* as=new AnimationSet(false);
rotate_tongue=new RotateAnimation(0,CalcAngle(frog,btn),frog.getWidth()/2,frog.getHeight()/2);
rotate_tongue.setDuration(1);
scale_tongue=new ScaleAnimation(30,btn.getY(),2,2,15,0);
scale_tongue.setDuration(200);
as.addAnimation(rotate_tongue);
as.addAnimation(scale_tongue);
tongue.startAnimation(as);*/
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
}
public float CalcAngle(View frog,View fly)
{
float angle = (float) Math.toDegrees(Math.atan2(frog.getY() - DataBase.FlyY, frog.getX() - DataBase.FlyX));
if(angle < 0){
angle += 360;
}
/* if (frog.getX()>=DataBase.FlyX)
{
angle=-90f-angle;
}
else
{
angle=-90f+angle;
}*/
return angle;
}
}
the problem is that doesnt work at all as the image rotates but in wrong degrees (also i would like to do this instantly not even with 1ms delay) and generally the result is not sutisfying
any help is really appreciated
thank you ;)
I'm trying a simple example like having a TextView and translate it from 0% to 100% of the screen width
code is :
anim0 = ObjectAnimator.ofFloat(textViewId00, "translationX", -120f, 480f);
where 480f is the width of my screen.
What I want is the next N times ( n=5 for my example ) to add a delay before it starts.
I have tried to add the delay with setStartDelay() method on onAnimationRepeat but there is no effect.
My code is :
textViewId00 = (TextView) findViewById(R.id.textViewId00);
anim0 = ObjectAnimator.ofFloat(textViewId00, "translationX", -120f, 480f);
anim0.setDuration(2000);
anim0.setRepeatCount(5);
anim0.addListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animation) {
Log.i(TAG, "anim0 onAnimationStart ");
textViewId00.setVisibility(View.VISIBLE);
}
#Override
public void onAnimationEnd(Animator animation) {
Log.i(TAG, "anim0 onAnimationEND " );
}
#Override
public void onAnimationCancel(Animator animation) {
}
#Override
public void onAnimationRepeat(Animator animation) {
Log.i(TAG, "anim0 onAnimation REPEAT " );
anim0.setStartDelay(14000);
}
});
anim0.start();
The textView appears to the screen, moves until it disappears and then again from start without any delay.
I have just tried it using this code and it looks like it is doing what you want it to:
public void animateView() {
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
final int screenWidth = size.x;
final ObjectAnimator anim0 = ObjectAnimator.ofFloat(textviewId00, "translationX", -120f, screenWidth);
anim0.setDuration(2000);
anim0.addListener(new Animator.AnimatorListener() {
int count;
#Override
public void onAnimationStart(Animator animation) {
textViewId00.setVisibility(View.VISIBLE);
}
#Override
public void onAnimationEnd(Animator animation) {
if (++count < 5) {
anim0.setStartDelay(14000);
anim0.start();
}
}
#Override
public void onAnimationCancel(Animator animation) {}
#Override
public void onAnimationRepeat(Animator animation) {}
});
anim0.start();
}