Skip to content

Instantly share code, notes, and snippets.

@shaon2016
Last active October 30, 2017 05:54
Show Gist options
  • Save shaon2016/1655f7f29c4379671e58abbe5fb15158 to your computer and use it in GitHub Desktop.
Save shaon2016/1655f7f29c4379671e58abbe5fb15158 to your computer and use it in GitHub Desktop.
private int imageHeight, imageWidth, w;
private ImageView imageSingleFoodItem;
onCreate() {
// w = (int) (getScreenWidth(this) * 0.80);
w = (int) (getScreenWidth(this));
//drawable image and you want that size you can get like this way
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(getResources(), R.drawable.food3, options);
imageHeight = options.outHeight;
imageWidth = options.outWidth;
imageSingleFoodItem.getLayoutParams().height = (int) Math.ceil((imageHeight * w) / imageWidth);
imageSingleFoodItem.getLayoutParams().width = w;
imageSingleFoodItem.requestLayout();
imageSingleFoodItem.setImageResource(R.drawable.food3);
}
public static int getScreenWidth(Context context) {
return context.getResources().getDisplayMetrics().widthPixels;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment