2016-03-26 6 views
0

私のアプリケーションにはいくつか問題があります。ボタンを押さずに、時間間隔を使って背景画像を自動的にスライドさせたい 私はいくつかの記事からいくつかの指示に従いますが、ボタンを使用して背景画像をスライドさせます。誰かがコードを編集するのを手伝ってくれるので、ある時間の間に画像が自動的にスライドされますか?ありがとう画像は一定期間、自動的にスライドします

ここに私のコードです。

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

} 

答えて

0

あなたはRESフォルダ 下のアニメーションのフォルダを作成し、このアニメーションを追加し、その後アニメーションフォルダの下に

<?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> 

imgae_animation.xmlに 書き込みを1 imgae_animation.xmlファイルを作成し、 .xmlから画像ビュー

<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

申し訳ありませんが、今開いて、それを見て、問題の解決について私にインスピレーションを与えてくれてありがとう。私はとても感謝しています。 –

関連する問題