在gridview android的onClick中显示更大的图像

时间:2014-09-25 18:32:17

标签: android

我正在使用gridview,它在sdcard上显示app文件夹中的图像。单击gridview中的图像时,如何显示全屏图像。在gridview中加载图像时,下面的代码工作正常。感谢

更新:所以我创建了另一个活动FullImageActivity,我试图在那里显示点击的图像grom gridview。问题是它只显示图像路径而不是图像。我需要在FullImageActivity中显示图像。我如何实现这一目标?

public class ImageAdapter extends BaseAdapter{

     private Context mContext;
     ArrayList<String> itemList = new ArrayList<String>();

     public ImageAdapter(Context c){

         mContext = c; 

     }

     void add (String path){

         itemList.add(path);
     }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return itemList.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return itemList.get(position);
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ImageView imageView;
         if (convertView == null) {  // if it's not recycled, initialize some attributes
             imageView = new ImageView(mContext);
             imageView.setLayoutParams(new GridView.LayoutParams(220, 220));
             imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
             imageView.setPadding(8, 8, 8, 8);
         } else {
             imageView = (ImageView) convertView;
         }

         Bitmap bm = decodeSampledBitmapFromUri(itemList.get(position), 220, 220);

         imageView.setImageBitmap(bm);
         return imageView;

    }

    public Bitmap decodeSampledBitmapFromUri(String path, int reqWidth, int reqHeight) {

           Bitmap bm = null;
           // First decode with inJustDecodeBounds=true to check dimensions
           final BitmapFactory.Options options = new BitmapFactory.Options();
           options.inJustDecodeBounds = true;
           BitmapFactory.decodeFile(path, options);

           // Calculate inSampleSize
           options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

           // Decode bitmap with inSampleSize set
           options.inJustDecodeBounds = false;
           bm = BitmapFactory.decodeFile(path, options); 

           return bm;   
          }

    public int calculateInSampleSize(

               BitmapFactory.Options options, int reqWidth, int reqHeight) {
               // Raw height and width of image
               final int height = options.outHeight;
               final int width = options.outWidth;
               int inSampleSize = 1;

               if (height > reqHeight || width > reqWidth) {
                if (width > height) {
                 inSampleSize = Math.round((float)height / (float)reqHeight);    
                } else {
                 inSampleSize = Math.round((float)width / (float)reqWidth);    
                }   
               }

               return inSampleSize;    
      }


}

ImageAdapter myImageAdapter;
GridView gridview;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.library_layout);


      gridview = (GridView) findViewById(R.id.gridview);
        myImageAdapter = new ImageAdapter(this);
        gridview.setAdapter(myImageAdapter);


        String ExternalStorageDirectoryPath = Environment.getExternalStorageDirectory().getAbsolutePath(); 
        String targetPath = ExternalStorageDirectoryPath + "/AppFolder/";

        File targetDirector = new File(targetPath);

        File[] files = targetDirector.listFiles();
        for (File file : files){
         myImageAdapter.add(file.getAbsolutePath());


        }

        gridview.setOnItemClickListener(myOnItemClickListener);


       }

         OnItemClickListener myOnItemClickListener = new OnItemClickListener(){


    @Override
      public void onItemClick(AdapterView<?> parent, View view, int position,
        long id) {


       String prompt = (String)parent.getItemAtPosition(position);
       Toast.makeText(getApplicationContext(),  prompt,  Toast.LENGTH_LONG).show();

       Intent intent = new Intent(LibraryActivity.this, FullImageActivity.class);
       intent.putExtra("Image", position);
       LibraryActivity.this.startActivity(intent);


    }



      };

}

// FullImageActivity

public class FullImageActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.full_image);

    int position = getIntent().getExtras().getInt("Image");
    ImageView imageView = (ImageView) findViewById(R.id.full_image_view);
    imageView.setImageResource(position);

  // above code displays the image path but not the image.How can I display the image?Thanks

}

}

1 个答案:

答案 0 :(得分:0)

在活动中使用TouchImageView并将图片传递给该活动

示例活动

public class FullScreenImageActivity extends Activity {

@Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      requestWindowFeature(Window.FEATURE_NO_TITLE);
      setContentView(R.layout.activity_full_screen_image);

      Intent intent = getIntent();
      Bitmap bitmap = (Bitmap) intent.getParcelableExtra("BitmapImage");
      TouchImageView imageView = (TouchImageView)findViewById(R.id.imgFullScreen);

      imageView.setLayoutParams( new RelativeLayout.LayoutParams(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT));

      imageView.setImageBitmap(bitmap);

   }

布局

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000"
    tools:context=".FullScreenImage" >

   <com.app.TouchImageView
       android:id="@+id/imgFullScreen"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"/>

</RelativeLayout>

以这种方式传递您的位图

Intent intent = new Intent(this, NewActivity.class);
intent.putExtra("BitmapImage", bitmap);

以这种方式检索它(在FullScreenImageActivity中)

Intent intent = getIntent();
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("BitmapImage");