- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我刚刚创建了 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.
有谁知道还有哪些其他选项可以解决这个烦人的 Canvas 问题,同时确保 Canvas 绘图看起来与 XML 绘图完全相同,无论屏幕大小和方向如何?在这种情况下是否也应该忽略“太多 View ”警告和/或我将 View 计数限制增加到 80 以上?所有相关帮助将不胜感激。
Java代码
XML 代码
最佳答案
您可以在下面看到处理您的代码后的结果。它与 xml 几乎相似,但是如果您使用放大镜查看它,您会发现一些差异。
所以当你画画的时候有一些重要的事情。
代码中的常见模式:
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;
...
}
这是我的完整代码:
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);
}
}
关于java - 尽管设置了属性, Canvas 绘图仍未正确绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32338754/
你能比较一下属性吗 我想禁用文本框“txtName”。有两种方式 使用javascript,txtName.disabled = true 使用 ASP.NET, 哪种方法更好,为什么? 最佳答案 我
Count 属性 返回一个集合或 Dictionary 对象包含的项目数。只读。 object.Count object 可以是“应用于”列表中列出的任何集合或对
CompareMode 属性 设置并返回在 Dictionary 对象中比较字符串关键字的比较模式。 object.CompareMode[ = compare] 参数
Column 属性 只读属性,返回 TextStream 文件中当前字符位置的列号。 object.Column object 通常是 TextStream 对象的名称。
AvailableSpace 属性 返回指定的驱动器或网络共享对于用户的可用空间大小。 object.AvailableSpace object 应为 Drive 
Attributes 属性 设置或返回文件或文件夹的属性。可读写或只读(与属性有关)。 object.Attributes [= newattributes] 参数 object
AtEndOfStream 属性 如果文件指针位于 TextStream 文件末,则返回 True;否则如果不为只读则返回 False。 object.A
AtEndOfLine 属性 TextStream 文件中,如果文件指针指向行末标记,就返回 True;否则如果不是只读则返回 False。 object.AtEn
RootFolder 属性 返回一个 Folder 对象,表示指定驱动器的根文件夹。只读。 object.RootFolder object 应为 Dr
Path 属性 返回指定文件、文件夹或驱动器的路径。 object.Path object 应为 File、Folder 或 Drive 对象的名称。 说明 对于驱动器,路径不包含根目录。
ParentFolder 属性 返回指定文件或文件夹的父文件夹。只读。 object.ParentFolder object 应为 File 或 Folder 对象的名称。 说明 以下代码
Name 属性 设置或返回指定的文件或文件夹的名称。可读写。 object.Name [= newname] 参数 object 必选项。应为 File 或&
Line 属性 只读属性,返回 TextStream 文件中的当前行号。 object.Line object 通常是 TextStream 对象的名称。 说明 文件刚
Key 属性 在 Dictionary 对象中设置 key。 object.Key(key) = newkey 参数 object 必选项。通常是 Dictionary 
Item 属性 设置或返回 Dictionary 对象中指定的 key 对应的 item,或返回集合中基于指定的 key 的&
IsRootFolder 属性 如果指定的文件夹是根文件夹,返回 True;否则返回 False。 object.IsRootFolder object 应为&n
IsReady 属性 如果指定的驱动器就绪,返回 True;否则返回 False。 object.IsReady object 应为 Drive&nbs
FreeSpace 属性 返回指定的驱动器或网络共享对于用户的可用空间大小。只读。 object.FreeSpace object 应为 Drive 对象的名称。
FileSystem 属性 返回指定的驱动器使用的文件系统的类型。 object.FileSystem object 应为 Drive 对象的名称。 说明 可
Files 属性 返回由指定文件夹中所有 File 对象(包括隐藏文件和系统文件)组成的 Files 集合。 object.Files object&n
我是一名优秀的程序员,十分优秀!