0

I simply want to rotate an image from min to max. But for that i have used multiple images to show Progress. Can some one suggest me a way to use a single image. which can rotate at an angle from min to max.

I know there are two possible ways to achieve it.

  1. Using Animation Classes
  2. Custom View

enter image description here

I simply want to rotate this image using SeekBar in any number of steps.

How can i achieve this?

To roate an Image

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);
}

Second Approach

imageView.setRotation(angle); // but requires API >= 11

I can use Matrix

Matrix matrix = new Matrix();
imageView.setScaleType(ScaleType.MATRIX);   //required
matrix.postRotate((float) angle, pivX, pivY);
imageView.setImageMatrix(matrix);

How can i set Start and end angle to SeekBar min and max respectively. Which approach is better and Whether i must put it in FrameLayout to let it rotate freely.

tkw83
  • 185
  • 1
  • 5
Zar E Ahmer
  • 33,936
  • 20
  • 234
  • 300

3 Answers3

2

You can do this way :

// load the origial BitMap (500 x 500 px)
Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),
       R.drawable.android);

int width = bitmapOrg.width();
int height = bitmapOrg.height();

// createa matrix for the manipulation
Matrix matrix = new Matrix();

// rotate the Bitmap
matrix.postRotate(45);

// recreate the new Bitmap
Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, width, height, matrix, true);

// make a Drawable from Bitmap to allow to set the BitMap
// to the ImageView, ImageButton or what ever
BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);

// set the Drawable on the ImageView
imageView.setImageDrawable(bmd);

For details check this out.

http://www.anddev.org/resize_and_rotate_image_-_example-t621.html

matrix.postRotate(45);  //here 45 is the degree of angle to rotate
0

You can use a Matrix as suggested here

Matrix matrix = new Matrix();
imageView.setScaleType(ScaleType.MATRIX);   //required
matrix.postRotate((float) angle, pivX, pivY);
imageView.setImageMatrix(matrix);
Community
  • 1
  • 1
Marcus
  • 6,697
  • 11
  • 46
  • 89
0

This would be an infinite rotation but you could spin it however you want. This view just spins 360 degrees infinitely.

        final RotateAnimation R = new RotateAnimation(0, 360, view.getWidth() / 2.0f, view.getHeight() / 2.0f);
            R.setRepeatCount(Animation.INFINITE);
            R.setDuration(800);
            view.startAnimation(R);
Xjasz
  • 1,238
  • 1
  • 9
  • 21