Android图片通过xml文件旋转

时间:2011-05-16 22:20:58

标签: android xml

我一直在尝试使图像文件在现场旋转并且正在挣扎,我找到的每个教程似乎都以不同的方式执行此操作。

有人可以指出我在这里出错的地方。

GamePlay.java

import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.widget.ImageView;

public class GamePlay extends Activity {

 /** Called when the activity is first created. */
 @Override public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.gameplay);

 ImageView logo = (ImageView)findViewById(R.id.mainlogo);
 logo.setBackgroundResource(R.anim.rotate);

 AnimationDrawable frameAnimation = (AnimationDrawable) logo.getBackground();

 frameAnimation.start();

 }
}

rotate.xml

<?xml version="1.0" encoding="UTF-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:pivotX="50%" 
android:pivotY="50%" 
android:fromDegrees="0"
android:toDegrees="360" 
android:drawable="@drawable/logo" />

gameplay.xml     

<ImageView
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content"
    android:id="@+id/mainlogo"
    android:src="@drawable/logo">       
</ImageView>

2 个答案:

答案 0 :(得分:1)

将rotate.xml更改为

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

将其放在res / anim /文件夹

并尝试此操作以启动动画

ImageView logo = (ImageView)findViewById(R.id.mainlogo);
Animation rotateAnimation = AnimationUtils.loadAnimation(context,
            R.anim.rotate);
logo.startAnimation(rotateAnimation);

答案 1 :(得分:0)

试试这段代码;它对我有用:

ImageView img=(ImageView)findViewById(R.id.ImageView01);
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.snoopy);
// Getting width & height of the given image.
int w = bmp.getWidth();
int h = bmp.getHeight();
// Setting post rotate to 90
Matrix mtx = new Matrix();
mtx.postRotate(90);
// Rotating Bitmap
Bitmap rotatedBMP = Bitmap.createBitmap(bmp, 0, 0, w, h, mtx, true);
BitmapDrawable bmd = new BitmapDrawable(rotatedBMP);

img.setImageDrawable(bmd);

}