i want to rotate the image of imageView, i have scaled the image using matrix but having problem in rotation, i'm using the code...
int previousDegrees = 0;
int degrees = 90;
RotateAnimation animation = new RotateAnimation(previousDegrees,degrees,160,160);
ImageView imageView = (ImageView)findViewById(R.id.imageView);
animation.setDuration(1000);//Set the duration of the animation to 1 sec.
imageView.startAnimation(animation);
it rotates the image for a second and setback to original position.. is there any way that image could rotate onclick continuosly.. like on (0,90.180.270,360)degrees...
Any Help please!!!
Set these parameters like so:
animation.setFillEnabled(true);
animation.setFillAfter(true);
From Android Developers Reference:
If fillAfter is true, the transformation that this animation performed will persist when it is finished.
Related
I want to rotate a View to 90 degree and update its content and again rotate it to 90 degree, and so far I tried below code.
ObjectAnimator anim1 = (ObjectAnimator) AnimatorInflater.loadAnimator(getActivity().getApplicationContext(), R.animator.flip_animator);
anim1.setTarget(v);
anim1.setDuration(1000);
anim1.start();
ImageView box = (ImageView) v.findViewById(R.id.box);
Bitmap bg = BoxUtils.getBoxBg(boxSize, boxMargin, Color.RED);
box.setImageBitmap(bg);
anim1.setTarget(v);
anim1.setDuration(1000);
anim1.start();
xml:
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:valueFrom="0" android:valueTo="90" android:propertyName="rotationY" >
</objectAnimator>
However its working but its changing its color before it rotates next 90 degree, even i had written updation code before than next rotation code.
Any help ?
The problem is that your object rotates asynchronously from your code. You have designed it so that the code sits there while the cube does the first rotation, then changes the color. What happens is that the code starts the animation, then keeps running as the cube rotates, which is why it changes color early.
The following method of animating your view will add an end action, which will be changing color then rotating the last 90:
View v;
v.animate()
.rotationY(90)
.setDuration(1000)
.withEndAction(new Runnable() {
#Override
public void run() {
ImageView box = (ImageView) v.findViewById(R.id.box);
Bitmap bg = BoxUtils.getBoxBg(boxSize, boxMargin, Color.RED);
box.setImageBitmap(bg);
v.animate().rotationY(90).setDuration(1000).start();
}
}).start();
I have sliding up panel and in the titlebar of that panel I have ImageView as in the picture. How to rotate to 180* by clockwise that ImageView when user slide panel up and anti-clockwise when slide down.
For sliding that panel I have method:
#Override
public void onPanelSlide(View panel, float slideOffset) {
Log.i(TAG, "onPanelSlide, offset " + slideOffset);
//Animation here
}
Thanks for any help!
If you want to rotate a view without animation:
view.setRotation(float) sets the degrees that the view is
rotated around the pivot point. Increasing values result in clockwise
rotation.
view.setRotationX(float) sets the degrees that the view is
rotated around the horizontal axis through the pivot point.
Increasing values result in clockwise rotation from the viewpoint of
looking down the x axis. When rotating large views, it is recommended to adjust the camera distance accordingly.
view.setRotationY(float) sets the degrees that the view is
rotated around the vertical axis through the pivot point. Increasing
values result in counter-clockwise rotation from the viewpoint of
looking down the y axis. When rotating large views, it is recommended to adjust the camera distance accordingly.
If you want to use animation:
https://github.com/daimajia/AndroidViewAnimations (recommended)
https://github.com/castorflex/FlipImageView
https://github.com/florent37/ViewAnimator
https://github.com/alexvasilkov/GestureViews
https://gist.github.com/simon-heinen/9795036
https://gist.github.com/methodin/5678214
https://www.google.com/ :D
I didn't want to mark this as an answer but I can't leave comments yet, I was able to get Apurva's solution working correctly but I had to use this instead of getApplicationContext(), I had to use this because of where I was setting the animation from, the code in my application where I am attaching the animation looks like this...
ImageView billysArm = (ImageView) findViewById(R.id.imageView6);
Animation rotate = AnimationUtils.loadAnimation(this, R.anim.rotate);
billysArm.startAnimation(rotate);
hope this helps
You should try this out, setting an animation for your e.g. ImageView
#Override
public void onPanelSlide(View panel, float slideOffset) {
Log.i(TAG, "onPanelSlide, offset " + slideOffset);
// Locate view
ImageView iv= (ImageView) findViewById(R.id.iv);
// Create an animation instance
Animation an = new RotateAnimation(0.0f, 180.0f, pivotX, pivotY);
// Set the animation's parameters
an.setDuration(10000); // duration in ms
an.setRepeatCount(0); // -1 = infinite repeated
an.setRepeatMode(Animation.REVERSE); // reverses each repeat
an.setFillAfter(true); // keep rotation after animation
// Aply animation to image view
iv.setAnimation(an);
}
I am developing a game where a user needs to tap on the image in ImageView to rotate it. On each tap image rotates by 90 degrees in clockwise direction.
But image is taking time to rotate from old to new position. This is hampering the gameplay experience. I have used the following:
protected void onCreate(Bundle savedInstanceState)
{
...
...
imgview = (ImageView)findViewById(R.id.imageView1);
imgview.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Matrix matrix = new Matrix();
matrix.postRotate(90);
Bitmap myImg = getBitmapFromDrawable(imgview.getDrawable());
Bitmap rotated = Bitmap.createBitmap(myImg,0,0,myImg.getWidth(),myImg.getHeight(),matrix,true);
imgview.setImageBitmap(rotated);
}
});
I want to know is there any other way to rotate the image
without creating any delay in rotation.
I also tried to do it once and couldn't find any other solution but using animation. Here how I'd do.
private void rotate(float degree) {
final RotateAnimation rotateAnim = new RotateAnimation(0.0f, degree,
RotateAnimation.RELATIVE_TO_SELF, 0.5f,
RotateAnimation.RELATIVE_TO_SELF, 0.5f);
rotateAnim.setDuration(0);
rotateAnim.setFillAfter(true);
imgview.startAnimation(rotateAnim);
}
You don't need to rotate the object, rotating the view should be enough.
If you are aiming API>=11 you can always do this.
mImageView.setRotation(angle);
Cheers.
Short method
View.animate().rotation(90).setDuration(0);
did you try to use a custom view extending imageview and rotating the image in a background thread?
Hi am doing one application here when my activity starts that time i need to rotate image first then i need to move image from top to center there i have to scale my image after some time i have to in visable my imageview,i tried using below code first i applied rotate animation then i applied translate animation.after 2 seconds i applied scale animation but image is not scaling in center its taking imagview original postion(top) there its scaling but i need to scale imageview after move animation where is there at that postion i need scale image view...any one suggest how to apply different animations to single imageview.
public class A extends Activity{
TranslateAnimation moveLefttoRight1;
Animation logoMoveAnimation;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
final ImageView myImage = (ImageView)findViewById(R.id.imageView1);
//rotate animation
final Animation myRotation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.rotator);
myImage.startAnimation(myRotation);
//translate animation
moveLefttoRight1 = new TranslateAnimation(0, 0, 0, 200);
moveLefttoRight1.setDuration(2000);
moveLefttoRight1.setFillAfter(true);
myImage.startAnimation(moveLefttoRight1);
//scale animation
logoMoveAnimation = AnimationUtils.loadAnimation(this, R.anim.sacle);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
myImage.startAnimation(logoMoveAnimation);
}
}, 2000);
}
}
Use Animation listener.
when get animation end start another animation.
for better reference
http://developer.android.com/reference/android/view/animation/Animation.AnimationListener.html
http://www.techrepublic.com/blog/android-app-builder/how-to-use-android-animation-listeners/
I want to rotate an imageview from 30 degrees on each click on a button.
On the first clic, i can set the animation properly but i can't succeed to update the imageview position after the animation. When i clicked again on the button, the animation start from the original position of the imageview and not from the final position after the first animation.
Here is my code :
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
turnImg = (ImageView) findViewById(R.id.imageViewturnImg );
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.imgTurn);
// Getting width & height of the given image.
int w = bmp.getWidth();
int h = bmp.getHeight();
turnImg .setImageBitmap(bmp);
Button buttonok = (Button) findViewById(R.id.buttonok);
buttonok.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
turn();
}
});
}
public void turn()
{
float degrees = 30;
RotateAnimation anim = new RotateAnimation(0, degrees,Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,0.5f);
anim.setInterpolator(new LinearInterpolator());
anim.setDuration(300);
anim.setFillEnabled(true);
anim.setFillAfter(true);
anim.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationEnd(Animation arg0) {
Matrix mat = turnImg.getImageMatrix();
mat.postRotate(30,turnImg.getWidth()/2, turnImg.getHeight()/2);
turnImg.setScaleType(ScaleType.MATRIX);
turnImg.setImageMatrix(mat);
}
#Override
public void onAnimationRepeat(Animation animation) {}
#Override
public void onAnimationStart(Animation animation) {}
});
turnImg.startAnimation(anim);
}
I think the problem came from the way i actualize the position in the onAnimationEnd().
ps : sorry for my bad english...
The problem is that your animation is not affecting the same object as your matrix rotation. That is, you are animating the rotation of the ImageView object, but then you are setting the rotation of the image that is within that ImageView object. So, for example, the first time you rotate it, the ImageView rotates from 0 to 30 degrees, and then remains at a rotation of 30 degrees due to the call to setFillAfter(true). Then the onAnimationEnd() handler runs, which rotates the internal image by 30 degrees. This effectively jumps the image to a rotation of 60 degrees, as seen by the user (30 for the View, 30 for the bitmap). Then the next time the animation runs, it rotates the view from 0 to 30 degrees, with the internal image still at its rotation of 30 degrees. This makes it look to the user like it's rotating from 30 to 60 degrees. Then you apply another rotation on the internal image when that animation finishes, ending up with the image rotated to 60 degrees and the view rotated (again) to 30 degrees, so the image is now visually rotated to 90 degrees.
And so on.
There are different ways to handle this problem, but one simple way is to avoid affecting the two different objects - just work with the ImageView. Instead of rotating from 0 to 30 each time, rotate from whatever the current rotation is to that value plus 30 degrees. You can remove the onAnimationEnd() handler, because you will no longer need to rotate the image inside the view.
The resulting code for turn() is much simpler:
public void turn()
{
RotateAnimation anim = new RotateAnimation(currentRotation, currentRotation + 30,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,0.5f);
currentRotation = (currentRotation + 30) % 360;
anim.setInterpolator(new LinearInterpolator());
anim.setDuration(1000);
anim.setFillEnabled(true);
anim.setFillAfter(true);
turnImg.startAnimation(anim);
}
This code assumes an instance variable of currentRotation, which is used to track the last degrees the view was rotated to.
Its a bit more involved if you allow the user to click mid-animation, but not too difficult.
By the way, this is much simpler in the animation system in 3.0; there is now a 'rotation' property on View, and you can run an animation on that property and it can track its own current value. But the approach above should work for older releases.