簡體   English   中英

如何在不點擊的情況下實現閃屏圖像閃爍?

[英]How to implement the splash screen image blink, without clicking?

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

閃爍.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>

活動飛濺.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>

請建議一種執行動畫的方法

在頁面加載時而不是在單擊圖像時。

onCreate方法中試試這個並評論onClick方法並嘗試下面的代碼,

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

試試下面的代碼:

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>

如果有任何問題,請打擾我。 認為會幫助你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM