Android 如何连续旋转图像?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21430318/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to rotate an image continuously?
提问by user2152295
I am trying to rotate this image by using a thread. what should I do?
我正在尝试使用线程旋转此图像。我该怎么办?
public class Start extends Activity {
View base;
Bitmap rotate, base1, rotate1;
ImageView rotator;
float angle, pivX, pivY;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.start);
base = findViewById(R.id.base);
rotator = (ImageView) findViewById(R.id.rotator);
pivX = (rotator.getLeft()) / 2;
pivY = (rotator.getTop()) / 2;
Thread thread = new Thread() {
@Override
public void run() {
for (angle = 0; angle < 50; angle++) {
Matrix matrix = new Matrix();
rotator.setScaleType(ScaleType.MATRIX); // required
matrix.postRotate((float) angle, pivX, pivY);
rotator.setImageMatrix(matrix);
if (angle == 40) {`enter code here`
angle = 0;
return;
}
}
}
};
thread.start();
}
}
回答by Rajan
use this code for rotating a button
使用此代码旋转按钮
btn_rotate = (Button)findViewById(R.id.btn_rotate);
rotation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.rotate);
rotation.setFillAfter(true);
btn_rotate.startAnimation(rotation);
rotate.xml
旋转文件
put this file in res->anim->rotate.xml
把这个文件放在 res->anim->rotate.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<rotate
android:duration="500"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="infinite"
android:startOffset="0"
android:toDegrees="360" />
</set>
回答by MetaSnarf
I know it's probably late but here's how I do a rotate animation on java code:
我知道现在可能已经晚了,但这是我在 Java 代码上执行旋转动画的方法:
RotateAnimation rotate = new RotateAnimation(
0, 360,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f
);
rotate.setDuration(900);
rotate.setRepeatCount(Animation.INFINITE);
itemImage.startAnimation(rotate);
回答by AndroidHacker
Check this out ..
看一下这个 ..
Your Java class ..
您的 Java 类..
package com.example.rotate;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.Animation.AnimationListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements AnimationListener {
TextView txtMessage;
Button btnStart;
// Animation
Animation animFadein;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtMessage = (TextView) findViewById(R.id.tv);
btnStart = (Button) findViewById(R.id.btn);
// load the animation
animFadein = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.rotate);
// set animation listener
animFadein.setAnimationListener(this);
// button click event
btnStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
txtMessage.setVisibility(View.VISIBLE);
// start the animation
txtMessage.startAnimation(animFadein);
}
});
}
@Override
public void onAnimationEnd(Animation animation) {
// Take any action after completing the animation
// check for fade in animation
if (animation == animFadein) {
Toast.makeText(getApplicationContext(), "Animation Stopped",
Toast.LENGTH_SHORT).show();
}
}
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
}
your xml file ..
你的xml文件..
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/btn"
/>
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world" />
</RelativeLayout>
And one more thing .. you need to define animfolder in your resfolder.
还有一件事..您需要在res文件夹中定义anim文件夹。
now place rotate.xml file in animfolder.
现在将rotate.xml 文件放在anim文件夹中。
rotate.xml file
旋转.xml文件
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
android:duration="600"
android:repeatMode="restart"
android:repeatCount="infinite"
android:interpolator="@android:anim/cycle_interpolator"/>
</set>
That's it. you are good to go.
就是这样。你已准备好出发。
回答by Hitesh Sahu
In Kotlin:
在科特林:
ivBall.setOnClickListener(View.OnClickListener {
//Animate using XML
// val rotateAnimation = AnimationUtils.loadAnimation(activity, R.anim.rotate_indefinitely)
//OR using Code
val rotateAnimation = RotateAnimation(
0f, 359f,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f
)
rotateAnimation.duration = 300
rotateAnimation.repeatCount = 2
//Either way you can add Listener like this
rotateAnimation.setAnimationListener(object : Animation.AnimationListener {
override fun onAnimationStart(animation: Animation?) {
}
override fun onAnimationRepeat(animation: Animation?) {
}
override fun onAnimationEnd(animation: Animation?) {
val rand = Random()
val ballHit = rand.nextInt(50) + 1
Toast.makeText(context, "ballHit : " + ballHit, Toast.LENGTH_SHORT).show()
}
})
ivBall.startAnimation(rotateAnimation)
})
rotate_indefinitely.xml
旋转_无限期.xml
<?xml version="1.0" encoding="UTF-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="600"
android:fromDegrees="0"
android:interpolator="@android:anim/linear_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="infinite"
android:toDegrees="360" />
回答by Jéw?m'
You can create an extension in Kotlin :
您可以在 Kotlin 中创建扩展:
fun ImageView.rotate(){
val rotateAnimation = RotateAnimation(
0f, 359f,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f
)
rotateAnimation.duration = 300
rotateAnimation.setAnimationListener(object : Animation.AnimationListener {
override fun onAnimationStart(animation: Animation?) {}
override fun onAnimationRepeat(animation: Animation?) {}
override fun onAnimationEnd(animation: Animation?) {}
})
this.startAnimation(rotateAnimation)
}
}