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

ut6juiuv  于 2023-01-07  发布在  Android
关注(0)|答案(3)|浏览(156)

我想通过单击网格视图项目来显示全屏图像。我已经完成了网格视图,但不知道如何做到这一点。请给予我一些想法来做到这一点??谢谢....!

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};
}
33qvvth1

33qvvth11#

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

grid.setOnItemClickListener(new OnItemClickListener(){
    Intent intent = new Intent
                  (getApplicationContext(), ViewImage.class);
                                       intent.putExtra("filename", fileName); 
    //here fileName is something which tells the next activity about "selected image"
    //It can be ImageLocation/ImageName/ImageID/ImageURL
                                       startActivity(intent);
}

ViewImage.class

//here I assume that you send the ImageFile in the extra.
//so, converted it into bitmap and showed in the ImageView

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

omqzjyyz2#

您应该在您的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);
        }
    });

如果您现在在ViewImage.class中仅发送单个图像ID,则无需传递Bundle。您可以使用以下命令从Intent获取图像
我将使用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

希望这会有帮助!

62o28rlo

62o28rlo3#

在itemClick上,只需传递position

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);
        myIntent.putInt("image", position);
        startActivityForResult(myIntent, 0);
    }
});

在其他活动中,

public class MyImageViewActivity extends AppCompatActivity {

    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
    };
    
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_question7_spinner_background);

        Intent intent = getIntent();
        int position = intent.getIntExtra("image", 0);
        int imageId = mThumbsIds[position];

    }
}

相关问题