Java 在画布上绘制具有多重大小的多条线
我已经知道如何在画布上画多条线了 但目前我只能为它们设置一次文本大小 现在,我想在画布上画多条大小不同的线,细节如下:Java 在画布上绘制具有多重大小的多条线,java,android,canvas,Java,Android,Canvas,我已经知道如何在画布上画多条线了 但目前我只能为它们设置一次文本大小 现在,我想在画布上画多条大小不同的线,细节如下: 正文1:尺寸16 文本2:大小32 文本3:尺寸14 实际上我不知道怎么做 知道的人请告诉我更多细节 谢谢, p/s:在画布上绘制多条线的示例代码我做了: 关于文本大小和位置的多行显示错误,因为我计算错误,我仍在检查 private TextPaint mTp; private String[] strings = { " Topics : Asukab
- 正文1:尺寸16
- 文本2:大小32
- 文本3:尺寸14
private TextPaint mTp;
private String[] strings = {
" Topics : Asukabu Tawain \n ",
"512 \n ",
"Comments \n",
"7:30 22/12/2017"
};
private float height = 0;
public CustomImageView(Context context) {
super(context);
// Initialize a new Bitmap object
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inMutable = true;
Bitmap mutableBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.red_circle, opt);
mTp = new TextPaint(Paint.ANTI_ALIAS_FLAG);
// text color - #3D3D3D
mTp.setColor(Color.WHITE);
// text shadow
mTp.setShadowLayer(1f, 0f, 1f, Color.WHITE);
textWidth = mutableBitmap.getWidth();
setImageBitmap(mutableBitmap);
}
public CustomImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomImageView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public CustomImageView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
x = (canvas.getWidth() - textWidth) / 2;
for (int i = 0; i < strings.length; i++) {
if (canvas.getWidth() < 150)
mTp.setTextSize(0);
else
mTp.setTextSize(determineMaxTextSize(strings[i], height/2));
StaticLayout mSl = new StaticLayout(
strings[i], mTp, textWidth, Layout.Alignment.ALIGN_CENTER, 1.0f, 0.0f, false);
// get height of multiline text
int textHeight = mSl.getHeight();
// get position of text's top left corner
y = (canvas.getHeight() - textHeight) / 2;
// draw text to the Canvas center
canvas.save();
canvas.translate(x, y);
mSl.draw(canvas);
canvas.restore();
}
}
public void setHeight(float height) {
this.height = height;
}
private int determineMaxTextSize(String str, float maxWidth){
int size = 0;
Paint paint = new Paint();
do {
paint.setTextSize(++ size);
} while(paint.measureText(str) < maxWidth);
return size;
}
private TextPaint mTp;
私有字符串[]字符串={
“主题:Asukabu Tawain\n”,
“512\n”,
“评论\n”,
"7:30 22/12/2017"
};
专用浮子高度=0;
公共CustomImageView(上下文){
超级(上下文);
//初始化一个新的位图对象
Options=new-BitmapFactory.Options();
opt.inMutable=true;
Bitmap mutableBitmap=BitmapFactory.decodeResource(getResources(),R.mipmap.red_circle,opt);
mTp=新的文本绘制(绘制.防锯齿标记);
//文本颜色-#3d3D
mTp.setColor(颜色为白色);
//文本阴影
mTp设置阴影层(1f、0f、1f、彩色、白色);
textWidth=mutableBitmap.getWidth();
setImageBitmap(可变位图);
}
公共CustomImageView(上下文、属性集属性){
超级(上下文,attrs);
}
公共CustomImageView(上下文上下文、属性集属性、int defStyleAttr){
super(上下文、attrs、defStyleAttr);
}
公共CustomImageView(上下文上下文、属性集属性、int-defStyleAttr、int-defStyleRes){
super(context、attrs、defStyleAttr、defStyleRes);
}
@凌驾
受保护的void onDraw(画布){
super.onDraw(帆布);
x=(canvas.getWidth()-textWidth)/2;
for(int i=0;i
只需使用3种不同的TextPaint
即可绘制3种不同大小的文本
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int width = canvas.getWidth();
int height = width;
paint.setColor(Color.RED);
paint.setAntiAlias(true);
canvas.drawCircle(width/2,height/2,width/2 - 10,paint);
textPaint1.setColor(Color.WHITE);
textPaint1.setTextSize(36);
canvas.drawText("You received",width/2 - 108,height/2 - 80,textPaint1);
textPaint2.setTextSize(72);
textPaint2.setColor(Color.WHITE);
canvas.drawText("135",width/2 - 72,height/2,textPaint2);
textPaint3.setTextSize(32);
textPaint3.setColor(Color.WHITE);
canvas.drawText("comments",width/2 - 96,height/2 + 40,textPaint3);
}
另一个建议是:不要在onDraw方法中创建新对象,因为它会在每次用户界面更新时被调用,创建这么多对象很容易触发GC并导致性能下降。只需使用3个不同的
TextPaint
即可绘制3个不同大小的文本
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int width = canvas.getWidth();
int height = width;
paint.setColor(Color.RED);
paint.setAntiAlias(true);
canvas.drawCircle(width/2,height/2,width/2 - 10,paint);
textPaint1.setColor(Color.WHITE);
textPaint1.setTextSize(36);
canvas.drawText("You received",width/2 - 108,height/2 - 80,textPaint1);
textPaint2.setTextSize(72);
textPaint2.setColor(Color.WHITE);
canvas.drawText("135",width/2 - 72,height/2,textPaint2);
textPaint3.setTextSize(32);
textPaint3.setColor(Color.WHITE);
canvas.drawText("comments",width/2 - 96,height/2 + 40,textPaint3);
}
另一个建议是:不要在
onDraw
方法中创建新对象,因为它会在每次用户界面更新时被调用,创建这么多对象会很容易触发GC并导致性能下降。你的想法很好。但我需要自动计算x
y
,还需要为文本值设置重心。就像我更新的图片一样,请看一看。你的想法很好。但我需要自动计算x
y
,还需要为文本值设置重心。就像我更新的图片一样,请看一看。