Question

I have this animation that makes a small "pop up" effect on a ImageButton whenever it is clicked:

<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXScale="0.9"
android:toXScale="1.05"
android:fromYScale="0.9"
android:toYScale="1.05"
android:pivotX="50%"
android:pivotY="50%"
android:duration="180"
/>

How can i fix it so that when the app is loaded the animation is launched once, just to "show that the ImageButton is clickable"?

Here is the ImageButton code:

    final ImageButton a = (ImageButton) findViewById(R.id.imageNew);
    a.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {
        Animation anim = AnimationUtils.loadAnimation(MainActivity.this, R.anim.animation);
        a.startAnimation(anim);

        Intent intent = new Intent(MainActivity.this,NewActivity.class);
        startActivity(intent);
    }
});
Was it helpful?

Solution

you cant run an animation in oncreate as it is not yet attached to the view, so to run a start up animation override this method.

  @Override
            public void onWindowFocusChanged(boolean hasFocus) {
                RunAnimations();
                super.onWindowFocusChanged(hasFocus);
            }

try this in you xml anim

android:repeatCount="0"

im not sure if you need to set oneshot for scaling but my onetime startup animation list looks like this:

<animation-list xmlns:android="http://schemas.android.com/apk/res/android" 
    android:oneshot="true">

    <item android:drawable="@drawable/artsun" android:duration="100"/>
   <item android:drawable="@drawable/artsun2" android:duration="100"/>
   <item android:drawable="@drawable/artsun3" android:duration="100"/>
   <item android:drawable="@drawable/artsun4" android:duration="100"/>


</animation-list>

android documentation clearly states to use a startup animation you override the above method, not ONSTART() or ONCREATE()!

@Override
            public void onWindowFocusChanged(boolean hasFocus) {
                RunAnimations();
                super.onWindowFocusChanged(hasFocus);
            }                

                                                         can go anywhere in your main activity, then just create the run animations method and in it put anything you want to start              

    private void RunAnimations() {


   Animation   a = AnimationUtils.loadAnimation(this, R.anim.fade);
  /*  a.reset();
    logoImage = (ImageView) findViewById(R.id.dopescrawl);
    logoImage.setBackgroundResource(R.drawable.dopesplash);
    logoAnimation = (AnimationDrawable)  logoImage.getBackground();
    logoImage.clearAnimation();
    logoImage.startAnimation(a);
    */
    a = AnimationUtils.loadAnimation(this, R.anim.slide);
    a.reset();
    ImageView title = (ImageView) findViewById(R.id.dopescrawl);
    title.clearAnimation();
    title.startAnimation(a);





    //logoAnimation.start();
}

whole class as follows

public class Splash extends Activity {
    /** Called when the activity is first created. */
//MediaPlayer mpSplash;
    AnimationDrawable logoAnimation;
     ImageView logoImage;
ProgressBar  progressBar1;
View ticker;
ImageView gplay;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.splash);
        progressBar1=(ProgressBar)findViewById(R.id.progressBar1);
        progressBar1.setVisibility(View.INVISIBLE);
        gplay=(ImageView) findViewById(R.id.gplay);
        ticker = (View) findViewById(R.id.ticker);

        ticker.setFocusable(true);
        ticker.requestFocus();
        ticker.setSelected(true);
        this.gplay.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=com.gmaninc.dopewars"));
                startActivity(browserIntent);
            }
        });


    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        //mpSplash.release();
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        //mpSplash.pause();
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        //mpSplash.start();
    }

    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
    }

    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        RunAnimations();
        super.onWindowFocusChanged(hasFocus);
    }
    private void RunAnimations() {


       Animation   a = AnimationUtils.loadAnimation(this, R.anim.fade);
      /*  a.reset();
        logoImage = (ImageView) findViewById(R.id.dopescrawl);
        logoImage.setBackgroundResource(R.drawable.dopesplash);
        logoAnimation = (AnimationDrawable)  logoImage.getBackground();
        logoImage.clearAnimation();
        logoImage.startAnimation(a);
        */
        a = AnimationUtils.loadAnimation(this, R.anim.slide);
        a.reset();
        ImageView title = (ImageView) findViewById(R.id.dopescrawl);
        title.clearAnimation();
        title.startAnimation(a);





        //logoAnimation.start();
    }
 @Override
public boolean onTouchEvent(MotionEvent event) {
    progressBar1.setVisibility(View.VISIBLE);
    progressBar1.setIndeterminate(true);
            startActivity(new Intent("com.gmaninc.dopewarsfree.MG"));

    return super.onTouchEvent(event);
}
    }

OTHER TIPS

its very simple. Show the animation in your onStart() function of activity. OnStart() function is called upon loading all resources and views.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top