2016-03-26 1 views
0

J'ai un problème avec mon application. Je veux faire glisser automatiquement mon image d'arrière-plan sans appuyer sur aucun bouton mais en utilisant un intervalle de temps. Je suis quelques instructions de certains articles, mais ils font simplement glisser l'image de fond en utilisant le bouton. Est-ce que quelqu'un peut m'aider à modifier le code pour que l'image glisse automatiquement pendant un certain temps? MerciGlisser une image automatiquement pendant un certain temps

Voici mon code.

code Java:

int image[] = {R.drawable.backgroundtwo, R.drawable.backgroundthree, R.drawable.backgroundfour}; 

ImageSwitcher imageSwitcher; 
Button button; 
Animation slide_in_left, slide_out_right; 
int curIndex; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_homepage); 

    imageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher); 
    button = (Button) findViewById(R.id.button); 

    slide_in_left = AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left); 
    slide_out_right = AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right); 

    imageSwitcher.setInAnimation(slide_in_left); 
    imageSwitcher.setOutAnimation(slide_out_right); 

    imageSwitcher.setFactory(new ViewSwitcher.ViewFactory() { 
     @Override 
     public View makeView() { 

      ImageView imageView = new ImageView(Homepage.this); 
      imageView.setScaleType(ImageView.ScaleType.FIT_CENTER); 

      ViewGroup.LayoutParams params = new ImageSwitcher.LayoutParams(
        ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT 
      ); 

      imageView.setLayoutParams(params); 
      return imageView; 
     } 
    }); 

    curIndex = 0; 
    imageSwitcher.setImageResource(image[curIndex]); 

    button.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      if(curIndex == image.length - 1){ 
       curIndex = 0; 
       imageSwitcher.setImageResource(image[curIndex]); 
      } else { 
       imageSwitcher.setImageResource(image[++curIndex]); 
      } 
     } 
    }); 

} 

Répondre

0

Vous créez dossier anim sous dossier res et de créer un fichier imgae_animation.xml sous dossier anim écrire sur imgae_animation.xml

<?xml version="1.0" encoding="utf-8"?> 
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" 
android:oneshot="false" > 

<item 
android:drawable="@drawable/img1" 
android:duration="120"/> 
<item 
android:drawable="@drawable/img2" 
android:duration="120"/> 
<item 
android:drawable="@drawable/img3" 
android:duration="120"/> 
<item 
</animation-list> 

puis ajoutez ce anim sous .xml à imageview

<ImageView 
    android:id="@+id/img_home_list" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="5dp" 
    android:background="@null" 
    android:scaleType="fitXY" 
    android:adjustViewBounds="true" 
    android:contentDescription="@null" 
    android:src="@anim/imgae_animation" 
    /> 
+0

désolé mais je viens ouvrez-le et voyez-le maintenant, merci de me donner une inspiration pour résoudre le problème. J'apprécie beaucoup. –