尽pipe设置属性,canvas绘图仍未正确绘制

我刚创build了2个图纸,试图让一个看起来像另一个,但我没有运气。 关于屏幕截图,顶部的graphics是使用canvas创build的,底部的graphics是使用XML创build的。 XML绘图是完美的,但不幸的是根据Android,我不应该使用它,因为它返回“太多的意见”的警告。 我已经尝试了以下方法的canvas绘图,但我仍然没有得到所需的结果。

我已经在十几个模拟器上testing了这个项目,尽pipeXML绘图有这么多的视图,但它们都只用了一秒钟就出现了。

  1. 根据设备的方向使用不同的图纸
  2. 使用浮点值而不是整数
  3. 调整浮点值多次

有谁知道有什么其他的select来解决这个令人讨厌的canvas问题,并确保canvas的绘制看起来与XML绘图完全一样,无论屏幕大小和方向? 在这种情况下,还应该忽略“太多的意见”的警告和/或我把查看次数限制增加到80以上? 所有相关的帮助将不胜感激。

Java代码

http://pastebin.com/VXgkJR2Z

XML代码

http://pastebin.com/JyVvxS5n

在这里输入图像说明 在这里输入图像说明

Solutions Collecting From Web of "尽pipe设置属性,canvas绘图仍未正确绘制"

下面你可以在处理你的代码后看到结果。 它几乎和xml相似,但是如果你用注意的话来看,你可以发现一些不同之处。

所以在绘画时有一些重要的事情。

  1. 不要使用int。 你失去了精确性。 我把所有的int都replace成你的代码。
  2. 小心循环。 你也在循环中放松精度。

代码中的常见模式:

 float boxWidth = (getWidth() - mSideRectWidth) / 8; // I replaced int to float for (int i = 0; i < 8; i++) { float position = i * boxWidth; // loss of precision ... } 

在循环中计算位置会更好:

 for (int i = 0; i < 8; i++) { float position = i * (getWidth() - mSideRectWidth) / 8; ... } 
  1. 不要忘记笔画宽度。 计算形状和线条的位置时,您会错过该值。

这是我的完整代码:

 import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.RectF; import android.text.TextPaint; import android.util.AttributeSet; import android.util.TypedValue; import android.view.View; public class Car extends View { private final Paint mBlackPaint = new Paint(); private final Paint mRedPaint = new Paint(); private final TextPaint mTextPaint; public static final int BOXES_COUNT = 8; private float oneDp; private float textSize; private float windowHeight; public Car(Context context, AttributeSet attrs) { super(context, attrs); oneDp = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, getResources().getDisplayMetrics()); windowHeight = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10, getResources().getDisplayMetrics()); textSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 15, getResources().getDisplayMetrics()); mRedPaint.setColor(Color.parseColor("#CC3333")); mBlackPaint.setAntiAlias(true); mBlackPaint.setColor(Color.BLACK); mBlackPaint.setStrokeWidth(oneDp); mBlackPaint.setStyle(Paint.Style.STROKE); mTextPaint = new TextPaint(TextPaint.ANTI_ALIAS_FLAG); mTextPaint.setColor(Color.BLACK); mTextPaint.setTextAlign(Paint.Align.CENTER); mTextPaint.setTextSize(textSize); mWindowPaint = new Paint(); mWindowPaint.setAntiAlias(true); mWindowPaint.setColor(Color.parseColor("#CC3333")); mWindowPaint.setStyle(Paint.Style.STROKE); mWindowPaint.setStrokeWidth(oneDp); } private Paint mWindowPaint; RectF rect = new RectF(); RectF rect2 = new RectF(); @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); if (getWidth() == 0) return; int w = canvas.getWidth(); int h = canvas.getHeight(); //draw red rectangles float mSideRectWidth = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 5, getResources().getDisplayMetrics()); canvas.drawRect(0, 0, mSideRectWidth, getHeight(), mRedPaint); //draw left end rectangle canvas.drawRect(getWidth() - mSideRectWidth, 0, getWidth(), getHeight(), mRedPaint); //draw right end rectangle //draw grey boxes setBackgroundColor(Color.parseColor("#808080")); for (int i = 0; i < BOXES_COUNT; i++) { float leftPosition = mSideRectWidth + i * oneDp + (getWidth() - mSideRectWidth * 2 - (BOXES_COUNT - 1) * oneDp) * i / BOXES_COUNT; float rightPosition = mSideRectWidth + i * oneDp + (getWidth() - mSideRectWidth * 2 - (BOXES_COUNT - 1) * oneDp) * (i + 1) / BOXES_COUNT; if (i == 0) { fillRectLeft(canvas, leftPosition, rightPosition, (i + 1) + ""); } else if ( i == BOXES_COUNT - 1) { fillRectRight(canvas, leftPosition, rightPosition, (i + 1) + ""); } else { fillRect(canvas, leftPosition, rightPosition, (i + 1) + ""); } } //draw black lines for (int i = 1; i < BOXES_COUNT; i++) { float position = mSideRectWidth + (getWidth() - mSideRectWidth * 2) * i / BOXES_COUNT; canvas.drawLine(position, 0, position, getHeight(), mBlackPaint); } } private void fillRect(Canvas canvas, float left, float right, String number) { rect.set(left + oneDp / 2, 0 + oneDp / 2, right - oneDp / 2, getHeight() - oneDp / 2); float xPos = left + ((right - left) / 2); float yPos = (canvas.getHeight() / 2) - ((mTextPaint.descent() + mTextPaint.ascent()) / 2); //((textPaint.descent() + textPaint.ascent()) / 2) is the distance from the baseline to the center. canvas.drawText(number, xPos, yPos, mTextPaint); //canvas.drawRect(rect, mWindowPaint); // top row rect2.set(left + oneDp / 2, 0 + oneDp / 2, left + (right - left) * 20 / 100 - oneDp / 2, windowHeight - oneDp / 2); canvas.drawRect(rect2, mWindowPaint); rect2.set(left + (right - left) * 27 / 100 + oneDp / 2, 0 + oneDp / 2, left + (right - left) * 47 / 100 - oneDp / 2, windowHeight - oneDp / 2); canvas.drawRect(rect2, mWindowPaint); rect2.set(left + (right - left) * 53 / 100 + oneDp / 2, 0 + oneDp / 2, left + (right - left) * 73 / 100 - oneDp / 2, windowHeight - oneDp / 2); canvas.drawRect(rect2, mWindowPaint); rect2.set(left + (right - left) * 80 / 100 + oneDp / 2, 0 + oneDp / 2, left + (right - left) * 100 / 100 - oneDp / 2, windowHeight - oneDp / 2); canvas.drawRect(rect2, mWindowPaint); // bottom row rect2.set(left + oneDp / 2, getHeight() - windowHeight + oneDp / 2, left + (right - left) * 20 / 100 - oneDp / 2, getHeight() - oneDp / 2); canvas.drawRect(rect2, mWindowPaint); rect2.set(left + (right - left) * 27 / 100 + oneDp / 2, getHeight() - windowHeight + oneDp / 2, left + (right - left) * 47 / 100 - oneDp / 2, getHeight() - oneDp / 2); canvas.drawRect(rect2, mWindowPaint); rect2.set(left + (right - left) * 53 / 100 + oneDp / 2, getHeight() - windowHeight + oneDp / 2, left + (right - left) * 73 / 100 - oneDp / 2, getHeight() - oneDp / 2); canvas.drawRect(rect2, mWindowPaint); rect2.set(left + (right - left) * 80 / 100 + oneDp / 2, getHeight() - windowHeight + oneDp / 2, left + (right - left) * 100 / 100 - oneDp / 2, getHeight() - oneDp / 2); canvas.drawRect(rect2, mWindowPaint); } private void fillRectLeft(Canvas canvas, float left, float right, String number) { rect.set(left + oneDp / 2, 0 + oneDp / 2, right - oneDp / 2, getHeight() - oneDp / 2); float xPos = left + ((right - left) / 2); float yPos = (canvas.getHeight() / 2) - ((mTextPaint.descent() + mTextPaint.ascent()) / 2); //((textPaint.descent() + textPaint.ascent()) / 2) is the distance from the baseline to the center. canvas.drawText(number, xPos, yPos, mTextPaint); //canvas.drawRect(rect, mWindowPaint); // top row rect2.set(left + (right - left) * 4 / 100 + oneDp / 2, 0 + oneDp / 2, left + (right - left) * 24 / 100 - oneDp / 2, windowHeight - oneDp / 2); canvas.drawRect(rect2, mWindowPaint); rect2.set(left + (right - left) * 42 / 100 + oneDp / 2, 0 + oneDp / 2, left + (right - left) * 62 / 100 - oneDp / 2, windowHeight - oneDp / 2); canvas.drawRect(rect2, mWindowPaint); rect2.set(left + (right - left) * 80 / 100 + oneDp / 2, 0 + oneDp / 2, left + (right - left) * 100 / 100 - oneDp / 2, windowHeight - oneDp / 2); canvas.drawRect(rect2, mWindowPaint); // bottom row rect2.set(left + (right - left) * 4 / 100 + oneDp / 2, getHeight() - windowHeight + oneDp / 2, left + (right - left) * 24 / 100 - oneDp / 2, getHeight() - oneDp / 2); canvas.drawRect(rect2, mWindowPaint); rect2.set(left + (right - left) * 42 / 100 + oneDp / 2, getHeight() - windowHeight + oneDp / 2, left + (right - left) * 62 / 100 - oneDp / 2, getHeight() - oneDp / 2); canvas.drawRect(rect2, mWindowPaint); rect2.set(left + (right - left) * 80 / 100 + oneDp / 2, getHeight() - windowHeight + oneDp / 2, left + (right - left) * 100 / 100 - oneDp / 2, getHeight() - oneDp / 2); canvas.drawRect(rect2, mWindowPaint); } private void fillRectRight(Canvas canvas, float left, float right, String number) { rect.set(left + oneDp / 2, 0 + oneDp / 2, right - oneDp / 2, getHeight() - oneDp / 2); float xPos = left + ((right - left) / 2); float yPos = (canvas.getHeight() / 2) - ((mTextPaint.descent() + mTextPaint.ascent()) / 2); //((textPaint.descent() + textPaint.ascent()) / 2) is the distance from the baseline to the center. canvas.drawText(number, xPos, yPos, mTextPaint); //canvas.drawRect(rect, mWindowPaint); // top row rect2.set(left + (right - left) * 0 / 105 + oneDp / 2, 0 + oneDp / 2, left + (right - left) * 20 / 105 - oneDp / 2, windowHeight - oneDp / 2); canvas.drawRect(rect2, mWindowPaint); rect2.set(left + (right - left) * 38 / 105 + oneDp / 2, 0 + oneDp / 2, left + (right - left) * 58 / 105 - oneDp / 2, windowHeight - oneDp / 2); canvas.drawRect(rect2, mWindowPaint); rect2.set(left + (right - left) * 78 / 105 + oneDp / 2, 0 + oneDp / 2, left + (right - left) * 98 / 105 - oneDp / 2, windowHeight - oneDp / 2); canvas.drawRect(rect2, mWindowPaint); // bottom row rect2.set(left + (right - left) * 0 / 105 + oneDp / 2, getHeight() - windowHeight + oneDp / 2, left + (right - left) * 20 / 105 - oneDp / 2, getHeight() - oneDp / 2); canvas.drawRect(rect2, mWindowPaint); rect2.set(left + (right - left) * 38 / 105 + oneDp / 2, getHeight() - windowHeight + oneDp / 2, left + (right - left) * 58 / 105 - oneDp / 2, getHeight() - oneDp / 2); canvas.drawRect(rect2, mWindowPaint); rect2.set(left + (right - left) * 78 / 105 + oneDp / 2, getHeight() - windowHeight + oneDp / 2, left + (right - left) * 98 / 105 - oneDp / 2, getHeight() - oneDp / 2); canvas.drawRect(rect2, mWindowPaint); } } 

肖像 肖像

景观 景观

把两个变体结合起来怎么样 – 用编程方式添加这些视图
addView(your_view,your_layout_params)