我有Button
,其功能是为我的ImageView
之一制作一秒钟delete specific table, recreate and insert the default value
。一切都工作正常,但我的要求之一是显示动画aleast 1秒甚至重新创建表已完成。我怎么能做到这一点?
注意:我使用旋转效果为ImageView
设置动画,这样看起来就像刷新一样。
refreshDatabase功能
public void refreshDatabase(View v){
Button btnRefresh = (Button)findViewById(R.id.btnRef);
ImageView refreshing = (ImageView) findViewById(R.id.rfs);
btnRefresh.setClickable(false);
btnRefresh.setEnabled(false);
Animation animation = AnimationUtils.loadAnimation(this, R.anim.rotate);
//I want to start animation here
refreshing.startAnimation(animation);
....recreat table here
//Stop animation after 1 second
refreshing.clearAnimation();
}
rotate.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false" >
<rotate
android:duration="300"
android:interpolator="@android:anim/linear_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="infinite"
android:repeatMode="restart"
android:toDegrees="360" />
</set>
我尝试添加此内容,
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
//recreate table here
refreshing.clearAnimation();
}
}, 1000);
但我收到了错误Handler is abstract; cannot be instantiated
。
任何人都可以帮助我吗?谢谢!!!
答案 0 :(得分:1)
您可以使用等待一秒钟的Handler,然后执行clearAnimation方法:
public void refreshDatabase(View v){
Button btnRefresh = (Button)findViewById(R.id.btnRef);
ImageView refreshing = (ImageView) findViewById(R.id.rfs);
btnRefresh.setClickable(false);
btnRefresh.setEnabled(false);
Animation animation = AnimationUtils.loadAnimation(this, R.anim.rotate);
//I want to start animation here
refreshing.startAnimation(animation);
....recreat table here
//Stop animation after 1 second
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
refreshing.clearAnimation();
}
}, 1000);
}
答案 1 :(得分:0)
使用Handler
(http://developer.android.com/reference/android/os/Handler.html)发送消息延迟或Runnable
clearAnimation()
答案 2 :(得分:0)
答案 3 :(得分:0)
ImageView iv_follow_insta = findViewById(R.id.iv_follow_insta);
Animation zoomInanimation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.dialog_zoomin);
Animation zoomOutanimation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.dialog_zoomout);
zoomOutanimation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation arg0) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationRepeat(Animation arg0) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationEnd(Animation arg0) {
iv_follow_insta.startAnimation(zoomInanimation);
}
});
zoomInanimation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation arg0) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationRepeat(Animation arg0) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationEnd(Animation arg0) {
iv_follow_insta.startAnimation(zoomOutanimation);
}
});
iv_follow_insta.startAnimation(zoomInanimation);
iv_follow_insta.startAnimation(zoomOutanimation);
new Handler().postDelayed(() -> {
zoomInanimation.setAnimationListener(null);
zoomOutanimation.setAnimationListener(null);
iv_follow_insta.clearAnimation();
}, 10000);
尝试