我有一個活動,在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;
}
}
非常感謝你的男人!它完美的工作!:) – Dani