gpt4 book ai didi

Android图像通过xml文件旋转

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:40:46 28 4
gpt4 key购买 nike

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

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

游戏.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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com