本範例由2個Gallery及1個ImageView元件組成, 最上層的g_folder(Gallery)代表相簿, 以
setOnItemClickListener()的方法對使用者的點擊做出換相簿的動作, 呈現圖片在g_photo(下一排的Gallery).
g_photo的onItemClick()事件中, 將使用者點選到的圖片放大到ImageView中.
Gallery_ImageView.java
package com.tsots.Gallery_ImageView; import android.app.Activity; import android.os.Bundle; 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 Gallery_ImageView extends Activity { Gallery g_folder,g_photo; ImageView i1; Context context = Gallery_ImageView.this; int level = 1; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); g_folder = (Gallery) findViewById(R.id.gallery_folder); g_photo = (Gallery) findViewById(R.id.gallery1); i1 = (ImageView) findViewById (R.id.imageView1); g_folder.setAdapter(new ImageAdapter(context)); g_folder.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView parent, View v, int position, long id) { Toast.makeText(context, String.valueOf(position), Toast.LENGTH_SHORT).show(); if(position == 0) { level = 11; } else if(position == 1) { level = 12; } else if(position ==2) { level = 13; } g_photo.setAdapter(new ImageAdapter(context)); backgroundType(i1); } }); g_photo.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView parent, View v, int position, long id) { Toast.makeText(context, String.valueOf(position), Toast.LENGTH_SHORT).show(); if(level == 11) { i1.setImageResource(new ImageAdapter(context).ImageId1[position]); } else if (level == 12) { i1.setImageResource(new ImageAdapter(context).ImageId2[position]); } else if (level == 13) { i1.setImageResource(new ImageAdapter(context).ImageId3[position]); } backgroundType(i1); } }); } public class ImageAdapter extends BaseAdapter { int mGalleryItemBackground; private Context mContext; public ImageAdapter(Context c) { mContext = c; } public int getCount() { if(level == 1) { return folderImageIds.length; } else if(level == 11) { return ImageId1.length; } else if(level == 12) { return ImageId2.length; } else { return ImageId3.length; } } public Object getItem(int position) { return position; } public long getItemId(int position) { return position; } public View getView(int position, View convertView, ViewGroup parent) { ImageView i = new ImageView(mContext); if(level == 1) { i.setImageResource(folderImageIds[position]); } else if(level == 11) { i.setImageResource(ImageId1[position]); } else if (level == 12) { i.setImageResource(ImageId2[position]); } else if(level == 13) { i.setImageResource(ImageId3[position]); } i.setScaleType(ImageView.ScaleType.FIT_XY); i.setLayoutParams(new Gallery.LayoutParams(120, 120)); backgroundType(i); return i; } private Integer[] folderImageIds = { R.drawable.ic_menu_slideshow, R.drawable.ic_menu_slideshow, R.drawable.ic_menu_slideshow }; private Integer[] ImageId1 = { R.drawable.pic_01, R.drawable.pic_02, R.drawable.pic_03, R.drawable.pic_04, R.drawable.pic_05, R.drawable.pic_06, R.drawable.pic_07, R.drawable.pic_08, R.drawable.pic_09, R.drawable.pic_10, R.drawable.pic_11, R.drawable.pic_12 }; private Integer[] ImageId2 = { R.drawable.photo_1, R.drawable.photo_2, R.drawable.photo_3, R.drawable.photo_4, R.drawable.photo_5, R.drawable.photo_6, R.drawable.photo_7, R.drawable.photo_8, R.drawable.photo_9, R.drawable.photo_10 }; private Integer[] ImageId3 = { R.drawable.img_01, R.drawable.img_02, R.drawable.img_03, R.drawable.img_04, R.drawable.img_05, R.drawable.img_06, R.drawable.img_07, R.drawable.img_08, R.drawable.img_09, R.drawable.img_10 }; } /* * 透過TypedArray設置Gallery/ImageView的背景風格 */ public void backgroundType(ImageView image) { int mGalleryItemBackground; TypedArray a = obtainStyledAttributes(R.styleable.Gallery); mGalleryItemBackground = a.getResourceId(R.styleable.Gallery_android_galleryItemBackground, 0); a.recycle(); image.setBackgroundResource(mGalleryItemBackground); } }
==============================補充 ==============================
當載入圖片時發生java.lang.OutOfMemoryError
可能是圖檔太大,超過能使用的記憶體
試試這個方法:
BitmapFactory.Options opts =
new
BitmapFactory.Options();
opts.inSampleSize =
2
; //
以 2 的平方為佳Bitmap bitmap = BitmapFactory.decodeFile(R.drawable.imageFile, opts);
沒有留言:
張貼留言