Rotate image on center not going smooth (Monodroid) - android

I have an Image (which looks like a round loading circle) that I want to rotate around it's own center.. I saw a lot of code which suggested to set the PivotY and PivotX to 0.5F or th half of the image. Both does not work.. After a lot of trial and error it does rotate around it's own center with the following code:
ImageView loading = FindViewById<ImageView>(Resource.Id.loadingGif);
RotateAnimation rAnim = new RotateAnimation(0.0F, 359.0F, Dimension.RelativeToSelf, 0.25F, Dimension.RelativeToSelf, 0.25F);
rAnim.Interpolator = new LinearInterpolator();
rAnim.RepeatCount = Animation.Infinite;
rAnim.Duration = 1000;
loading.StartAnimation(rAnim);
But the animation itself is not going smooth anymore, the image seems to rotate about 50degrees then starts to hang and skips half of the rotation and then continues normally on the top half of the rotation (I hope that makes any sense).
Any idea why my rotation does not rotate the full 360 degrees smoothly?
EDIT
I still haven't solved this problem, but I did just discover something peculiar about it. The animation is used on a loading screen with no extra functionalities on the moment of the animation. I noticed that when I keep my finger on the screen the animation DOES happen smoothly!!
This got me thinking that the issue maybe isn't in the animation code but something else.. but what I don't know yet..

I've finally found the answer!
I've included the line:
loading.setDrawingCacheEnabled(true);
And after that had to change the pivots to 0.5F too. And now the animation runs smoothly!
//Rotate image
ImageView loading = FindViewById<ImageView>(Resource.Id.loadingGif);
loading.setDrawingCacheEnabled(true);
rAnim = new RotateAnimation(0.0F, 359.0F, Dimension.RelativeToSelf, 0.5F, Dimension.RelativeToSelf, 0.5F);
rAnim.Interpolator = new LinearInterpolator();
rAnim.RepeatCount = Animation.Infinite;
rAnim.Duration = 1500;
loading.StartAnimation(rAnim);

Related

how to play sound in Rollette game to each section cross android

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!

How to rotate a text View 90 degree around itself?

As I said I have a textView and I want to rotate it 90 degree.
Problem is I don´t understand where I can set the fixed point.
I use this function
myTextView.animate().rotation(90).setDuration(1000).start();
this does rotate my view but it is always a little bit transformed in one direction.
My end result I want to be that here where the buttom of the text was befor now the right side of the text should be. and there where the left side was I now want the buttom.
Does anybody know how I can do that.
If nothing works I can use transformations to get the text at the right position but I don´t know whow large the text is and so I don´t know how to get the values for the transformation.
RotateAnimation rotate = new RotateAnimation(0, 90,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
rotate.setDuration(4000);
rotate.setRepeatCount(Animation.INFINITE);
yourView.setAnimation(rotate);
For more detail check LINK
use this
RotateAnimation rotateAnmi = (RotateAnimation) AnimationUtils.loadAnimation(this,R.drawable.rorate);
rotateTv.setAnimation(rotateAnmi);
rotateTv.setGravity(Gravity.AXIS_X_SHIFT);
rotateTv.setText();

Android Image View Animation leaving behind trail

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!

Android rotation animation

I have a problem and I just can't manage to find a solution that works.
So, here is my problem. I have to make an ImageView to rotate itself, to 90 degree on orientation change.
I did that, and the image is actually rotating pretty awesome, but when the animation finishes, the image reset itself to previous position.
Here is the code I used to rotate the image:
Matrix matrix = mImageView.getImageMatrix();
RectF dst = new RectF();
matrix.mapRect(dst, new RectF(mImageView.getDrawable().getBounds()));
mAnimation = new RotateAnimation(0.0f, -90.0f, Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
mAnimation.setDuration(5000);
mImageView.startAnimation(mAnimation);
mImageView.setImageMatrix(matrix);
mCurrentOrientation = 1;
you can use this to animation persist after it has been done:
mAnimation.setFillEnabled(true);
mAnimation.setFillAfter(true);
One thing you can do is set the imageview to an already rotated version of the image and then rotate it from -90 to 0 degrees. That will work with the API you are already using. If you make the two calls, one right after it will work correctly without stutter.
The other option is to use a later API version. The Object Animator animations do not return the view to it's original position. More on this here.
How to rotate a drawable by ObjectAnimator?
The code is simple, but this doesn't work with gingerbread.

Android RotateAnimation - rotate arrow back and forth

In my application, I am rotating a clock hand to the desired amount of minutes. This works good with RotateAnimation. Now what I want to do is to return the hand back to the starting position (0 minutes).
For example:
I first rotate the hand like this:
final RotateAnimation anim = new RotateAnimation(0f, ammountDegress, RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 1f);
anim.setFillAfter(true);
anim.setFillEnabled(true)
Then I would like to move the hand back to it's starting position, so that means I should be calling a RotateAnimation after the first animation ends.
The problem is that the pivots have changed, how I can I set the pivot position to the exact position as previous? So that the rotating point is again at the same position of the clock hand.
Edit To make it more clearer - I would like to have something like a fixed point pivot that doesn't change with the rotation, so I am always rotating the hand around the same point.
I hope you can understand what I mean.

Categories

Resources