grunk
grunk

Reputation: 14938

How to rotate a drawable but not the imageView itself?

I'have an image view with a drawable inside. This drawable is a ring similar to the one present in progress dialog :

loading_ring :

<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="ring"
    android:innerRadiusRatio="3"
    android:thicknessRatio="8"
    android:useLevel="false">
    <size
        android:width="20dip"
        android:height="20dip"/>

    <gradient
        android:type="sweep"
        android:useLevel="false"
        android:startColor="#4c737373"
        android:centerColor="#4c737373"
        android:centerY="0.50"
        android:endColor="#ffffffff"/>
</shape>

In order to rotate this ring i have created a rotate animation like this :

<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="0"
    android:toDegrees="359"
    android:pivotX="50%"
    android:pivotY="50%"
    android:repeatCount="infinite"
    android:duration="1000"
    android:interpolator="@android:anim/linear_interpolator" />

but when i apply the animation on the imageView :

this.cameraView.setAnimation(AnimationUtils.loadAnimation(this,R.anim.loadinganimation));

The all imageView is rotating. I would like to rotate only the ring (the src of the imageview) how can i achieve that ?

Thanks

Upvotes: 3

Views: 5194

Answers (2)

Macarse
Macarse

Reputation: 93123

Make two separated views and use a FrameLayout to contain them. This way you will be able to apply the animation to the ring and not to the rest of the widgets.

EDIT:

I guess you are placing the shape inside the ImageView with android:src and setting something else with android:background. If that is the case, instead of using an ImageView you can try:

<FrameLayout>
  <ImageView with the background>
  <ImageView with JUST the shape>
</FrameLayout>

or

<FrameLayout android:background="blah">
  <ImageView with JUST the shape>
</FrameLayout>

Upvotes: 1

Arslan Anwar
Arslan Anwar

Reputation: 18746

// 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

Upvotes: 2

Related Questions