首頁  >  文章  >  後端開發  >  Android UI控制系列:Gallery(圖庫視圖)

Android UI控制系列:Gallery(圖庫視圖)

黄舟
黄舟原創
2017-01-19 09:56:241510瀏覽

Gallery能夠水平顯示其內容,一般用來瀏覽圖片,被選中的選項位於中間,並且可以相應事件顯示資訊。下面結合ImageSwitcher組件來實現一個透過縮圖來瀏覽圖片的程序,具體步驟如下

第一步:

創建一個Andorid工程”GalleryTest”,該工程的入口是Activity類GalleryTest繼承Activity並實現OnItemySelectedListener和ViewFactorFactorySelectedListener和ViewFactorySelected接口,來實現圖片和視圖的創建

package org.hualang.Gallery;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ViewSwitcher.ViewFactory;
//继承Activity,实现onItemSelectedListener和ViewFactory接口
public class GalleryTest extends Activity implements OnItemSelectedListener,ViewFactory{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

        @Override
        public View makeView() {
                // TODO Auto-generated method stub
                return null;
        }

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
                        long arg3) {
                // TODO Auto-generated method stub

        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

        }
}

第二步:

在工程的resdrawable目錄下添加7張圖片和對應的縮圖

第三步:

在工程佈局文件main.xml,在其中那個新增一個Gallery元件和一個ImageSwitcher元件,並設定對應的屬性

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:orientation="vertical"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"  
    >  
<ImageSwitcher android:id="@+id/switcher"  
        android:layout_width="fill_parent"  
        android:layout_height="fill_parent"  
        android:layout_alignParentTop="true"  
        android:layout_alignParentLeft="true"  
    />  
  
    <Gallery android:id="@+id/gallery"  
        android:background="#55000000"  
        android:layout_width="fill_parent"  
        android:layout_height="60dp"  
        android:layout_alignParentBottom="true"  
        android:layout_alignParentLeft="true"  
        android:gravity="center_vertical"  
        android:spacing="16dp"  
    />  
</LinearLayout>

第四步:在GalleryTest頂端宣告所使用的ImageSwitcher實例圖片資源Integer陣列

public class GalleryTest extends Activity implements OnItemSelectedListener,ViewFactory{
    /** Called when the activity is first created. */
        //声明ImageSwitcher
        private ImageSwitcher switcher;
        //缩略图片id数组
        private Integer[] thumbids={
                        R.drawable.thumb0,
                        R.drawable.thumb1,
                        R.drawable.thumb2,
                        R.drawable.thumb3,
                        R.drawable.thumb4,
                        R.drawable.thumb5,
                        R.drawable.thumb6,
                        R.drawable.thumb7
        };
        //图片id数组
        private Integer[] imgids={
                        R.drawable.img0,
                        R.drawable.img1,
                        R.drawable.img2,
                        R.drawable.img3,
                        R.drawable.img4,
                        R.drawable.img5,
                        R.drawable.img6,
                        R.drawable.img7
        };

第五步:
在GalleryTest的onCreate()方法中,將視窗樣式設定為無標題,設定目前版面視圖,取得ImageSwitcher實例,並設定漸進式漸進式動畫,取得Gallery實例

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置窗口特征无标题
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);
        //通过findViewById方法获得ImageSwitcher对象
        switcher=(ImageSwitcher)findViewById(R.id.switcher);
        //为ImageSwitcher设置工厂
        switcher.setFactory(this);
        //设置动画渐入效果
        switcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
        //设置动画渐出效果
        switcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));
        //通过findViewById方法获得Gallery对象
        Gallery g=(Gallery)findViewById(R.id.gallery);
    }

第六步:


建立內部類別ImageAdapter ,此類別繼承BaseAdapter,為Gallery設定Adapter實例

public class ImageAdapter extends BaseAdapter {
            //构造方法
                public ImageAdapter(Context c) {
                        mContext = c;
                }
                //获得数量
                public int getCount() {
                        return thumbids.length;
                }
                //获得当前选项
                public Object getItem(int position) {
                        return position;
                }
                //获得当前选项ID
                public long getItemId(int position) {
                        return position;
                }
                //获得View对象
                public View getView(int position, View convertView, ViewGroup parent) {
                        //实例化ImageView对象
                        ImageView i = new ImageView(mContext);
                        //设置缩略图片资源
                        i.setImageResource(thumbids[position]);
                        //设置边界对齐
                        i.setAdjustViewBounds(true);
                        //设置布局参数
                        i.setLayoutParams(new Gallery.LayoutParams(
                                        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                        //设置背景资源
                        i.setBackgroundResource(R.drawable.picturefrom);
                        return i;
                }
                private Context mContext;
        }

第七步:


實作onItemSelected()方法,更換圖片

@Override  
        public void onItemSelected(AdapterView<?> adapter, View v, int position,  
                        long id) {  
                switcher.setImageResource(imgids[position]);  
        }

第八步:


實作makeView(

@Override  
        public View makeView() {  
                // TODO Auto-generated method stub  
                //创建ImageView  
                ImageView i=new ImageView(this);  
                //设置背景颜色  
                i.setBackgroundColor(0xFF000000);  
                //设置精度类型  
                i.setScaleType(ImageView.ScaleType.FIT_CENTER);  
                //设置布局参数  
                i.setLayoutParams(new ImageSwitcher.LayoutParams(  
                                LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));  
                return i;  
        }

第九步:

為Gallery加入Adapter並加入OnItemSelectedListener監聽器

g.setAdapter(new ImageAdapter(this));  
                g.setOnItemSelectedListener(this);

至此,全部,結束,運行結果如下

(畫廊視圖)的內容,更多相關內容請關注PHP中文網(www.php.cn)! Android UI控制系列:Gallery(圖庫視圖)

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn