1. 程式人生 > >UI組件之AdapterView及其子類(四)Gallery畫廊控件使用

UI組件之AdapterView及其子類(四)Gallery畫廊控件使用

convert cal instance ram scaletype 循環 reat targe 外觀

聽說 Gallery如今已經不使用了,API使用ViewPaper取代了,以後再學專研ViewPaper吧如今說說Gallery畫廊,就是不停顯示圖片的意思

Gallery是用來水平滾動的顯示一系列項目。Gallery組件能夠橫向顯示一個圖像列表,當單擊當前圖像的後一個圖像時,這個圖像列表會向左移動一格,當單擊當前圖像的前一個圖像時,這個圖像列表會向右移動一樣

也能夠通過拖動的方式來向左和向右移動圖像列表在使用Gallery的時候,我們應指定他的背景。不然它的項目會緊湊的貼在一起。不會產生畫廊的效果了。可是。你也能夠通過指定Gallery的屬性來設置距離。高度等參數來產生畫廊的效果。

Gallery的xml屬性:


技術分享

能夠看到僅僅有4個屬性:

animationDuration:當布局已經改變,設置一個過渡動畫應該執行多長時間(以毫秒為單位)。
gravity:項目的位置,對齊方式
spacing:設置項目之間間距,圖片之間的間距
unselectedAlpha:設置沒有選擇時的Alpha,沒有選中圖片的透明度


一個簡單的樣例,使用一個Gallery,一個ImageView顯示選中的圖片。

方法是:Gallery既然AdapterView的子類當然是能夠使用Adapter來提供列表項的。當點擊列表項時。ImageView顯示圖片

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="320dp"
        android:layout_height="320dp"
        android:layout_gravity="center_horizontal"
        android:src="@drawable/baiyang" />
<!-- android:unselectedAlpha設置沒選中圖片的透明度 android:spacing設置圖片之間間隔  -->
    <Gallery
        android:id="@+id/gallery1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:spacing="4dp"
        android:unselectedAlpha="0.6" />
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity {
      int[] imagesids=new int[]{
    	R.drawable.p1,	R.drawable.p2,R.drawable.p3,R.drawable.p4,
    	R.drawable.p5,R.drawable.p6,R.drawable.p7,R.drawable.p8
      };
      Gallery g;
      ImageView image;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        g=(Gallery) findViewById(R.id.gallery1);
        image=(ImageView) findViewById(R.id.imageView1);
        
        BaseAdapter ba=new BaseAdapter(){

			@Override
			public int getCount() {
				// TODO Auto-generated method stub
				return imagesids.length;
			}

			@Override
			public Object getItem(int position) {
				// TODO Auto-generated method stub
				return position;
			}

			@Override
			public long getItemId(int position) {
				// TODO Auto-generated method stub
				return position;
			}
            //設置子選項ImageView的外觀
			@Override
			public View getView(int position, View convertView, ViewGroup parent) {
				// TODO Auto-generated method stub
				 ImageView imageview= new ImageView(MainActivity.this);
				 imageview.setImageResource(imagesids[position]);
				 
				 imageview.setScaleType(ImageView.ScaleType.FIT_XY);
				 imageview.setLayoutParams(new Gallery.LayoutParams(160,200));
								
				return imageview;
			}
        	
        };
        //設置adapter
        g.setAdapter(ba);
        //設置選項被選監聽器
        g.setOnItemSelectedListener(new OnItemSelectedListener(){

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				
				image.setImageResource(imagesids[position]);
				
			}

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


看了幾篇好的博客,能夠實現循環Gallery,3DGallery的效果,先放一下。如今時間緊沒有時間專研。如今留著以後學習

http://blog.csdn.net/loongggdroid/article/details/7581236

http://blog.csdn.net/herryz/article/details/6141957

http://blog.csdn.net/easyer2012/article/details/8244483

UI組件之AdapterView及其子類(四)Gallery畫廊控件使用