2016-08-30 125 views
1

我有一個活動,在GridView中顯示圖庫圖像。在gridview上方,有一個ImageView顯示從GridView(點擊時)中選擇的圖像。 當我從gridview中獲取drawable並將其設置在ImageView中時出現問題。ImageView的質量非常低。從gridview設置可繪製的imageview時的低質量圖像

的ImageView的屬性是:ScaleTye="FitXY", Width="match_parent", Height="wrap_content"

什麼建議嗎?謝謝!

public class FotoGaleria extends AppCompatActivity { 

private Cursor cursor; 
private ArrayList<String> images; 
private int columnIndex; 
ImageAdapter myImageAdapter; 
ImageView imagen; 
Drawable dr; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_foto_galeria); 

    DisplayMetrics dm = new DisplayMetrics(); 
    getWindowManager().getDefaultDisplay().getMetrics(dm); 

    int width = dm.widthPixels; 
    int height = dm.heightPixels; 

    getWindow().setLayout((int) (width * .95), (int) (height * .8)); 

    GridView gallery = (GridView) findViewById(R.id.GridView); 
    imagen= (ImageView) findViewById(R.id.imagengrande); 

    gallery.setAdapter(new ImageAdapter(this)); 


    gallery.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> arg0, View arg1, 
           int position, long arg3) { 

      if (null != images && !images.isEmpty()) 


       imagen.setImageDrawable(((ImageView) arg1).getDrawable()); 



     } 
    }); 

} 

/** 
* The Class ImageAdapter. 
*/ 
private class ImageAdapter extends BaseAdapter { 

    /** The context. */ 
    private Activity context; 

    /** 
    * Instantiates a new image adapter. 
    * 
    * @param localContext 
    *   the local context 
    */ 
    public ImageAdapter(Activity localContext) { 
     context = localContext; 
     images = getAllShownImagesPath(context); 
    } 

    public int getCount() { 
     return images.size(); 
    } 

    public Object getItem(int position) { 
     return position; 
    } 

    public long getItemId(int position) { 
     return position; 
    } 

    public View getView(final int position, View convertView, 
         ViewGroup parent) { 
     ImageView picturesView; 
     if (convertView == null) { 
      picturesView = new ImageView(context); 
      picturesView.setScaleType(ImageView.ScaleType.FIT_CENTER); 
      picturesView 
        .setLayoutParams(new GridView.LayoutParams(126, 126)); 

     } else { 
      picturesView = (ImageView) convertView; 
     } 
     ImageView img = (ImageView)findViewById(R.id.imagengrande); 
     Glide.with(context).load(images.get(position)) 
       .placeholder(R.drawable.ic_nofoto).centerCrop() 
       .into(picturesView); 

     return picturesView; 
    } 

    /** 
    * Getting All Images Path. 
    * 
    * @param activity 
    *   the activity 
    * @return ArrayList with images Path 
    */ 
    private ArrayList<String> getAllShownImagesPath(Activity activity) { 
     Uri uri; 
     Cursor cursor; 
     int column_index_data, column_index_folder_name; 
     ArrayList<String> listOfAllImages = new ArrayList<String>(); 
     String absolutePathOfImage = null; 
     uri = android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI; 

     String[] projection = {MediaStore.MediaColumns.DATA, 
       MediaStore.Images.Media.BUCKET_DISPLAY_NAME }; 

     cursor = activity.getContentResolver().query(uri, projection, null, 
       null, null); 

     column_index_data = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA); 
     column_index_folder_name = cursor 
       .getColumnIndexOrThrow(MediaStore.Images.Media.BUCKET_DISPLAY_NAME); 
     while (cursor.moveToNext()) { 
      absolutePathOfImage = cursor.getString(column_index_data); 

      listOfAllImages.add(absolutePathOfImage); 

     } 

     return listOfAllImages; 
    } 
} 

回答

0

圖像質量差的原因是因爲滑行
Glide以imageview的分辨率存儲圖像,根據您的代碼,圖像視圖大小爲126x126。然後在你的活動中,你會得到在imageview中加載的大小爲126x126的drawable。所有的 首先移動getAllShownImagesPath(context)到你的活動,使那裏的ArrayList並將它傳遞給適配器:

所以爲了擁有全尺寸的圖像,則可以按如下修改代碼。因此,您將在活動中包含所有圖像列表。
現在gallery.setOnItemClickListener,可以簡單的寫如下

Glide.with(context).load(imageslist.get(position)) 
       .placeholder(R.drawable.ic_nofoto).centerCrop() 
       .into(imagen); 

希望,這將幫助你。

+0

非常感謝你的男人!它完美的工作!:) – Dani

0

實際視圖中的圖像已被Glide調整爲適合該視圖。你應該做的是,再次使用Glide將圖像加載到視圖中。由於Glide緩存圖像,不會有任何延遲。