2016-09-26 59 views
-4

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> 

請建議執行動畫的方式

當頁面加載,而不是點擊圖像時。

回答

0

onCreate方法試試這個,這onClick方法發表評論,並試用一下下面的代碼,

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

它顯示上下文的錯誤。 –

+0

現在工作正常。非常感謝....使用這個--->上下文context = this;對於上下文錯誤... –

+0

您的歡迎。 :)請投票它,如果它可以幫助你,並接受答案:) –

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> 

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