Android控件之Gallery实现图片列表
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
values/attrs.xml:
1.
2.
3.
4.
5.
layout/img.xml:
Java代码
1.
2. android" 3.android:orientation="vertical" android:layout_width="fill_parent" 4.android:layout_height="fill_parent"> 5. android:layout_width="fill_parent" 6.android:layout_height="wrap_content" android:layout_x="12px" 7.android:layout_y="106px" /> 8. android:layout_width="fill_parent" 9.android:layout_height="wrap_content" /> 10. ImageAdapter.java: Java代码 1.import java.util.List; 2. 3.import org.mdx.R; 4. 5.import android.content.Context; 6.import android.content.res.TypedArray; 7.import android.graphics.Bitmap; 8.import android.graphics.BitmapFactory; 9.import android.view.View; 10.import android.view.ViewGroup; 11.import android.widget.BaseAdapter; 12.import android.widget.Gallery; 13.import android.widget.ImageView; 14.public class ImageAdapter extends BaseAdapter { 15.intmGalleryItemBacjground; 16.private Context mContext; 17.private List 18. 19.public ImageAdapter(Context c, List 20.mContext = c; 21.list = li; 22.TypedArray a = c.obtainStyledAttributes(R.styleable.Gallery); 23.mGalleryItemBacjground = a.getResourceId( 24.R.styleable.Gallery_android_galleryItemBackground, 0); 25.a.recycle(); 26.} 27. 28.public intgetCount() { 29.return list.size(); 30.} 31. 32.public Object getItem(int position) { 33.return list.get(position%list.size()).intValue(); 34.} 35. 36.public long getItemId(int arg0) { 37.// TODO Auto-generated method stub 38.return arg0; 39.} 40. 41.public View getView(int position, View arg1, ViewGroup arg2) { 42.// TODO Auto-generated method stub 43.ImageViewi = new ImageView(mContext); 44.Bitmap bm = BitmapFactory.decodeResource(mContext.getResources(), list 45..get(position%list.size()).intValue()); 46.i.setImageBitmap(bm); 47.i.setScaleType(ImageView.ScaleType.FIT_XY); 48.i.setLayoutParams(new youtParams(136, 88)); 49.i.setBackgroundResource(mGalleryItemBacjground); 50.return i; 51.} 52. 53.} ImgActivity.java: Java代码 1.import java.util.ArrayList; 2.import java.util.List; 3. 4.import org.mdx.R; 5.import org.mdx.core.activity.adapter.ImageAdapter; 6. 7.import android.app.Activity; 8.import android.os.Bundle; 9.import android.view.View; 10.import android.widget.AdapterView; 11.import android.widget.AdapterView.OnItemClickListener; 12.import android.widget.AdapterView.OnItemSelectedListener; 13.import android.widget.Gallery; 14.import android.widget.ImageView;