Android的 - 像素骤降 [英] Android - pixels to dips
问题描述
我有一个面(250像素X 250像素),它是在一个绝对的布局元件的图像。我目前得到用户的触摸坐标,并使用一些数学计算一下已经触碰(如鼻),然后做一些相应的。
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.
我的问题是如何扩大这种以适应现有的屏幕宽度。如果我设置的映像(XML)来FILL_PARENT,坐标出路。这可以通过将触摸来补救坐标逢低(如果是这样,怎么做),或将我需要得到屏幕的宽度(再次转换成蘸料),并使用更多的数学梳理协调问题?
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?
任何和所有帮助AP preciated。
Any and all help appreciated.
推荐答案
像素= DPS *(密度/ 160)
在(密度/ 160)
因子被称为密度比例系数,并得到从显示度量对象检索在Java中。你应该做的是存储鼻子等的位置在骤降
项(这是相同与密度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);
这篇关于Android的 - 像素骤降的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!