我尝试在按钮单击上旋转图像,然后在另一个按钮单击时停止旋转图像。我有两个问题,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>
答案 0 :(得分:1)
1,使用clearAnimation()
停止动画,将代码放在onClickListener
下面:
if (blade.getAnimation() == null) {
// no animation, start it
blade.startAnimation(animRotate);
} else {
//animation is showing, stop it
blade.clearAnimation();
}
2,在imgView.invalidate()
之后使用imgView.setImageResource(images[current_image_index]);
来解决“滞后问题”。