I have a app which uses co-ordinates on the image to pin a marker.
final GestureDetector gestureDetector = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener() {
#Override
public boolean onSingleTapConfirmed(MotionEvent e) {
if (imageView.isReady()) {
sCoord = imageView.viewToSourceCoord(e.getX(), e.getY());
imageView.setPin(new PointF(sCoord.x,sCoord.y));
Toast.makeText(getApplicationContext(), "Single tap: " + ((int) convertPixelsToDp(sCoord.x)) + ", " + ((int) convertPixelsToDp(sCoord.y)), Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Something is not right!", Toast.LENGTH_SHORT).show();
}
return true;
}
});
Now am doing some calculations on the point and points and changing the position of the marker. It's working fine on the device which am using but when I use another device, it's shows random location(which is obvious).
I tried this to change the co-ordinates(pixels) to dp unit and then pinning the image to that position, still it doesn't work.
/**
* This method converts dp unit to equivalent pixels, depending on device density.
*
* #param dp A value in dp (density independent pixels) unit. Which we need to convert into pixels
* #param context Context to get resources and device specific display metrics
* #return A float value to represent px equivalent to dp depending on device density
*/
public static float convertDpToPixel(float dp, Context context){
Resources resources = context.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
float px = dp * (metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT);
return px;
}
/**
* This method converts device specific pixels to density independent pixels.
*
* #param px A value in px (pixels) unit. Which we need to convert into db
* #param context Context to get resources and device specific display metrics
* #return A float value to represent dp equivalent to px value
*/
public static float convertPixelsToDp(float px, Context context){
Resources resources = context.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
float dp = px / (metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT);
return dp;
}
Let's say I have an floorplan of a store, I want a pin a marker near the door. So I take the co-ordinate and convert it to dp using above mentioned function,then I pass that dp location to another device and there am converting that dp to pixels and using that location to pin the marker, but it's not working. Showing random locations.
//trying
float x=convertDpToPixel(sCoord.x);
float y=convertDpToPixel(sCoord.y);
imageView.setPin(new PointF(x,y));
Any suggestion is welcomed. Thanks.
The approach to solve this problem is just to use a Percentage approach, to elaborate the given answer I am going to show some simple Math.
The process of this computation requires the following Values:
imageWidth = the width of the image from the left to right.
imageHeight = the height if the image from top to bottom.
xOccupation = the occupied with of the x-coordinate from left to right.
yOccupation = the occupied height of the y-coordinate from top to bottom.
To compute the xPercentage and yPercentage we are going to use this formula:
xPercentage = xOccupation/imageWidth
yPercentage = yOccupation/imageHeight
After getting those 2 values, you are now ready to send it to your server and access the same value in all kinds of device regardless of the size density.
The client device requires to recompute the x and y coordinates by doing the reverse formula.
new-x-coordinate = newImageWidth*xPercentage.
new-y-coordinate = newImageHeight*yPercentage.
Related
I am converting px to dp for given graphics. Going through docs I noticed that 1dp = 3px for 480ppi screen(xxhdpi).
I am testing it on Redmi Note3 which has approx 403 ppi.
I have been provided margins in pixels by my designer. Should I convert those into pixels to by using 1:3 ratio or it should be different
/**
* This method converts dp unit to equivalent pixels, depending on device density.
*
* #param dp A value in dp (density independent pixels) unit. Which we need to convert into pixels
* #param context Context to get resources and device specific display metrics
* #return A float value to represent px equivalent to dp depending on device density
*/
public static float convertDpToPixel(float dp, Context context){
Resources resources = context.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
float px = dp * ((float)metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT);
return px;
}
/**
* This method converts device specific pixels to density independent pixels.
*
* #param px A value in px (pixels) unit. Which we need to convert into db
* #param context Context to get resources and device specific display metrics
* #return A float value to represent dp equivalent to px value
*/
public static float convertPixelsToDp(float px, Context context){
Resources resources = context.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
float dp = px / ((float)metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT);
return dp;
}
for more information look into this SOF post Converting pixels to dp
Well, i been trying to figure out, is there a simple way to pass from dp or milimetres or even inches to px. (like: 6dp would be 20px)
It seems everything is usually done in px but i want to use these in orther to keep the proportions in different screens and have a rough sense of how big is going to be (guessing with pixels feels really...bothering)
Thanks in advance and sorry if the question is too vague.
There is no way to convert milimetres or even inches to px programmatically. If you want to support different screens, you can follow the document here.
To convert dp to px, you can also use the following method:
/**
* Convert dp to px.
* #param context
* #param dp the input dp.
* #return the output px.
*/
public static int dpToPx(Context context, int dp) {
float density = context.getResources().getDisplayMetrics().density;
return (int) (dp * density + 0.5);
}
Update:
Here is how to get a device's width and height pixels:
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
int widthPixels = metrics.widthPixels;
int heightPixels = metrics.heightPixels;
I want to place a button on image view at specific position.i.e at fixed x,y position. Is it possible to do that since android have different screen size for every device.
I have tried doing that using layout params but its not working properly.
POI3 = new Button(this);
POI3.setId(3);
POI3.setBackgroundColor(Color.TRANSPARENT);
POI3.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
onButtonClick("3");
}
});
params = new RelativeLayout.LayoutParams(75, 75);
params.leftMargin = 466;
params.topMargin = 255;
// This line defines how params.leftMargin and params.topMargin are interpreted.
// In this case, "<80,90>" means <80,90> to the right of the yellow ImageView.
params.addRule(RelativeLayout.RIGHT_OF, yellow_iv_id);
rl.addView(POI3, params);
for set your button in fix position you must use dp instead of xp,use following code to convert that:
/**
* This method converts dp unit to equivalent pixels, depending on device density.
*
* #param dp A value in dp (density independent pixels) unit. Which we need to convert into pixels
* #param context Context to get resources and device specific display metrics
* #return A float value to represent px equivalent to dp depending on device density
*/
public static float convertDpToPixel(float dp, Context context){
Resources resources = context.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
float px = dp * (metrics.densityDpi / 160f);
return px;
}
/**
* This method converts device specific pixels to density independent pixels.
*
* #param px A value in px (pixels) unit. Which we need to convert into db
* #param context Context to get resources and device specific display metrics
* #return A float value to represent dp equivalent to px value
*/
public static float convertPixelsToDp(float px, Context context){
Resources resources = context.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
float dp = px / (metrics.densityDpi / 160f);
return dp;
}
I'm adding a widget to a layout dynamically.
I need to set the widget width as 100 through code like the below :
Width = 100;
How can I convert the above given value to dps in android
/**
* This method converts device specific pixels to density independent pixels.
*
* #param px A value in px (pixels) unit. Which we need to convert into db
* #param context Context to get resources and device specific display metrics
* #return A float value to represent dp equivalent to px value
*/
public static float convertPixelsToDp(float px, Context context){
Resources resources = context.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
float dp = px / (metrics.densityDpi / 160f);
return dp;
}
Credit: Converting pixels to dp
I have an image of a face (250px X 250px) that is in an absolute layout element. I currently get the user's touch coordinates and using some maths calculate what has been touched (eg the nose), then do something accordingly.
My question is how to scale this to fit the screen width available. If I set the image (in the xml) to fill_parent, the coordinates are way out. Can this be remedied by converting the touch coordinates to dips (if so, how), or will I need to get the screen width (again convert into dips) and sort out the coordinate problem using more maths?
Any and all help appreciated.
pixels = dps * (density / 160)
The (density / 160) factor is known as the density scale factor, and get be retrieved in Java from the Display Metrics object. What you should do is store the position of the nose etc in terms of dips (which are the same as pixels on a screen with density 160), and then convert dips to pixels depending on what screen you are running on:
final static int NOSE_POSITION_DP = 10;
final float scale = getContext().getResources().getDisplayMetrics().density;
final int nosePositionPixels = (int) (NOSE_POSITION_DP * scale + 0.5f);
I have three useful functions in my library...
get Screen Density
public static float getDensity(Context context){
float scale = context.getResources().getDisplayMetrics().density;
return scale;
}
convert Dip to Pixels.
public static int convertDiptoPix(int dip){
float scale = getDensity();
return (int) (dip * scale + 0.5f);
}
convert Pixels to Dips.
public static int convertPixtoDip(int pixel){
float scale = getDensity();
return (int)((pixel - 0.5f)/scale);
}
A very simple way of doing this.
int value = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, 250, (mContext).getResources().getDisplayMetrics());
public int getDip(int pixel)
{
float scale = getBaseContext().getResources().getDisplayMetrics().density;
return (int) (pixel * scale + 0.5f);
}