【发布时间】:2023-04-03 10:42:01
【问题描述】:
我试图在单击按钮时旋转图像,然后在单击另一个按钮时停止旋转图像。我有两个问题,1.)当您第二次单击按钮时,我不确定如何停止动画,以及 2.)我的图像旋转真的很慢,而且根本没有持续旋转。我试图让它不断地在同一个位置旋转。
这是我的 java 代码...
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
private static ImageView imgView;
private static Button button;
private int current_image_index;
private static Button creditsButton;
int[] images = {R.mipmap.imageid, R.mipmap.imageid};
ImageView blade;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonClick();
creditsClick();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
}
public void buttonClick() {
imgView = (ImageView) findViewById(R.id.imageView);
button = (Button) findViewById(R.id.button);
blade = (ImageView)findViewById(R.id.imageView4);
final Animation animRotate = AnimationUtils.loadAnimation(this, R.anim.rotate);
button.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
blade.startAnimation(animRotate);
current_image_index++;
current_image_index = current_image_index % images.length;
imgView.setImageResource(images[current_image_index]);
}
}
);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
这是我的旋转 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="1000"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="48%"
android:toDegrees="1080"
android:repeatCount="infinite"
/>
</set>
【问题讨论】:
-
您可以设置一个布尔值,例如 isRotating,并在点击监听器代码中更改使用 if / else 语句。使用滞后动画在您的 XML 动画中添加一个线性插值器 - 这可能会有所帮助。此外,toDegress 只需 359,因为您处于无限循环中。
-
好的,感谢您的反馈:)
标签: java android image-rotation