Resizing image for XXHDPI - android

I have an image that is being displayed perfectly for MDPI.
Its resolution is 191 x 255.
Then I resize it to XXDPI, using 1:3 proportion, it gets to 573 x 765.
Still, when the emulator displays it, the quality is not as good as the MDPI one.
It gets clearly poor.
For both cases, I am using it Wrap Content
If I display both images on a image editor, they have perfect quality.
Why does that happen? Am I missing something here?

DisplayMetrics metrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getRealMetrics(metrics);
int widthPixels = metrics.widthPixels;
int heightPixels = metrics.heightPixels;
float widthDpi = metrics.xdpi;
float heightDpi = metrics.ydpi;
float widthInches = widthPixels / widthDpi;
float heightInches = heightPixels / heightDpi;
double diagonalInches = Math.sqrt((widthInches * widthInches) + (heightInches * heightInches)); // this code returns the actual screen size (eg. 7,8,9,10 inches in double format ex: 7.932189832)
diagonalInches = (double) Math.round(diagonalInches * 10) / 10; // round up to first decimal places
ArrayList<Integer> imageId = new ArrayList<>();
imageId.clear();
for (Integer i : imageNum){ // uses loop since im using TransitionDrawable to change images in single view
String stringID;
if (isPortrait) {
stringID = "featured_img_" + i.toString() + "_port"; // portrait images
} else {
if (diagonalInches >= 7 && diagonalInches <=8){
stringID = "featured_img_" + i.toString() + "_land_b"; // landscape images this is the drawable filename
} else {
stringID = "featured_img_" + i.toString() + "_land_a"; //landscape images this is the drawable filename
}
}
int drawableId = getResId(stringID);
imageId.add(drawableId); // adding it to arraylist
}

you can add android:scaleType=" . . . " on what you want for your image to be displayed

Related

Supporting 9.6 inch and 10.1 inch android tablet

I have to support android devices with screen resolution 1280*800(in dp)(KitKat) and 1280 * 752(dp)(Lollipop). The first one is a 10.1 inch tablet and second one is 9.6 inch tablet.
I am using same layout for all device and using externalized dimension to adjust layout for different screen.
But I am not able to separate the device using "values-sw720dp" and "values-sw800dp". If I use sw800dp both of them use the dimen value from sw800dp dimen folder.
How can I give separate dimension for the two devices?
Get your device's screnn inches programmatically and appy dimension
public static double getDeviceInches(Activity activity) {
DisplayMetrics metrics = getMetrics(activity);
int widthPixels = metrics.widthPixels;
int heightPixels = metrics.heightPixels;
float widthDpi = metrics.xdpi;
float heightDpi = metrics.ydpi;
float widthInches = widthPixels / widthDpi;
float heightInches = heightPixels / heightDpi;
return getDiagonalInches(widthInches, heightInches);
}
private static double getDiagonalInches(float widthInches, float heightInches) {
double diagonalInches = Math.sqrt((widthInches * widthInches) + (heightInches * heightInches));
float roundedValue = (float) Math.round(diagonalInches);
return (double)roundedValue;
}
//From this, we can get the information required to size the display:
private static DisplayMetrics getMetrics(Activity activity) {
DisplayMetrics metrics = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
return metrics;
}
public static int convertDpToPx(Context context, int value) {
// Get the screen's density scale
final float scale = context.getResources().getDisplayMetrics().density;
// Convert the dps to pixels, based on density scale(0.5f is for rounding up value) (arrowWidth is 50dp)
int pxValue= (int) (value * scale + 0.5f);
return pxValue;
}
Put your above code in utility class and call getDeviceInches method from your acitivty or fragment class

Android : How to calculate aspect ratio of images?

I have 100 images of huge size (500*700 , 401*800 , 2345* 3567) , so I want all these images height same as 220. Below I'm implementing calculation for the aspect ratio of image. I want to set the fixed height of all huge size images. I'm using some arithmetic formula to calculate the aspect ratio but getting all images width as same, and height is different but I want height to be fixed. How to get the fixed height? Thanks to appreciate.
Here is my calculation code for image aspect ratio
if (bitmapResizeImage != null) {
int originalWidth = bitmapResizeImage.getWidth();
int originalHeight = bitmapResizeImage.getHeight();
Log.e("originalWidth "," = " + originalWidth +" originalHeight = " + originalHeight+" of imgElement = " + imgElement+ " Notation = " +all_Post.getStrNotationNo());
float aspect_ratio = originalWidth / originalHeight ;
Log.e("aspect_ratio "," = " + aspect_ratio);
float adjusted_width = 220 * originalWidth / originalHeight ;
Log.e("adjusted_width "," = " + adjusted_width);
float adjusted_height = originalWidth * originalHeight / originalWidth ;
Log.e("adjusted_height "," = " + adjusted_height);
pBar.setVisibility(View.GONE);
imageView.setImageBitmap(bitmapResizeImage);
}
You should probably try typecasting:
float aspect_ratio = (float)originalWidth / (float)originalHeight ;
Because this typecasting operation will return a float value.
Integer divided by Integer will give the result as integer, which will be assigned to a float variable. Hence the operands need to be converted to float, in order to make sure that the result is computed as float.
In addition to what Priyank has posted, once you have the correct values
you can create a bitmap with the new dimensions before using setImageBitmap on the ImageView. Use
imageView.setImageBitmap(Bitmap.createBitmap(int width, int height, Bitmap.Config config));

How to get the Display Size in Inches in Android?

I need for my Application the exact inch of a display. My current solution is:
double inch;
double x = Math.pow(widthPix/dm.xdpi,2);
double y = Math.pow(heigthPix/dm.ydpi,2);
double screenInches = Math.sqrt(x+y);
inch = screenInches;
inch = inch * 10;
inch = Math.round(inch);
inch = inch / 10;
Is there a better way to get the display inch? Current on my test device 4 inch it say 5.9 and that is wrong...
The following gave me a a result quite close to the specs:
DisplayMetrics dm = getResources().getDisplayMetrics();
double density = dm.density * 160;
double x = Math.pow(dm.widthPixels / density, 2);
double y = Math.pow(dm.heightPixels / density, 2);
double screenInches = Math.sqrt(x + y);
log.info("inches: {}", screenInches);
Output: inches: 4.589389937671455
Specs: Samsung Galaxy Nexus (720 x 1280 px, ~320 dpi, 4.65")
Please note, that dm.heightPixels (or dm.widthPixels depending on your orientatation) does not necessarily provide accurate information, since soft keys (as used in the Galaxy Nexus) are not added to the height (or width).
Try this.... This will give you the exact size of the display..
static String getDisplaySize(Activity activity) {
double x = 0, y = 0;
int mWidthPixels, mHeightPixels;
try {
WindowManager windowManager = activity.getWindowManager();
Display display = windowManager.getDefaultDisplay();
DisplayMetrics displayMetrics = new DisplayMetrics();
display.getMetrics(displayMetrics);
Point realSize = new Point();
Display.class.getMethod("getRealSize", Point.class).invoke(display, realSize);
mWidthPixels = realSize.x;
mHeightPixels = realSize.y;
DisplayMetrics dm = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
x = Math.pow(mWidthPixels / dm.xdpi, 2);
y = Math.pow(mHeightPixels / dm.ydpi, 2);
} catch (Exception ex) {
ex.printStackTrace();
}
return String.format(Locale.US, "%.2f", Math.sqrt(x + y));
}

How to get android device screen size?

I have two android device with same resolution
Device1 -> resolution 480x800 diagonal screen size -> 4.7 inches
Device2 -> resolution 480x800 diagonal screen size -> 4.0 inches
How to find device diagonal screen size?
Detect 7 inch and 10 inch tablet programmatically
I have used the above link but it gives both device diagonal screen size -> 5.8
try this code to get screen size in inch
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width=dm.widthPixels;
int height=dm.heightPixels;
double wi=(double)width/(double)dm.xdpi;
double hi=(double)height/(double)dm.ydpi;
double x = Math.pow(wi,2);
double y = Math.pow(hi,2);
double screenInches = Math.sqrt(x+y);
This won't work?
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
double x = Math.pow(dm.widthPixels / dm.xdpi, 2);
double y = Math.pow(dm.heightPixels / dm.ydpi, 2);
double screenInches = Math.sqrt(x + y);
Log.d("debug", "Screen inches : " + screenInches);
Don't forget to multiply the screen size by the scaledDensity if you are doing what I did and change the size of stuff based on the screen size. e.g.:
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
double x = Math.pow(dm.widthPixels / dm.xdpi, 2);
double y = Math.pow(dm.heightPixels / dm.ydpi, 2);
double screenInches = Math.sqrt(x + y) * dm.scaledDensity;
Log.d("debug", "Screen inches : " + screenInches);
Note the second last line! Here's more info the scaledDensity stuff: What does DisplayMetrics.scaledDensity actually return in Android?
None of the above answers gave correct screen size... But the below method does it right.
private String getScreenSize() {
Point point = new Point();
((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getRealSize(point);
DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
int width=point.x;
int height=point.y;
double wi=(double)width/(double)displayMetrics.xdpi;
double hi=(double)height/(double)displayMetrics.ydpi;
double x = Math.pow(wi,2);
double y = Math.pow(hi,2);
return String.valueOf(Math.round((Math.sqrt(x+y)) * 10.0) / 10.0);
}
Note: This works only on API 17 & above.
Try this:
public static Boolean isTablet(Context context) {
if ((context.getResources().getConfiguration().screenLayout &
Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE) {
return true;
}
return false;
}
DisplayMetrics displayMetrics = context.getResources()
.getDisplayMetrics();
String screenWidthInPix = displayMetrics.widthPixels;
String screenheightInPix = displayMetrics.heightPixels;
Pythagoras theorem to find the diagonal size of Android phone/tablet screen, same principal can be applied to iPhone or Blackberry screen.
Try as below the other way:
DisplayMetrics met = new DisplayMetrics();
this.getWindowManager().getDefaultDisplay().getMetrics(met);// get display metrics object
String strSize =
new DecimalFormat("##.##").format(Math.sqrt(((met.widthPixels / met.xdpi) *
(met.widthPixels / met.xdpi)) +
((met.heightPixels / met.ydpi) * (met.heightPixels / met.ydpi))));
// using Dots per inches with width and height

how to fix the text size dynamically for different screen resolutions on android

i have a home screen with header and footer and in between i have ten rows of text to display. the text size is hard coded in java but when i run the app the footer gets stretched to adjust with the text size fixed. but it looks odd when it gets stretched. is there a way to assign the text size dynamically based on the screen. please help me with this, if you could look at the image you will understand the footer problem.
if (dpiClassification == DisplayMetrics.DENSITY_HIGH) {
if (isTablet) {
textSize = (int) (((d.getHeight() - (totImageHeight)) / 10) - (25 * dm.density));
} else {
textSize = (int) (((d.getHeight() - (totImageHeight)) / 10) - (11 * dm.density));
}
} else if (dpiClassification == DisplayMetrics.DENSITY_MEDIUM) {
if (isTablet) {
textSize = 90;// (int) (((d.getHeight() - (totImageHeight)) /
// 10) - (15 * dm.density));
} else {
textSize = (int) (((d.getHeight() - (totImageHeight)) / 10));
}
} else {
textSize = (int) (((d.getHeight() - (totImageHeight)) / 10));
}
Try to set your text size in DP and dynamically convert DP to PX and put this PX values in
*.textSize(pxValue);
To convert I use next Code:
private static DisplayMetrics metrics = null;
private static float density = 0.0;
private int dpToPxConverter(Context context, int dp){
if (metrics == null) {
metrics = context.getResources().getDisplayMetrics();
density = metrics.density;
}
return (int) (density * dp + 0.5f);
}
Hope it help you!

Categories

Resources