android ImageSwitcher和Gallery的使用

來源:互聯網
上載者:User

標籤:

  android 中ImageSwitcher和Gallery可以用來做出類似投影片的效果,ImageSwticher控制項的功能是用來顯示圖片和設定圖片進齣動畫,通俗點講,ImageSwitcher是一個用來顯示具體圖片的面板(view),對這個面板,可以設定圖片進入時候的動畫,以及離開時候的動畫,Gallery是一個容器,用來儲存在ImageSwticher裡面顯示的具體圖片。

布局檔案:

  <RelativeLayout xmlns:android="http://shemas.android.com/apk/res/android"

          android:layout_width="match_parent"

          android:layout_hegiht="match_paernt">

  <ImageSwticher          <!--  申明ImageSwitcher控制項,用來顯示圖片,有點像圖片顯示器-->

      android:id="@+id/imgs"

      android:layout_width="fill_parent"

      android:layout_height="fill_parent"

      android:layout_alignParentTop="true"    <!--設定控制項和父布局頂端和左面對齊-->

      android:layout_alignParentLeft="true"/>

  <Gallery

    android:id="@+id/gallery"

    android:layout_width="fill_parent"

    android:layout_hegiht="100dp"

    android:layout_alignParentBottom="true"    <!-- 設定控制項和父布局底部和靠左對齊->

    android:layout_alignParentLeft="true"

    android:gravity="center_vertical"

    android:spacing="6dp"/>        <!--設定容器裡面各個部分之間的間隔距離-->

  </RelativeLayout>

 

android代碼簡介:

    public class MainActivity extends Activity implements android.widget.ViewSwitcher.ViewFactory,android.widget.AdapterView.OnItemSelectedListener

    {

        private ImageSwitcher imgSwitcher;

        private Gallery gallery;

        private Integer imgs[] ={R.drawable.a,R.drawable.b,R.drawable.c,R.drawable.d,R.drawabe.e,R.drawable.f,R.drawable.g};//這裡添加自己的圖片

        protected void onCreate(Bundle savedInstanceState)

        {

            super.onCreate(savedInstanceState);

            setContentView(R.layout.mainlayout);//mainlayout為上面定義的布局檔案

            imgSwitcher = (ImageSwitcher)findViewById(R.id.imgs);

            gallery  = (Gallery)findViewById(R.id.gallery);  

            imgSwitcher.setInAnimation(this,android.R.anim.fade_in);//設定圖片進入動畫

            imgSwitcher.setOutAnimation(this,android.R.anim.fade_out);//設定圖片退齣動畫

            imgSwither.setFactory(this);//設定視圖工廠,不知道這個視圖工廠是幹嘛用的!好像和makeView對應

            GalleryAdapter adapter = new GalleryAdaper(this);//這個自訂配接器主要用來設定Gallery容器裡面的資料

            gallery.setOnItemSelectedListener(this);  

            gallery.setAdapter(adapter);

            

        }

 

        @Override

        public void onItemSelected(AdapterView<?> parent,View view,int position,int id)

        {

            imgSwitcher.setImageResource(imgs[position]);//根據Gallery的選擇,在ImageSwitcher裡面顯示對應的圖片

        }

        @Override

        public void onNothingSelected(AdapterView<?> parent)

        {

        }

        @Override

        public View makeView()

        {    

            ImageView view = new ImageView(this);

            view.setBackgroundColor(0xff000000);

            view.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));

            view.setScaleType(ImageSwitcher.ScaleType.FIT_CENTER);

            return view;

        }

        class GalleryAdapter extends BaseAdapter

        {

            private Context context;  

            GalleryAdaper(Context c)

            {

              context = c;

            }

            public int getCount()

            {

              return imgs.length;

            }

            public Object getItem(int position)

            {      

              return imgs[position];

            }

            public long getItemId(int position)

            {

                return position;

            }

            public View getView(int position,View view,ViewGroup groupview)

            {

                ImageView v  = new ImageView(context);

                v.setImageResource(imgs[position]);

                v.setAdjustViewBounds(true)//設定允許調整圖片大小,這裡設定它的原因是:布局檔案裡面對Gallery高度的設定是:

                /*android:layout_height=""100dp*,這時候使用的圖片資源高度可以比它大或者小,加上這個屬性,就可以讓Gallery自動調整圖片的大小/

                v.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));

                return v;

            }

        }

    }

android ImageSwitcher和Gallery的使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.