TQC-106 設定Gallery

TQC-106 設定Gallery

這是TQC的106題,

程式碼為自行撰寫,

僅供參考。

 

最主要練習設定Gallery,

因為要upload圖檔,

所以程式執行時要等待upload完成

 


package COM.TQC.GDD01;

import android.app.Activity;
import android.os.Bundle;
/*本範例需使用到的class*/
import android.content.Context;
import android.content.res.TypedArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class GDD01 extends Activity 
{
	Gallery gallery;
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) 
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    findview();
    setimage();
  }
   
  public void findview()
  {
	  gallery = (Gallery) findViewById(R.id.mygallery);	  
  }
  
  public void setimage()
  {
	//設定圖片來源
	    Integer[] mImageIds = {
	        R.drawable.png001,
	        R.drawable.png002,
	        R.drawable.png003,
	        R.drawable.png004
	    };
	    
	    ImageAdapter imageAdapter = new ImageAdapter(this);
	  //設定圖片的位置
	    imageAdapter.setmImageIds(mImageIds);
	    //圖片高度
	    imageAdapter.setHeight(100);
	    //圖片寬度
	    imageAdapter.setWidth(200);
	    gallery.setAdapter(imageAdapter);
	    gallery.setOnItemClickListener(new OnItemClickListener(){
	        public void onItemClick(AdapterView parent, View view, int position, long id) {
	            Toast.makeText(GDD01.this, "J 選的是第"+(position+1)+"張圖", Toast.LENGTH_LONG).show();
	        }
	    });
  }

  public class ImageAdapter extends BaseAdapter 
  {    
	  	private Context mContext;
	    private Integer width;
	    private Integer height;
	    private Integer[] mImageIds;

	    public ImageAdapter(Context c) {
	        mContext = c;
	    }

	    public View getView(int position, View convertView, ViewGroup parent) {
	        ImageView imageView = new ImageView(mContext);
	        //設定圖片來源
	        imageView.setImageResource(mImageIds[position]);
	        //設定圖片的寬、高
	        imageView.setLayoutParams(new Gallery.LayoutParams(width, height));
	        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
	        return imageView;
	    }

	    public Integer getHeight() {
	        return height;
	    }

	    public void setHeight(Integer height) {
	        this.height = height;
	    }

	    public Integer[] getmImageIds() {
	        return mImageIds;
	    }

	    public void setmImageIds(Integer[] mImageIds) {
	        this.mImageIds = mImageIds;
	    }

	    public Integer getWidth() {
	        return width;
	    }

	    public void setWidth(Integer width) {
	        this.width = width;
	    }

	    public int getCount() {
	        return mImageIds.length;
	    }

	    public Object getItem(int position) {
	        return position;
	    }

	    public long getItemId(int position) {
	        return position;
	    }
  }
}
   
    
    //使用android.R.drawable裡的圖片當成圖庫來源

 

 

自我LV~