Android图片通过xml文件旋转

Pho*_*x26 6 xml android

我一直试图让图像文件在现场旋转并且正在挣扎,我找到的每个教程似乎都以不同的方式做到这一点.

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

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

 }
}
Run Code Online (Sandbox Code Playgroud)

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" />
Run Code Online (Sandbox Code Playgroud)

gameplay.xml

<ImageView
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content"
    android:id="@+id/mainlogo"
    android:src="@drawable/logo">       
</ImageView>
Run Code Online (Sandbox Code Playgroud)

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

}
Run Code Online (Sandbox Code Playgroud)