0

I have Button which function is to animate one of my ImageView for a second and delete specific table, recreate and insert the default value. Everything's working fine but one of my requirements is to show the animation aleast 1 second even recreating table is done. how can i achieve this?

NOTE: I animate my ImageView with Rotate effect so it will looks like refreshing.

refreshDatabase function

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>

I try to add this,

new Handler().postDelayed(new Runnable() {
           @Override
            public void run() {
                //recreate table here
                refreshing.clearAnimation();
           }
       }, 1000);

but i got an error Handler is abstract; cannot be instantiated.

Anybody can help me? Thank You!!!

bernzkie
  • 1,269
  • 2
  • 16
  • 35

4 Answers4

1

You can use Handler which will wait for a second and then execute clearAnimation method:

 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);

}
Rohit Sharma
  • 2,017
  • 1
  • 20
  • 22
  • I already do it before, but i got this error: `Handler is abstract; cannot be instantiated` – bernzkie Jan 15 '16 at 10:55
  • import android.os.Handler; – Rohit Sharma Jan 15 '16 at 10:59
  • I did use `handler postdelay` and then its not working, then i post a question, then i realize that i forgot `I pressed ALT + ENTER` when i saw the error then `log.handler` is imported instead of `os.handler`. so i change it to `os.handler` and now it's working. Thanks Guys! – bernzkie Jan 15 '16 at 11:10
  • Thanks @Rohit Sharma, i just forgot that i have to choose between `os.handler` and `log.handler` when using it. – bernzkie Jan 15 '16 at 11:11
0

Use a Handler (http://developer.android.com/reference/android/os/Handler.html) to post delay a message or a Runnable in which you call clearAnimation()

0xDEADC0DE
  • 2,453
  • 1
  • 17
  • 22
0

You can set the duration of the animation like the following example in your rotate.xml file

<rotate android:interpolator="@android:anim/accelerate_interpolator" 
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
android:duration="1000" //set this property (1000 is 1second)
android:startOffset="1500"/>

Or you can do it programatically too by using setDuration() .

Community
  • 1
  • 1
nobalG
  • 4,544
  • 3
  • 34
  • 72
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);

try this