从gridview设置可绘制的imageview时的低质量图像

时间:2016-08-30 12:41:40

标签: android gridview

我有一个活动,在GridView中显示图库图片。在gridview上方,有一个ImageView,显示从GridView中选择的图像(单击时)。 当我从gridview获取drawable然后在ImageView中设置它时,会出现问题。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;
    }
}

2 个答案:

答案 0 :(得分:0)

实际视图中的图片已由Glide调整以适合该视图。您应该做的是,再次使用Glide将图像加载到视图中。当Glide缓存图像时,不会有任何延迟。

答案 1 :(得分:0)

图像质量差的原因是滑行  Glide以imageview的分辨率存储图像,根据您的代码,图像视图大小为126x126。然后在您的活动中,您将获得在imageview中加载的drawable,其大小为126x126。

因此,为了获得完整尺寸的图像,您可以按如下方式修改代码: 首先将getAllShownImagesPath(context)移动到您的活动并在那里制作arraylist并将其传递给适配器。因此,您将在活动中列出所有图像列表 现在在gallery.setOnItemClickListener中,您可以按如下方式编写

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

希望,这会对你有帮助。