本文介绍了在借鉴Android的3×3平方厘米的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要在屏幕上绘制一个确切的3×3平方厘米,你需要在任何类型的屏幕作为设计工作的确切尺寸。
I need to draw an exact square 3x3 cm on the screen, you need the exact dimensions in any type of screen as design work.
在三星王牌,结果为2.8厘米,结果不准确。
The result in a Samsung Ace is 2.8 cm, the result is not exact.
Java的...
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
float ld = 30 * metrics.densityDpi * (metrics.density/25.4f);
bitmap = Bitmap.createBitmap((int) ld, (int) ld, Bitmap.Config.ARGB_8888);
canvas = new Canvas(bitmap);
paint = new Paint();
paint.setColor(Color.WHITE);
imageView.setImageBitmap(bitmap);
canvas.drawRect(0, 0, (float) ld, (float) ld, paint);
XML ...
XML...
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/image_view"
android:scaleType="center"/>
非常感谢你。
推荐答案
我认为你需要使用DPI在具体x和y方向如:
I think you need to use the dpi in the specific directions x and y eg:
// We want 30/25.4 inches but in dots
float inches = 30/25.4f;
float xdpi = getResources().getDisplayMetrics().xdpi;
float xDots = inches * xdpi;
float ydpi = getResources().getDisplayMetrics().ydpi;
float yDots = inches * ydpi;
Paint paint = new Paint();
paint.setColor(Color.WHITE);
canvas.drawRect(0, 0, xDots, yDots, paint);
然后,你将有更多的机会,但他们是不准确的每一个电话。
Then you will have more chance, but they are not accurate for every phone. I got the following results after placing this code in the onDraw() method of and Android app:
Nexus 7: 2.8 cm
Galaxy Nexus 3.0 cm
HTC Sensation 3.0 cm
HTC Desire HD 2.7 cm
因此,这不是在所有情况下精确。
So it is not exact in all cases.
这篇关于在借鉴Android的3×3平方厘米的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!