Android图像通过xml文件旋转

标签 android xml

我一直在尝试让图像文件当场旋转,但我很挣扎,我发现的每个教程似乎都以不同的方式做到这一点。

有人可以指出我哪里出错了吗?

游戏.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();

 }
}

旋转.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" />

游戏.xml

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

最佳答案

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

关于Android图像通过xml文件旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6024113/

相关文章:

java - 如何使用 JAXB 解码输入流?

objective-c - RaptureXML 奇怪的行为

javascript - XML : Remove parent, 克隆到同级

android - Listview错误无法运行;强制关闭

android - 如果未选择任何选项,DialogPreference 不应关闭

android - 使用编译不同风格的库迁移到 gradle 3.+

android - 如何从 Edittext 中检索多行文本?

java - 努力使 OnClickListener 为具有自定义 ViewHolder 的 Firebase Recycler UI 工作

android - 实现 setOnClickListener

java - 在 Angular 项目中显示 pom.xml 版本