有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Android:加载应用程序时添加动画

我有一个动画,每当单击ImageButton时,它都会在ImageButton上产生一个小的“弹出”效果:

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

我如何修复它,以便在加载应用程序时只启动一次动画,以“显示ImageButton是可单击的”

以下是ImageButton代码:

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

共 (2) 个答案

  1. # 1 楼答案

    这很简单。在活动的onStart()函数中显示动画。加载所有资源和视图时调用OnStart()函数

  2. # 2 楼答案

    无法在oncreate中运行动画,因为它尚未附着到视图,因此要运行启动动画,请覆盖此方法

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

    在你的xml动画中试试这个

    android:repeatCount="0"
    

    我不确定是否需要为缩放设置oneshot,但我的一次性启动动画列表如下所示:

    <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文档明确指出,要使用启动动画,您必须覆盖上述方法,而不是ONSTART()或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();
    }
    

    全班如下

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