Devide独立性Canvas类的图元 [英] Devide independence for Canvas class drawing primitives

查看:220
本文介绍了Devide独立性Canvas类的图元的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Android的Canvas类支持一组丰富的绘图图元 - 圆,直线,等我有一个使用这些应用程式绘制一些统计数据。

The Android Canvas class supports a rich set of drawing primitives - circles, lines, etc. I have an app that uses these to chart some statistical data.

阅读说明书上的<一后href="http://developer.android.com/reference/android/graphics/Canvas.html#drawLine%28float,%20float,%20float,%20float,%20android.graphics.Paint%29">http://developer.android.com/reference/android/graphics/Canvas.html#drawLine%28float,%20float,%20float,%20float,%20android.graphics.Paint%29 。 。 。我不清楚的坐标是或什么单位如何使他们的设备/解决方案的独立。

After reading the description on http://developer.android.com/reference/android/graphics/Canvas.html#drawLine%28float,%20float,%20float,%20float,%20android.graphics.Paint%29 . . . I was unclear what units the coordinates were in or how to make them device / resolution independent.

什么单位是上述的,什么是最佳实践为画线和圆圈,并在许多不同的屏幕尺寸和分辨率的这项工作矩形?先谢谢了。

What units are these in and what's "best practice" for drawing lines and circles and rectangles that work on lots of different screen sizes and resolutions? Thanks in advance.

推荐答案

Android的文件说,该单位的位置和尺寸是像素。试验了一段时间后,我发现之前的2.0版本的单位是像素。但是,从2.0开始的单元是最有可能的倾角(设备无关的像素)。

Android documentation says "The unit for location and dimensions is the pixel". After experimenting for a while I found out that prior to version 2.0 the unit was the pixel. But starting from 2.0 the unit is most likely the dip (device-independent pixel).

有关下列code:

Paint paint = new Paint();
paint.setColor(Color.RED);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(0);
canvas.drawRect(new Rect(1, 1, 319, 319), paint);

我得到同样大小的方形分辨率320x480,480x800的匹配屏幕的宽度,240x320的仿真器与Android 2.0 +。

I get the same size square matching the width of the screen on 320x480, 480x800, and 240x320 emulators with android 2.0+.

此发现使我要解决的问题:在大屏幕上的1像素的垂直线,有时2像素宽。设置描边宽度为0,绘制1个像素的线独立的屏幕尺寸。

This discovery allowed me to solve the problem: 1-pixel vertical lines on a large screen are sometimes 2-pixel wide. Set stroke width to 0 to draw 1-pixel lines independent of the screen size.

修改

获得更多的经验与Android之后,我需要纠正我的结论。其实,有一个属性机器人:anyDensity在AndroidManifest.xml的支持屏的标签。此属性默认为true。如果这是真的,度量单位是DP,否则它是一个像素。

After getting more experience with Android, I need to correct my conclusions. Actually there is an attribute "android:anyDensity" in the "supports-screens" tag of the AndroidManifest.xml. This attribute is true by default. When it's true, the unit of measure is dp, otherwise it's a pixel.

这篇关于Devide独立性Canvas类的图元的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆