如何旋转drawable而不是imageView本身?

问题描述:

我有一个内部绘图的图像视图。这可绘制是一个类似于存在于进度对话框的环:如何旋转drawable而不是imageView本身?

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

all imageVie w正在旋转。我只想旋转戒指(imageview的src)我怎么能实现这一目标?

感谢

让两周地分居的看法和使用FrameLayout遏制他们。这样你就可以将动画应用到铃声而不是其他的小部件。

编辑:

我猜你是放置ImageView内部形状与android:src和设置别的东西与android:background。如果是这样的话,而不是使用ImageView你可以尝试:

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

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

你能详细介绍一下你的答案吗? “制作两个分开的视图”是什么意思,两个视图是什么? – grunk

+0

@grunk:我刚刚编辑了我的答案。 – Macarse

+0

好的感谢您的更新。我已经在使用这个“技巧”,但我希望有一个更清洁的方法。无论如何都要上传,稍等一会再接受;) – grunk

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

+0

这个解决方案有一个一次旋转的事情。我不确定这是否会对无限制(及时)旋转有用。 – grunk