2 qq 29509099 qq_29509099 于 2015.07.06 15:48 提问

Gallery不出图片是为什么?

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.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

public class TestImageView extends Activity {

private Gallery mGallery;
@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) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    catch(IllegalAccessException e){
        e.printStackTrace();
    }
    mGallery.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView parent, View v, int position,
                long id) {
            // TODO Auto-generated method stub
            TestImageView.this.setTitle(String.valueOf(position));
        }
    });
}
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 IllegalAccessException, IllegalArgumentException {
        // TODO Auto-generated constructor stub
        mContext = c;

        //用反射机制来获取资源中的图片ID和尺寸
        Field[] fields = R.drawable.class.getDeclaredFields();
        for(Field field : fields){
            if(!"ic_launcher".equals(field.getName())){
                int index = field.getInt(R.drawable.class);
                //保存图片ID
                imgList.add(index);
                //保存图片大小
                int size[] = new int[2];
                Bitmap bmImg = BitmapFactory.decodeResource(getResources(), index);
                size[0] = bmImg.getWidth();
                size[1] = 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);
        //从imgSize取得图片大小
        int size[] = new int[2];
        size = (int[]) imgSizes.get(position);
        i.setLayoutParams(new Gallery.LayoutParams(size[0], size[1]));
        return i;
    }

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

1个回答

danielinbiti
danielinbiti   Ds   Rxr 2015.07.06 15:57
 没有复制图片
 @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ImageView i = new ImageView(mContext);
        //从imgSize取得图片大小
                i.setImageResource(this.imgList.get(position));  //添加图片,增加这句,需要赋值显示图片
        int size[] = new int[2];
        size = (int[]) imgSizes.get(position);
        i.setLayoutParams(new Gallery.LayoutParams(size[0], size[1]));
        return i;
    }
Csdn user default icon
上传中...
上传图片
插入图片