Resources r = getResources();
float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 14, r.getDisplayMetrics());
这种神奇的代码很有用(dp to px,sp to px...)
TypedValue有很多单位
/** {@link #TYPE_DIMENSION} complex unit: Value is raw pixels. */
public static final int COMPLEX_UNIT_PX = 0;
/** {@link #TYPE_DIMENSION} complex unit: Value is Device Independent
* Pixels. */
public static final int COMPLEX_UNIT_DIP = 1;
/** {@link #TYPE_DIMENSION} complex unit: Value is a scaled pixel. */
public static final int COMPLEX_UNIT_SP = 2;
/** {@link #TYPE_DIMENSION} complex unit: Value is in points. */
public static final int COMPLEX_UNIT_PT = 3;
/** {@link #TYPE_DIMENSION} complex unit: Value is in inches. */
public static final int COMPLEX_UNIT_IN = 4;
...
发现于:http://stackoverflow.com/questions/4605527/converting-pixels-to-dp-in-android
Android View用到的一些代码
填充图形:setStyle(Style.FILL); 不进行填充:setStyle(Style.STROKE); 抗锯齿:paint.setAntiAlias(true); canvas.setDrawFilter(new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG|Paint.FILTER_BITMAP_FLAG)); 设置笔触宽度(线的宽度):paint.setStrokeWidth(float width); 绘制圆形:canvas.drawCircle(float cx, float cy, float radius, Paint paint); 绘制矩形:canvas.drawRect(float left, float top, float right, float bottom, Paint paint); 绘制圆角矩形:canvas.drawRoundRect(RectF rect, float rx, float ry, Paint paint); 绘制文字:canvas.drawText(String text, float x, float y, Paint paint); 绘制椭圆:canvas.drawOval(RectF oval, Paint paint); 绘制弧:canvas.drawArc(oval, startAngle, sweepAngle, useCenter, paint); 绘制多边形:path.moveTo(float x, float y); path.linTo(float x, float y); path.close(); canvas.drawPath(Path path, Paint paint);