如何旋转Drawable而不是ImageView本身?

3
我有一个带有图像视图的可绘制对象。这个可绘制对象是一个类似于进度对话框中存在的圆环: 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>

为了旋转这个环,我创建了一个旋转动画,代码如下:
<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" />

但是,当我将动画应用于imageView时:
this.cameraView.setAnimation(AnimationUtils.loadAnimation(this,R.anim.loadinganimation));

所有的imageView都在旋转。我想只旋转环(即imageview的src),我该怎么做?
谢谢
2个回答

2
// 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);

具体细节请查看链接。

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


这个解决方案适合一次性旋转。我不确定它在无时间限制的情况下是否有用。 - grunk

1
创建两个分离的视图,并使用FrameLayout来包含它们。这样,您就可以将动画应用于环而不是其他小部件。

编辑:

我猜你是把形状放在带有android:srcImageView中,并使用android:background设置其他内容。如果是这种情况,您可以尝试不使用ImageView

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

或者

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

你能详细说明一下你的答案吗?你所说的“制作两个分离视图”是什么意思,为什么要有两个视图? - grunk
好的,感谢更新。我已经在使用这个“技巧”了,但我希望有更干净的方法。不过还是点赞并等待一下再接受它吧 ;) - grunk

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接