2017-01-09 4 views
1

Я новичок в android.ANDROID заставка с двумя затухающими изображениями

Мое сомнение в том, как я могу создать заставку с двумя разными фонами с эффектом замирания друг к другу, после чего я хочу перейти к следующему действию.

У меня есть googled. Но я не могу найти решение для этого. Мой код приведен ниже.

SplashActivity.java

public class SplashActivity extends Activity { 
    LinearLayout splashLayout; 
    protected Animation fadeIn,fadeOut; 
    protected ImageView img1; 
    protected ImageView img2; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.splash_activity); 
     img1 = (ImageView) findViewById(R.id.imageView); 
     img2 = (ImageView) findViewById(R.id.imageView2); 
     splashLayout = (LinearLayout) findViewById(R.id.splashDrawer); 
     splashLayout.setBackgroundResource(R.drawable.slidetwo); 
     fadeIn = AnimationUtils.loadAnimation(SplashActivity.this, R.anim.fade_in); 
     fadeOut = AnimationUtils.loadAnimation(SplashActivity.this, R.anim.fade_out); 
     //img2.setVisibility(View.VISIBLE); 
     splashLayout.startAnimation(fadeIn); 


     //splashLayout.setBackgroundResource(R.drawable.slidetwo); 
     new Handler().postDelayed(new Runnable() { 
      @Override 
      public void run() { 
       Intent intent = new Intent(SplashActivity.this, 
         SigninActivity.class); 
       startActivity(intent); 
       splashLayout.setBackgroundResource(R.drawable.slideone); 
       //img2.setVisibility(View.GONE); 
       //img2.startAnimation(fadeOut); 
       img1.setVisibility(View.VISIBLE); 
       img1.startAnimation(fadeIn); 
       SplashActivity.this.finish(); 

       overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out); 

      } 
     }, Constants.SPLASH_TIME_OUT); 
    } 
} 

splash_activity.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#FFFFFF" 
    android:gravity="center_vertical" 
    android:orientation="vertical" 
    android:id="@+id/splashDrawer" > 

     <ImageView 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:id="@+id/imageView" 
      android:src="@drawable/slideone" 
      android:visibility="gone" /> 

     <ImageView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:id="@+id/imageView2" 
      android:src="@drawable/slidetwo" 
      android:visibility="gone" /> 
</LinearLayout> 

fade_in.xml

<set xmlns:android="http://schemas.android.com/apk/res/android" 
    android:fillAfter="true"> 
    <alpha android:fromAlpha="0.0" 
     android:toAlpha="1.0" 
     android:duration="5000"/> <!--//Time in milliseconds--> 
</set> 

Fade_out.xml

<set xmlns:android="http://schemas.android.com/apk/res/android" 
    android:fillAfter="true"> 
    <alpha android:fromAlpha="1.0" 
     android:toAlpha="0.0" 
     android:duration="5000"/><!-- //Time in milliseconds--> 
</set> 

push_left_in.xml

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android"> 
    <translate 
     android:duration="500" 
     android:fromXDelta="100%p" 
     android:toXDelta="0" /> 

</set> 

push_left_out.xml

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android"> 
    <translate 
     android:duration="500" 
     android:fromXDelta="0" 
     android:toXDelta="-100%p" /> 
</set> 

Это прекрасно работает для первого фонового изображения, но изображение точно не подходит для экрана. Но прежде чем менять фон, сначала перейдите меня к signinActivity

Требование моих клиентов состоит в том, чтобы отображать каждый фон с 2-секундным дисплеем.

+0

Что происходит сейчас с этим кодом? –

+0

вы можете использовать GIF для этого, это не лучше appproach –

+0

@VishalPatoliya ツ У меня есть только два изображения, как я могу сделать GIF из этого? –

ответ

0

Не уверен, но вы можете попробовать это

new Handler().postDelayed(new Runnable() { 
       @Override 
       public void run() { 

       img1.setAnimation(inFromRightAnimation()); 
       img1.setBackground(R.Drawable.YourSecondImageId); 

      //splashLayout.setBackgroundResource(R.drawable.slidetwo); 
      new Handler().postDelayed(new Runnable() { 
       @Override 
       public void run() { 
        Intent intent = new Intent(SplashActivity.this, 
          SigninActivity.class); 
        startActivity(intent); 
        splashLayout.setBackgroundResource(R.drawable.slideone); 
        //img2.setVisibility(View.GONE); 
        //img2.startAnimation(fadeOut); 
        img1.setVisibility(View.VISIBLE); 
        img1.startAnimation(fadeIn); 
        SplashActivity.this.finish(); 

        overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out); 

        } 
       }, Constants.SPLASH_TIME_OUT); 


       } 
      }, Constants.TIME_TO_CHANGE_FIRST_IMAGE); 

И

public Animation inFromRightAnimation() 
    { 
     Animation inFromRight = new TranslateAnimation(
       Animation.RELATIVE_TO_PARENT, +1.0f, 
       Animation.RELATIVE_TO_PARENT, 0.0f, 
       Animation.RELATIVE_TO_PARENT, 0.0f, 
       Animation.RELATIVE_TO_PARENT, 0.0f); 
     inFromRight.setDuration(240); 
     inFromRight.setInterpolator(new AccelerateInterpolator()); 
     return inFromRight; 
    } 

Вы должны управлять временной промежуток между двумя изображениями и Всплеск Сообщите мне, если не работает

+0

Я получаю ошибку на 'img1.setBackground (R.Drawable.YourSecondImageId);' эта строка, пожалуйста, взгляните на это –

+0

.setBackground (R.drawable.card_background); Здесь card_background - это имя изображения, которое является следующим изображением, которое вы хотите показать, которое вы можете заменить другим изображением, которое вы хотите: –

+0

заменить img1.setBackground (R.Drawable.YourSecondImageId); это с img1.setBackgroundResource (R.drawable.slidetwo); этот –

0

Его так просто. В вашем onStart. Используя это, вы увидите анимацию выцветания на активность всплеска. Нет необходимости использовать анимацию. overridePendingTransition может сделать то же самое.

@Override 
protected void onStart() { 
    super.onStart(); 
    overridePendingTransition(0, R.anim.splashfadeout); 
} 

и в обработчике использовать разницу во времени Set this-

new Handler().postDelayed(new Runnable() { 
     @Override 
     public void run() { 
      fadeOutAndHideImage(img); 
     } 
    }, Constants.IMAGE_TIME_OUT); 

//handler for switching to other activity 
new Handler().postDelayed(new Runnable() { 
     @Override 
     public void run() { 
      Intent intent = new Intent(SplashActivity.this, 
        SigninActivity.class); 
      startActivity(intent); 
     } 
    }, Constants.SPLASH_TIME_OUT); 


    private void fadeOutAndHideImage(final ImageView img) 
    { 
    Animation fadeOut = new AlphaAnimation(1, 0); 
    fadeOut.setInterpolator(new AccelerateInterpolator()); 
    fadeOut.setDuration(1000); 

    fadeOut.setAnimationListener(new AnimationListener() 
    { 
     public void onAnimationEnd(Animation animation) 
     { 
       img.setVisibility(View.GONE); 
     } 
     public void onAnimationRepeat(Animation animation) {} 
     public void onAnimationStart(Animation animation) {} 
}); 

img.startAnimation(fadeOut); 
} 

между обоими обработчиками.

splashfadeout.xml

<?xml version="1.0" encoding="utf-8"?> 
<alpha 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:duration="750" 
android:fromAlpha="1.0" 
android:interpolator="@android:interpolator/accelerate_cubic" 
android:startOffset="250" 
android:toAlpha="0.0" 
android:zAdjustment="top" /> 
+0

спасибо, позвольте мне попробовать .. –

+0

@KaranadeepAtwal Это не работает. Я получаю только белый экран –