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);
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!
Im trying to animate two Buttons from a point (1st button). On Click of the first button, that button is set to INVISIBLE/GONE and two other button are shown. I am trying to pass the X and Y values of the View(1st button) as follows:
button.getX();
button.getY();
but im not getting the exact center (of 1st button).
Im trying to send those X and Y values to the animation but both the buttons (button2, button3) are coming side by side.. I want them to overlap as the point where they start animation is same. Im using the following code for the animation:
AnimatorSet animations = new AnimatorSet();
Animator xAnim = ObjectAnimator.ofFloat(button, "translationX", finalXValue);
xAnim.setDuration(3000);
Animator yAnim = ObjectAnimator.ofFloat(button, "translationY", finalYValue);
yAnim.setDuration(3000);
Animator alphaAnim = ObjectAnimator.ofFloat(button, "alpha", finalAlphaValue);
alphaAnim.setDuration(alphaDuration);
//Play all the animations together
animations.play(xAnim).with(yAnim).with(alphaAnim);
I want the animation to be like this
#Deev Thanks thats really helped but the code im using has no start position
TranslateAnimation tanim_stay = new TranslateAnimation(
TranslateAnimation.ABSOLUTE, getIntent().getFloatExtra("ButtonX", 0) - btn_armed_stay.getWidth(),
TranslateAnimation.ABSOLUTE, btn_armed_stay.getX()+ (btn_armed_stay.getWidth()/2),
TranslateAnimation.ABSOLUTE, -getIntent().getFloatExtra("ButtonY", 0),
TranslateAnimation.ABSOLUTE, btn_armed_stay.getY()+ (btn_armed_stay.getHeight()/2));
Im using this code for the start and end position of the animation but how to add another animation to the current Translate animation.. I want to add Alpha animation to this Translate animation..
I am animating an ImageView from the center of the screen to the top-left using an Animation Set.I am Translating and Scaling the View.
Heres the code snippet.
AnimationSet tempAnimation=new AnimationSet(true);
TranslateAnimation anim = new TranslateAnimation( 0,xPos, 0, yPos );
anim.setFillAfter( true );
anim.setFillEnabled(true);
ScaleAnimation scaleanimation = new ScaleAnimation(1, (float)0.22, 1, (float)0.22, Animation.RELATIVE_TO_SELF, (float)0.5, Animation.RELATIVE_TO_SELF, (float)0.5);
scaleanimation.setDuration(1000);
scaleanimation.setFillEnabled(true);
scaleanimation.setFillAfter(true);
tempAnimation.addAnimation(scaleanimation);
tempAnimation.addAnimation(anim);
tempAnimation.setDuration(1000);
// This takes care that the ImageViews stay in their final positions after animating .
tempAnimation.setFillEnabled(true);
tempAnimation.setFillAfter(true);
mimageView.startAnimation(tempAnimation);
So,the problem is that it is leaving behind a trail /previous positions for just a brief moment.But it doesnt look good or smooth.I have noticed that if I use only 1 animation it is fine but using the animation set causes the trail.Are there any tips to avoid this?
Cheers
Found the answer eventually at https://code.google.com/p/android/issues/detail?id=22151.
Scale and Translation Animations have a problem on Android 2.3 .The simplest way to fix this is to add a small transparent padding around the image.In the code shown above,Just add
mimageView.setPadding(1,1,1,1);
Works like a charm!
Alphaing a drawable work well like this:
if(mAlphaAnimation == null){
mAlphaAnimation = ObjectAnimator.ofFloat(this, "alpha", 0.0f,1.0f).setDuration(TARGET_ANIM_ALPHA_DURATION);
mAlphaAnimation.setInterpolator(new AccelerateDecelerateInterpolator());
mAlphaAnimation.setStartDelay(TARGET_ANIM_ALPHA_DELAY_BASE*power);
mAlphaAnimation.setRepeatCount(ValueAnimator.INFINITE);
mAlphaAnimation.setRepeatMode(ValueAnimator.REVERSE);
mAlphaAnimation.addUpdateListener(this);
}
But if I want rotate a drawable like below , it don's work.
private void createRotateAnim(float fromDegress,float toDegress,int duration){
if(mRotateAnimation == null){
mRotateAnimation = ObjectAnimator.ofFloat(this, "rotation",fromDegress,toDegress).setDuration(duration);
mRotateAnimation.setStartDelay(100);
mRotateAnimation.setInterpolator(new AccelerateInterpolator());
mRotateAnimation.addUpdateListener(this);
}
}
Anyone can help me to fix this issue, or these is any other way to create a rotation drawable animation .
I am sorry to my poor English.
Try with ObjectAnimator instead.
ImageView imageview = (ImageView)findViewById(R.id.image);
ObjectAnimator imageViewObjectAnimator = ObjectAnimator.ofFloat(imageview ,
"rotation", 0f, 360f);
imageViewObjectAnimator.setDuration(1000); // miliseconds
imageViewObjectAnimator.start();
EDIT
Since this question draw some attention let me to explain why to use ObjectAnimator instead of other Transition animators
The thing about using ObjectAnimator is that it's moving both the visible and the clickable area of the item, if you use another animation method, for example Transition Animation or some other Animators, and let's say if you want to move the Button from the bottom left of the screen to the top left, it will only move the visible area but not the Button itself, the clickable area will still be on the previous position, in this case the clickable area will still be on the bottom left instead of the top left where you moved the button.
If you do the same with ObjectAnimator, both the visible area, and the clickable area will move the the desired location.
Try this simple Rotation Animation applied to a image.
ImageView imageview = (ImageView)findViewById(R.id.myimage);
RotateAnimation rotate = new RotateAnimation(180, 360, Animation.RELATIVE_TO_SELF,
0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotate.setDuration(500);
imageview.startAnimation(rotate);
This answer is just for a sake of question, it is correct that Clickable area will be different than View's current position. Please check this question for making clickable area correct. Button is not clickable after TranslateAnimation
I want to be able to do this: the TextView to change its size (to get bigger) and to change its alpha value (from invisible to become visible). All of this using Animation and have this changes happen at the same time.
For that purpose I come up with this code:
AnimationSet set = new AnimationSet(true);
Animation animation = new AlphaAnimation(0.0f, 1.0f);
animation.setDuration(2000);
animation.setStartOffset(300);
animation.setFillAfter(true);
set.addAnimation(animation);
animation = new ScaleAnimation(1.0f, 2.0f, 1.0f, 2.0f);
animation.setDuration(2000);
animation.setStartOffset(300);
animation.setFillAfter(true);
set.addAnimation(animation);
text.startAnimation(set);
The problem with this is that I want this transformation to remain. But the textView keeps coming back to its original size at the end. (But it remains visible - alpha = 1.0f). Am I doing something wrong? Please, if someone knows how can I make this work, help me. Thank u in advance!
I've found an answer for the problem five minutes after actually posting it. You should not define setFillAfter for the individual animations that you define in the animation set. You should only define setFillAfter ON the animation set itself