2014-09-25 59 views
1

我正在使用gridview显示从SD卡上的应用程序文件夹的图像。如何在gridview中单击图像时显示全屏图像。下面的代码在gridview中加载图片时工作正常。谢谢在gridview的onClick中显示更大的图像android

更新:所以我创建了另一个活动FullImageActivity,我试图在那里显示点击图像格罗姆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 

} 

}

回答

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"); 
+0

我已经添加在原来的问题一些代码之上,但我仍然不能得到在另一个活动中显示来自gridview的图像。 – artist 2014-09-25 21:26:46

+0

看到我的编辑,你可以传递位图或图片url,但是现在你在gridview中传递视图的位置。 – 2014-09-26 13:35:02