RotateDrawable以编程方式在android中

时间:2014-02-27 13:19:02

标签: android shapedrawable animationdrawable

如何以编程方式提供fromFegreestoDegreesandroid:color="#000000"

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item >


        <rotate 
            android:fromDegrees="45"
            android:toDegrees="45"
            android:pivotX="-40%"
            android:pivotY="87%" >

            <shape 
            android:shape="rectangle"  >
            <stroke android:color="@android:color/transparent" android:width="10dp"/>
            <solid  
                android:color="#000000"  />
        </shape>

        </rotate>
    </item>
</layer-list>

我在视图背景中使用此xml。

我必须以编程方式创建三角形。所以需要以编程方式创建RotationDrawable。

2 个答案:

答案 0 :(得分:4)

这是一个很好的解决方案,可以为imageView放置一个旋转的drawable:

RotateAnimation anim = new RotateAnimation(0.0f, 360.0f , Animation.RELATIVE_TO_SELF, .5f, Animation.RELATIVE_TO_SELF, .5f);
anim.setInterpolator(new LinearInterpolator());
anim.setRepeatCount(Animation.INFINITE);
anim.setDuration(3000);
iv.setAnimation(anim);
iv.startAnimation(anim);

答案 1 :(得分:0)

您可以使用Matrix以编程方式旋转图像:

rotationMatrix = new Matrix();
// Set the scale type of your ImageView to "Matrix"
yourImageView.setScaleType(ScaleType.MATRIX);

// to set the rotation to a specific degree:
rotationMatrix.setRotate(degrees);
// or add some degrees to the current rotation
rotationMatrix.postRotate(degrees);

// apply matrix to your ImageView
yourImageView.setImageMatrix(rotationMatrix);