Android - Gallery

具體步奏:

1、建立新項目ActivityBasicActivity

2、把圖片拷貝到res/drawable目錄

3、在res/values目錄中新建一個attrs.xml內容



這裏我是用Framelayout來實現疊加效果,使用ImageView來顯示大圖,Gallery來展示畫廊,Toast類時用來顯示圖像名稱

main.xml代碼:

<?xml version="1.0" encoding="utf-8"?>
      
            <FrameLayout
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/frameLayout1"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" >
                
                <ImageView
                android:id="@+id/imageView1"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:src="@drawable/p1"
                 />
                
                 <Gallery
                android:id="@+id/gallery1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" 
                android:spacing="5dp"/>
                 
                
            </FrameLayout>


attrs.xml代碼:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="Gallery1">
        <attr name="android:galleryItemBackground"></attr>
    </declare-styleable>
</resources>

這部


Java代碼:

package com.demo;

import android.app.Activity;
import android.os.Bundle;

import android.view.View;
import android.view.ViewGroup;
import android.widget.Gallery;
import android.widget.Toast;
import android.widget.ImageView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.content.Context;
import android.content.res.TypedArray;

public class ActivityBasicActivity extends Activity {
	///要顯示的圖像,圖片數組
	Integer[] images = {
			R.drawable.p1,
			R.drawable.p2,
			R.drawable.p3,
			R.drawable.p4,
			R.drawable.p5
	};
	
    /** 當第一次被創建時調用 */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);//創建一個線性佈局管理器
        setContentView(R.layout.main);  //顯示該視圖
        
        ///定義UI組件
        ///final 如果修飾類,該類不能被繼承;如果修飾變量,該變量不能被改變,就是不能再重新賦值;
        ///如果修飾方法,這個方法不能被重寫。
        final ImageView iv = (ImageView)findViewById(R.id.imageView1);
        Gallery gallery = (Gallery) findViewById(R.id.gallery1);///獲取id傳值給gallery
        
        ///設置圖片匹配器
        gallery.setAdapter(new ImageAdapter(this));
        
        ///設置AdapterView點擊監聽器,Gallery是AdapterView的子類
        gallery.setOnItemClickListener(new OnItemClickListener(){
        	
        	
        	public void onItemClick(AdapterView<?> parent, View v, int position, long id)
        	{
        		///顯示點擊的是第幾張圖片
        		Toast.makeText(getBaseContext(), "第"+(position+1)+"張", Toast.LENGTH_SHORT).show();
        		///設置背景部分的ImageView顯示當前的Item的圖片
        		iv.setImageResource(((ImageView)v).getId());
        		//iv.setImageResource(images[position]);
        	}
        });
    }
    
    public class ImageAdapter extends BaseAdapter{
    	
    	///Item的修飾背景
    	int myGalleryItemBackground;
    	
    	///上下文對象
    	private Context myContext;
    	
    	///構造方法
    	public ImageAdapter(Context c){
    		myContext = c;
    		////讀取styleable資源,設定樣式
    	TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
    	myGalleryItemBackground = a.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0);
    	a.recycle();
    	}
    	
    	///返回相片數組的數量
    	public int getCount(){
    		return images.length;
    	}
    	
    	///返回項目
    	public Object getItem(int position){
    		return position;
    	}
    	
    	///返回項目id
    	public long getItemId(int position){
    		return position;
    	}
    	
    	///返回視圖
    	public View getView(int position, View convertView, ViewGroup parent){
    		ImageView iv = new ImageView(myContext);
    		iv.setImageResource(images[position]);
    		//給生成的ImageView設置id,不設置的話id都是-1
    		iv.setId(images[position]);
    		iv.setLayoutParams(new Gallery.LayoutParams(120,160));
    		iv.setScaleType(ImageView.ScaleType.FIT_XY);
    		iv.setBackgroundResource(myGalleryItemBackground);
    		return iv;
    	}
    }
    
   
}

這裏提供一個小擴展,AdapterView的繼承關係



發佈了36 篇原創文章 · 獲贊 4 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章