从GridView中的缩略图扩展图像

时间:2015-04-20 12:20:24

标签: android gridview android-gridview

所以我有一个简单的应用程序,我用相机拍摄图像,然后将图像加载到GridView,当我点击GridView时,它必须打开更大的版本图片。我无法让图像更大。

问题是,在将图像传递给Activity时,我没有引用该图像,这会使图像变大。代码如下。

MainActivity.java

protected static final String EXTRA_RES_ID = "POS";
private ArrayList<String> mThumbIdsSelfies = new ArrayList<String>();

if(populateArrayList())
{
    GridView gridview = (GridView) findViewById(R.id.gridview);

    // Create a new ImageAdapter and set it as the Adapter for this GridView
    gridview.setAdapter(new ImageAdapter(this, mThumbIdsSelfies));

    // Set an setOnItemClickListener on the GridView
    gridview.setOnItemClickListener(new AdapterView.OnItemClickListener()
    {
        public void onItemClick(AdapterView<?> parent, View v,
                                int position, long id)
        {

            //Create an Intent to start the ImageViewActivity
            Intent intent = new Intent(MainActivity.this, ImageViewActivity.class);

            // Add the ID of the thumbnail to display as an Intent Extra
            intent.putExtra(EXTRA_RES_ID, (int) id);

            // Start the ImageViewActivity
            startActivity(intent);
        }
    });
}

private boolean populateArrayList()
{
    File dir = getAlbumDir();
    //Bitmap myBitmap;

    if (dir.isDirectory())
    {
        File[] files = dir.listFiles();

        for (int i = 0; i < files.length; i++)
        {
            //myBitmap = BitmapFactory.decodeFile(files[i].toString());
            mThumbIdsSelfies.add(files[i].toString());
        }
    }

    return true;
}

ImageViewActivity.java - 这是使图像更大的那个

public class ImageViewActivity extends Activity 
{

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);

        // Get the Intent used to start this Activity
        Intent intent = getIntent();

        // Make a new ImageView
        ImageView imageView = new ImageView(getApplicationContext());

        // Get the ID of the image to display and set it as the image for this ImageView
        imageView.setImageResource(intent.getIntExtra(MainActivity.EXTRA_RES_ID, 0));

        setContentView(imageView);
    }
}

ImageAdapter.java

public class ImageAdapter extends BaseAdapter 
{
    private static final int PADDING = 8;
    private static final int WIDTH = 250;
    private static final int HEIGHT = 250;
    private Context mContext;
    private List<String> mThumbIds;

    // Store the list of image IDs
    public ImageAdapter(Context c, List<String> ids) 
    {
        mContext = c;
        this.mThumbIds = ids;
    }

    // Return the number of items in the Adapter
    @Override
    public int getCount() 
    {
        return mThumbIds.size();
    }

    // Return the data item at position
    @Override
    public Object getItem(int position) 
    {
        return mThumbIds.get(position);
    }

    // Will get called to provide the ID that
    // is passed to OnItemClickListener.onItemClick()
    @Override
    public long getItemId(int position) 
    {
        return mThumbIds.indexOf(position);
    }

    // Return an ImageView for each item referenced by the Adapter
    @Override
    public View getView(int position, View convertView, ViewGroup parent) 
    {

        ImageView imageView = (ImageView) convertView;

        // if convertView's not recycled, initialize some attributes
        if (imageView == null) 
        {
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(WIDTH, HEIGHT));
            imageView.setPadding(PADDING, PADDING, PADDING, PADDING);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        }

        //imageView.setImageResource(mThumbIds.get(position));
        Bitmap b = BitmapFactory.decodeFile(mThumbIds.get(position));
        imageView.setImageBitmap(b);
        return imageView;
    }
}

所以上面这个函数的问题是我不能使用getItemId,因为它不返回long,而是返回一个字符串,我无法从中获得有用的东西。

我尝试过的另一件事是将位图图像作为我的包中的额外图像传递并在另一侧读取它,但我仍然没有运气来显示实际图像。

2 个答案:

答案 0 :(得分:1)

我终于弄明白了,所以这就是我所做的。

MainActivity.java - 更改了以下行

intent.putExtra(EXTRA_RES_ID, mThumbIdsSelfies.get(position)/*(int) id*/);

ImageViewActivity.java

String s = intent.getStringExtra(MainActivity.EXTRA_RES_ID);
Bitmap bitmap = BitmapFactory.decodeFile(s);
imageView.setImageBitmap(bitmap);

所以这些更改最终帮助了我,我所要做的就是将字符串引用传递给我刚刚点击的缩略图的文件位置。然后在放大该图像的Activity中,我必须得到该字符串引用并从中生成一个位图,然后将该位图设置为imageView。

答案 1 :(得分:0)

假设mThumbIdsSelfiesArrayList图像路径,您可以使用此功能:

Intent intent = new Intent(MainActivity.this, ImageViewActivity.class);
intent.putExtra(EXTRA_RES_ID, mThumbIdsSelfies[position]);
startActivity(intent);

然后在ActivityImageViewActivity)中检索它,并像在适配器的getView()方法中一样使用它。