android 这 Gallery的使用
来源:互联网 发布:淘宝禁止销售兴奋剂 编辑:程序博客网 时间:2024/05/29 09:15
原文引用:http://blog.csdn.net/hellogv/article/details/5961525
使用 Gallery 查看图片:
main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/testImageView" > <Gallery android:id="@+id/gallery" android:layout_height="fill_parent" android:layout_width="fill_parent" ></Gallery> </LinearLayout>
GallerydemoActivity.java
package com.hkrt;import java.lang.reflect.Field;import java.util.ArrayList;import android.app.Activity;import android.content.Context;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.os.Bundle;import android.util.DisplayMetrics;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.AdapterView.OnItemClickListener;public class GallerydemoActivity extends Activity {private Gallery mGallery; ///获取手机屏幕分辨率的类 private DisplayMetrics dm; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mGallery = (Gallery)findViewById(R.id.gallery); try { mGallery.setAdapter(new ImageAdapter(this)); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } mGallery.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView parent, View v, int position, long id) {// TextView testImageView = (TextView) findViewById(R.id.testImageView);// testImageView.setText(String.valueOf(position)); } }); } /* * class ImageAdapter is used to control gallery source and operation. */ private class ImageAdapter extends BaseAdapter{ private Context mContext; private ArrayList<Integer> imgList=new ArrayList<Integer>(); private ArrayList<Object> imgSizes=new ArrayList<Object>(); public ImageAdapter(Context c) throws IllegalArgumentException, IllegalAccessException{ mContext = c; //用反射机制来获取资源中的图片ID和尺寸 Field[] fields = R.drawable.class.getDeclaredFields(); for (Field field : fields) { if (!"icon".equals(field.getName()))//除了icon之外的图片 { int index=field.getInt(R.drawable.class); //保存图片ID imgList.add(index); //保存图片大小 int size[]=new int[2]; Bitmap bmImg=BitmapFactory.decodeResource(getResources(),index); dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); //获得手机的宽度和高度像素单位为px size[0]=dm.widthPixels;//bmImg.getWidth(); size[1]=dm.heightPixels;//bmImg.getHeight(); imgSizes.add(size); } } } @Override public int getCount() { // TODO Auto-generated method stub return imgList.size(); } @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; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub ImageView i = new ImageView (mContext); //从imgList取得图片ID i.setImageResource(imgList.get(position).intValue()); i.setScaleType(ImageView.ScaleType.FIT_XY); //从imgSizes取得图片大小 int size[]= new int[2]; size=(int[]) imgSizes.get(position); i.setLayoutParams(new Gallery.LayoutParams(size[0], size[1])); return i; } }; }效果图:
- android 这 Gallery的使用
- android gallery的使用
- Android组件的使用:Gallery
- Android中的Gallery的使用
- Android中的Gallery的使用
- Android中的Gallery的使用
- Android中的Gallery的使用
- Android中Gallery的使用
- Android之Gallery的使用
- android 的 Gallery 的简单使用
- android Gallery与ImageView的结合使用
- android gallery控件的使用问题
- Android中Gallery和ImageSwitcher的使用
- Android中Gallery和ImageSwitcher的使用
- android 画廊控件Gallery的使用
- Android : Gallery使用例子
- android Gallery的学习心得
- android--Gallery的实现
- ORACLE DECODE函数
- Linux应用程序之Helloworld入门
- HTTP错误码大全
- c/c++ API函数大全
- PHP程序员最容易出现的错误总结
- android 这 Gallery的使用
- 调试经验总结:VC下的错误对话框
- C/C++中函数指针的含义
- Hibernate 第一篇
- 在SUSE Linux 下IP设置的方法
- 【原】flash图片批量上传处理专用php类。
- SQL查询表的列名,类型等
- 交易机制的相关问题
- 搜狗笔试算法设计题参考答案