如何在其位置旋转拨号图像。

时间:2012-03-02 08:48:55

标签: android

我有一个轮子,我想把它旋转到它的位置。我的代码正在旋转此图像,但它会改变它的位置。我想把这张图片放在修复的地方。

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    dialImage = (ImageView) findViewById(R.id.inner_dial);

    float rotation = (float) Math.toDegrees(90);
    Matrix matrix = new Matrix();
    matrix.postRotate(rotation);

    bMap = BitmapFactory.decodeResource(getResources(),
            R.drawable.inner_temp);

    dialImage.setBackgroundResource(0);

    Bitmap bMapRotate = Bitmap.createBitmap(bMap, 0, 0, bMap.getWidth(),
            bMap.getHeight(), matrix, true);
    dialImage.setImageBitmap(bMapRotate);

}

我的xml文件:

<?xml version="1.0" encoding="utf-8"?>

<ImageView android:background="@drawable/inner_temp"
    android:layout_height="wrap_content" android:id="@+id/inner_dial"
    android:layout_width="wrap_content" android:layout_alignParentLeft="true" />

2 个答案:

答案 0 :(得分:1)

问题是坐标系的参考点。会发生的是,您的图像围绕此参考点旋转90度。为了将图像旋转到位,您需要以某种方式变换图像位置,使图像的中心和坐标系的参考点相同。

答案 1 :(得分:0)

将此rotate.xml保存在anim的{​​{1}}文件夹中。

res

开始动画:

<?xml version="1.0" encoding="UTF-8"?>
<rotate
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="0"
    android:toDegrees="360"
    android:pivotX="50%"
    android:pivotY="50%"
    android:repeatCount="infinite"
    android:duration="1200"
    android:interpolator="@android:anim/linear_interpolator" />

修改

以编程方式使用旋转动画:

dialImage.startAnimation(AnimationUtils.loadAnimation( this, R.anim.rotate ) );

请按照此link进行详细说明。