2011-05-16 210 views
6

我一直在试图使图像文件在现场旋转,并且挣扎着,我发现每个教程似乎都以不同的方式做到这一点。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> 

+0

图像旋转你说?我会在OpenGL中将其作为纹理加载,然后使用正常的Open GL旋转命令来执行xml所说的操作。我也会使用简单的XML框架来解析XML。 – 2011-05-17 11:04:30

+0

在rotate.xml中应该位于'res/anim'中,您应该添加一些属性,例如:android:repeatCount =“infinite”'android:duration =“1200”' – McIntosh 2012-11-06 12:04:46

+1

我认为它正在旋转,你没有'android:duration =“”'它不能被看到。 – Aiapaec 2014-06-12 14:42:47

回答

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

} 
+0

你的大括号有问题......特别是,你错过了开头的一个。 – 2011-12-22 06:51:13

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 /动画/文件夹

,并尝试这个启动动画

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