通过单击Gridview中的网格项显示全屏图像

时间:2011-07-06 06:54:15

标签: android

我想通过单击网格视图项来显示全屏图像。我完成了网格视图,但不知道如何执行此操作。请给我一些想法吗? 感谢....!

mGridView.setOnItemClickListener(new OnItemClickListener() {

            @Override
        public void onItemClick(AdapterView<?> parent, View v, int position,long id) {
                // TODO Auto-generated method stub
                Toast.makeText(getApplicationContext(), "Image"+(position+1), Toast.LENGTH_SHORT).show();
                Intent intent =new Intent(getApplicationContext(),MyImageViewActivity.class);
                intent.putExtra("filename", i);
                startActivity(intent);
            }
        });

MyGridView.java

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.KeyStore.LoadStoreParameter;

import com.myworkspace.R.menu;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.provider.ContactsContract.CommonDataKinds.Im;
import android.provider.MediaStore.Audio.Media;
import android.provider.SyncStateContract.Columns;
import android.sax.StartElementListener;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class MyGridView extends Activity {
    GridView mGridView;
    int counter,i;
    MyImageAdapter adapter = null;
    ImageView imgVw;
    Bitmap bitmap;
    private Cursor cursor;
    int columnIndex;
    Integer[] mThumbsIds = {
            R.drawable.image1,R.drawable.image2,
            R.drawable.image3,R.drawable.image4,
            R.drawable.image5,R.drawable.icon,
            R.drawable.icon,R.drawable.icon,
            R.drawable.icon
            };

    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mygridacti);

        ImageView imageview = (ImageView)findViewById(R.id.GalleryView);
        mGridView = (GridView)findViewById(R.id.grdvw);
        adapter = new MyImageAdapter(this);
        adapter.notifyDataSetChanged();
        mGridView.setAdapter(adapter);
        mGridView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View v, int position,long id) {
                // TODO Auto-generated method stub
                Toast.makeText(getApplicationContext(), "Image"+(position+1), Toast.LENGTH_SHORT).show();
                Intent myIntent = new Intent(MyGridView.this, MyImageViewActivity.class);
                Bundle bundle = new Bundle();
                bundle.putInt("image", mThumbsIds[position]); 
                myIntent.putExtras(bundle);
                startActivityForResult(myIntent, 0);
            }
        });
    }
}

class MyImageAdapter extends BaseAdapter{
    private Context mContext;
    ImageView imageview;
    int counter;

    public MyImageAdapter(Context c){
        mContext = c;
    }

    public MyImageAdapter(OnClickListener onClickListener) {
        // TODO Auto-generated constructor stub

    }
    @Override
    public int getCount() {
        return mThumbsIds.length;
    }
    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }
    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }
    @Override
    public View getView(final int position, final View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        if(convertView == null){
            imageview = new ImageView(mContext);
            imageview.setLayoutParams(new GridView.LayoutParams(85,85));
            imageview.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageview.setPadding(8, 8, 8, 8);
            counter++;
            }
        else{
            imageview = (ImageView) convertView;
        }
        imageview.setImageResource(mThumbsIds[position]);
        return imageview;
    }

private Integer[] mThumbsIds = {
            R.drawable.image1,R.drawable.image2,
            R.drawable.image3,R.drawable.image4,
            R.drawable.image5,R.drawable.icon,
            R.drawable.icon,R.drawable.icon,
            R.drawable.icon};
}

3 个答案:

答案 0 :(得分:1)

您好可能是这个链接将为您的问题提供解决方案。 Android GridView Layout

答案 1 :(得分:0)

您知道如何在gridView中显示图像。在此之后执行

的动作
grid.setOnItemClickListener(new OnItemClickListener()
{
    Intent intent = new Intent
                  (getApplicationContext(), ViewImage.class);
                                       intent.putExtra("filename", i);
                                       startActivity(intent);
}

ViewImage.class

Bundle extras = i.getExtras();
                BitmapFactory.Options bfo = new BitmapFactory.Options();
                bfo.inSampleSize = 2;
                filename = extras.getString("filename");
mBitmap = BitmapFactory.decodeFile(filename, bfo);
imageview.setbitmap(mbitmap);

答案 2 :(得分:0)

您应该在setOnItemClickListener中执行类似的操作,我正在使用您的代码并对其进行改进

mGridView.setOnItemClickListener(new OnItemClickListener() {

        @Override
    public void onItemClick(AdapterView<?> parent, View v, int position,long id) {
            // use gridItem position to get the Image from mThumbsIds
            int image = mThumsIds[position] // here I m passing the position of GridViewItem
            Intent intent =new Intent(getApplicationContext(),MyImageViewActivity.class);
            intent.putExtra("image", image);
            startActivity(intent);
        }
    });

如果您只发送一个图像ID,则无需传递Bundle 现在,在您的ViewImage.class中,您可以使用

从意图中获取图像

我正在使用Glide向您展示如何在ImageView中显示此图像

int image = getIntent().getIntExtra("image");

//now you can use this "image" variable to display image

ImageView imageView = findViewById(R.id.imageView);

Glide.with(this).load(image).into(imageView); // here pass you imageView in the last method as parameter

希望这会有所帮助!