gpt4 book ai didi

java - 尽管设置了属性, Canvas 绘图仍未正确绘制

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:57:00 26 4
gpt4 key购买 nike

我刚刚创建了 2 个绘图,试图让一个看起来与另一个看起来一样,但我没有运气。关于屏幕截图,顶部的绘图是使用 Canvas 创建的,底部的绘图是使用 XML 创建的。 XML 绘图对我来说是完美的,但不幸的是,根据 Android,我不应该使用它,因为它会返回“太多 View ”警告。我已经尝试了以下 Canvas 绘图方法,但我仍然没有得到想要的结果。

I've tested the project on a dozen emulators and they all take no more than a second to appear despite the XML drawing having so many views.

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

有谁知道还有哪些其他选项可以解决这个烦人的 Canvas 问题,同时确保 Canvas 绘图看起来与 XML 绘图完全相同,无论屏幕大小和方向如何?在这种情况下是否也应该忽略“太多 View ”警告和/或我将 View 计数限制增加到 80 以上?所有相关帮助将不胜感激。

Java代码

http://pastebin.com/VXgkJR2Z

XML 代码

http://pastebin.com/JyVvxS5n

enter image description here enter image description here

最佳答案

您可以在下面看到处理您的代码后的结果。它与 xml 几乎相似,但是如果您使用放大镜查看它,您会发现一些差异。

所以当你画画的时候有一些重要的事情。

  1. 不要使用整数。你正在失去精度。我在您的代码中将所有 int 替换为 float。
  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);
}
}

肖像 Portrait

横向 Landscape

关于java - 尽管设置了属性, Canvas 绘图仍未正确绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32338754/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com