如何实现启动画面图像闪烁,而不点击?

时间:2016-09-26 10:59:35

标签: java android

SplashActivity.java

public class SplashActivity extends AppCompatActivity {

    private static int SPLASH_TIME_OUT = 3000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        ImageView imageView = (ImageView) findViewById(R.id.imageView);
        imageView.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                blink(v);
                //I am calling the animation only when the image is clicked.
                //I want to perform animation as the page loads.
                //Not on clicking the image.
            }

        });
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent i = new Intent(SplashActivity.this, LoginOptionsActivity.class);
                startActivity(i);
                finish();
            }
        }, SPLASH_TIME_OUT);
    }

    public void blink(View view){
        ImageView image = (ImageView)findViewById(R.id.imageView);
        Animation animation1 = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.blink);
        image.startAnimation(animation1);
    }
}

blink.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha android:fromAlpha="0.0"
    android:toAlpha="1.0"
    android:interpolator="@android:anim/accelerate_interpolator"
    android:duration="600"
    android:repeatMode="reverse"
    android:repeatCount="infinite"/>
</set>

activity_splash.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.codeinks.luxehues.SplashActivity"
    android:background="@drawable/splash_shirt">
<ImageView
    android:layout_width="300dp"
    android:layout_height="150dp"
    android:id="@+id/imageView"
    android:src="@drawable/fullsize"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true" />
</RelativeLayout>

请建议一种执行动画的方法

当页面加载而不是单击图像时。

2 个答案:

答案 0 :(得分:0)

onCreate方法中尝试此操作,并对onClick方法发表评论并尝试使用以下代码,

ImageView imageView = (ImageView) findViewById(R.id.imageView);
Animation myAnim = AnimationUtils.loadAnimation(context,R.anim.blink);
imageView.startAnimation(myAnim);

答案 1 :(得分:0)

尝试以下代码:

Thread splashTread;

public void onAttachedToWindow() {
    super.onAttachedToWindow();
    Window window = getWindow();
    window.setFormat(PixelFormat.RGBA_8888);
}

@Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash);
    StartAnimations();

    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
}

private void StartAnimations() {
    Animation anim = AnimationUtils.loadAnimation(this, R.anim.alpha);
    anim.reset();
    LinearLayout l = (LinearLayout) findViewById(R.id.layoutAnimation);
    l.clearAnimation();
    l.startAnimation(anim);

    anim = AnimationUtils.loadAnimation(this, R.anim.blink);
    anim.reset();
    ImageView iv = (ImageView) findViewById(R.id.imageSplash);
    iv.clearAnimation();
    iv.startAnimation(anim);

    splashTread = new Thread() {
        @Override
        public void run() {
            try {
                int waited = 0;
                // Splash screen pause time
                while (waited < 3000) {
                    sleep(100);
                    waited += 100;
                }
                Intent intent = new Intent(SplashActivity.this, StartActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                startActivity(intent);
            } catch (InterruptedException e) {
                // do nothing
            } finally {
                SplashActivity.this.finish();
            }
        }
    };
    splashTread.start();
}

和相应的xml布局是:

<!-- activity_splash.xml -->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/layoutAnimation"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activities.SplashActivity">

    <ImageView
        android:id="@+id/imageSplash"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:contentDescription="@string/splash"
        android:scaleType="fitXY"
        android:src="@drawable/splash" />

</LinearLayout>

如果有任何问题,请打扰我。认为会帮助你。