I'm still having problem with the most basic need.
I wanna have 2 buttons: one runs a tween animation that moves an image 50 pixels, the other button runs a frame by frame animation on that image.
once the tween animation is done, the frame by frame animation runs in the wrong location. weird.
I'm using FillAfter/FillEnabled,
the code to run the tween animation is a basic
TranslateAnimation(0, 50, 0 , 0) with the fill after and fillEnabled = true.
the frame by frame code is
image.setImageDrawable(getResources().getDrawable(resourceId));
((AnimationDrawable) image.getDrawable()).start();
help will be appreciated.
Cheers
The only way I found of doing it is
first you move your imageview position, using setLayoutParams with the new margins,
and only then you start the TranslateAnimation using (-x, -y, 0, 0) so for my specific case in the question, here is my code:
TranslateAnimation tweenAnim = new TranslateAnimation(-1 * XStep, -1 * YStep , 0, 0);
tweenAnim.setDuration(number_of_steps * 750);
tweenAnim.setFillAfter(true);
tweenAnim.setFillEnabled(true);
tweenAnim.setInterpolator(new LinearInterpolator());
//layout positioning
lp.leftMargin += XStep;
lp.bottomMargin += YStep;
imageView.setLayoutParams(lp);
Log.v(TAG, "leftMargin=" + lp.leftMargin);
Log.v(TAG, "bottomMargin=" + lp.bottomMargin);
imageView.startAnimation(tweenAnim);
This was a huge pain. seriously.
thanks for http://www.clingmarks.com/?p=400 I was able to overcome it.
Related
I am making a Rollette game and I want to play sound in each crossing section like if 1 is crossing it should find some trigger or collision detection and ping one tick sound. My problem is that I am not able to find the collision detection on image. Below is the approaches that I have done.
I have taken LayoutView and placed a Rollette wheel image inside it.
In each section (0-9) has taken a green small button which will be used to detect the collision with the arrow. Once it collides there will be a Tick sound with up-down animation in arrow. (Image attached).
Problem.
I am not able to find the new co-ordinate of views in each rotation. It is returning the same static location every time and hence collision is never happening.
Rotation Code..
final RotateAnimation rotate = new RotateAnimation(0, 360f,
RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f);
rotate.setDuration(10000);
rotate.setFillAfter(true);
Collision detection code...
Rect arrowBtnRect = new Rect();
arrowBtn.getGlobalVisibleRect(arrowBtnRect);
Rect btn0Rect = new Rect();
btn0.getGlobalVisibleRect(btn0Rect);
if(arrowBtnRect.intersect(btn0Rect)) {
System.out.println("Collision detected "+numberSelected);
numberSelected = "0";
return true;
}
Your idea is brilliant!
But I have certain remarks on your approach:
setFillAfter() is not helping, according to documentations:
If fillAfter is true, the transformation that this animation performed will persist when it is finished.
I recommend using ObjectAnimator instead of RotateAnimation. While RotateAnimation is just making the view looks as if it is rotating until you call fillAfter (which happens after animation stops), ObjectAnimator makes the view really change its actual angle while rotating. See this: https://stackoverflow.com/a/29465710/10005752
Usage:
Random random = new Random();
float randomAngle = (float) (random.nextInt(360) + 1);
ObjectAnimator rouletteAnimator = ObjectAnimator.ofFloat(rouletteView, View.ROTATION, 0f, randomAngle);
rouletteAnimator.setDuration(10000);
rouletteAnimator.start();
Also, to elaborate more on my comment I recommend that get all angles that have buttons on them before starting animating, and when animation starts keep using rouletteView.getRotation(); to check what angle is at the top.
Example:
Button1 is at angle 90 and rouletteView is rotating Counter Clockwise. Then if rouletteView.getRotation() == 360 - 90 is true then Button1 is touching the arrow.
I hope I'm clear!
I have image which is symmetrical and I want to move it infinitly from right to left smoothly. I tried to use TranslateAnimation but first I have to properly set my image which is quite difficult mainly because this image is using all screen width and I should set negative margins. Are there any other solution? And is there a possibility to move image without moving ImageView?
Finally I made it by my own and the solution is to put 2 images on each other and then measure screen width and use 2 TranslateAnimation, one from screen width to 0 and second one from 0 to -screen width:
TranslateAnimation anim = new TranslateAnimation(0, -screenWidth, 0, 0);
TranslateAnimation anim2 = new TranslateAnimation(screenWidth, 0, 0, 0);
anim.setDuration(5000);
anim.setRepeatCount(Animation.INFINITE);
anim.setInterpolator(new LinearInterpolator());
anim2.setDuration(5000);
anim2.setRepeatCount(Animation.INFINITE);
anim2.setInterpolator(new LinearInterpolator());
backgroundOverlayImage.startAnimation(anim);
backgroundOverlayImage2.startAnimation(anim2);
I think this is what you are trying to do:
TranslateAnimation anim = new TranslateAnimation(0, -1000, 0, 0);
anim.setDuration(1500);
anim.setFillAfter(true);
anim.setRepeatCount(0);
anim.setInterpolator(this, Android.Resource.Animation.LinearInterpolator);
Edit:
at the end don't forget imageView.startAnimation(anim);
Though you got answer, try this repository, this will solve all your queries regarding slides and also will add custom animation to your views!
I need to translate and scale image view like on this picture from state 1 to state 2,
State 1 is center of screen, state 2 is any location on screen;
I have combined to animations
a= new TranslateAnimation(Animation.ABSOLUTE,startx,Animation.ABSOLUTE,destX*5,Animation.ABSOLUTE,starty,Animation.ABSOLUTE,(destY-32)*5);
a.setDuration(1000);
a.setStartOffset(2000);
as.addAnimation(a);
a = new ScaleAnimation(1f, 0.2f, 1f, 0.2f);
a.setDuration(1000);
a.setStartOffset(2000);
as.addAnimation(a);
Where "as" is AnimationSet. When I start the animation.
Everything works well, start and end position ok, but translation between them not going along "straight" line. It goes like an arc, on picture 2;
What kind of scale-animation and translate-animation need I perform and in which order to get straight transformation?
picture 1 pic1
picture 2 pic2
After changing order of animations and removing mult by 5 in translateanimation all works ok
a = new ScaleAnimation(1f, 0.2f, 1f, 0.2f);
a.setDuration(1000);
a.setStartOffset(2000);
as.addAnimation(a);
a= new TranslateAnimation(Animation.ABSOLUTE,startx,Animation.ABSOLUTE,destX,Animation.ABSOLUTE,starty,Animation.ABSOLUTE,(destY-32));
a.setDuration(1000);
a.setStartOffset(2000);
as.addAnimation(a);
I have a linear layout with three images in it. On the click of any ImageView I want to animate the whole linear layout. The animation consists of two animations - scale and transform. I don't know how to perform this animation.
This is what I want -
Image 1 - Before Animation
Image 2 - After Animation
I want to not only animate the View but actually shift the view to its new location with animation. After animation I should be able to click the Images at their new locatoin not the old location.
How can I perform this? Please help me out.
I used nineoldandroids library to achieve this animation. I used two animation: translation and scaling.
Translation:
ObjectAnimator.ofFloat(frameCategoryScroll, "translationY", 0, -yValue).setDuration(600).start();
yValue is the the value in y axis upto which I want to translate.
Scaling:
ObjectAnimator.ofFloat(frameCategoryScroll, "scaleX", 1.0f, 0.6f).setDuration(600).start();
ObjectAnimator.ofFloat(frameCategoryScroll, "scaleY", 1.0f, 0.6f).setDuration(600).start();
I have an 800x480 image, and I want to have a scale animation so that the image scales to 960x576. I know that Android has a ScaleAnimation class for this, but I also want the user to be able to pause the animation while it's scaling and resume from where they paused.
How can I do this?
I did write some custom refresh loop but it's not super smooth like the ScaleAnimation class. The custom refresh loop just calculates the change in width and height that is desired per 25 ms and then sets the new width to the Rect. Then the loop calls invalidate.
//Reset the image size
x = (x + dx);
y = (y + dy);
imageWidth = (imageWidth + dWidth);
imageHeight = (imageHeight + dHeight);
I then draw on the canvas.
outputRect.set((int)(-x), (int)(-y), (int)imageWidth, (int)imageHeight);
canvas.drawBitmap(bitmap, srcRect, outputRect, paint);
How can I do this just as smoothly as with the Android OS or more efficiently?
I would suggest using Androids ScaleAnimation. You can anim.cancel() an animation and it might resume right where it was canceled if started again. The documentation suggest calling reset() before reusing an animation, so that might work.
If it does not work, you can save the animation-state on pause and create a new animation from that point on, if the user resumes the animation.