圖像切換器使用ImageSwitcher表示,用于實現類似于Windows操作系統下的“Windows照片查看器”中的上一張、下一張切換圖片的功能。在使用ImageSwitcher時,必須實現ViewSwitcher.ViewFactory接口,并通過makeView()方法創建用于顯示圖片的ImageView對象。makeView()方法將返回一個顯示圖片的ImageView。在使用ImageSwitcher組件時,還有一個非常重要的方法,那就是setImageResource()方法,改方法用于指定在ImageSwitcher中顯示的圖片資源。
第一步:XML布局文件的代碼如下:
<ImageSwitcher android:id="@+id/im" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/button1" android:layout_centerHorizontal="true" android:layout_marginTop="99dp" ></ImageSwitcher><Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="77dp" android:text="上一張" /><Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/button1" android:layout_below="@+id/im" android:layout_marginTop="51dp" android:text="下一張" />
第二步:在Java中編寫邏輯代碼,詳細代碼如下所示:
package com.example.imageswitcher;import android.app.Activity;import android.app.ActionBar;import android.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup;import android.widget.Button;import android.widget.ImageSwitcher;import android.widget.ImageView;import android.widget.ViewSwitcher.ViewFactory;import android.os.Build;public class MainActivity extends Activity { private int[] imageId=new int[]{ R.drawable.bj, R.drawable.bj1, R.drawable.bj11,R.drawable.bj12 }; private int index=0; private ImageSwitcher imageSwitcher; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageSwitcher=(ImageSwitcher) findViewById(R.id.im); imageSwitcher.setInAnimation(this, android.R.anim.fade_in);//設置淡入的動畫 imageSwitcher.setOutAnimation(this ,android.R.anim.fade_out);//設置淡出的動畫 imageSwitcher.setFactory(new ViewFactory() { @Override public View makeView() { ImageView imageView=new ImageView(MainActivity.this);//創建一個Iv 的類 imageView.setAdjustViewBounds(true); imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);//設置保持橫縱比居中縮放圖片 imageView.setLayoutParams(new ImageSwitcher.LayoutParams(240,180)); return imageView; } }); imageSwitcher.setImageResource(imageId[index]);//顯示默認的圖片 //獲取兩個按鈕的ID Button up=(Button) findViewById(R.id.button1); Button down=(Button) findViewById(R.id.button2); up.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if(index>0){ index--; }else{ index =imageId.length-1; } imageSwitcher.setImageResource(imageId[index]);//顯示當前的圖片 } }); down.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if(index < imageId.length-1){ index++; }else{ index=0; } imageSwitcher.setImageResource(imageId[index]);//顯示當前的圖片 } }); }}第三步:用手機運行的結果如下所示:

感謝大家的閱讀,如有錯誤和不足請指出,謝謝大家。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。
新聞熱點
疑難解答