paint之文字示例
package com.loaderman.customviewdemo; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Rect; import android.util.AttributeSet; import android.view.View; public class MinTextRectView extends View { public MinTextRectView(Context context, AttributeSet attrs) { super(context, attrs); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); String text = "harvic\'s blog"; int baseLineY = 90; int baseLineX = 0 ; //设置paint Paint paint = new Paint(); paint.setTextSize(60); //以px为单位 paint.setTextAlign(Paint.Align.LEFT); //画text所占的区域 Paint.FontMetricsInt fm = paint.getFontMetricsInt(); int top = baseLineY + fm.top; int bottom = baseLineY + fm.bottom; int width = (int)paint.measureText(text); Rect rect = new Rect(baseLineX,top,baseLineX+width,bottom); paint.setColor(0xA0888888); canvas.drawRect(rect,paint); //画最小矩形 Rect minRect = new Rect(); paint.getTextBounds(text,0,text.length(),minRect); minRect.top = baseLineY + minRect.top; minRect.bottom = baseLineY + minRect.bottom; paint.setColor(Color.BLACK); canvas.drawRect(minRect,paint); //写文字 paint.setColor(Color.WHITE); canvas.drawText(text, baseLineX, baseLineY, paint); } }
package com.loaderman.customviewdemo; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.util.AttributeSet; import android.view.View; public class MyTextView extends View { public MyTextView(Context context, AttributeSet attrs) { super(context, attrs); } protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawColor(Color.WHITE); int baseLineY = 90; int baseLineX = 0; Paint paint = new Paint(); //写文字 paint.setColor(Color.BLACK); paint.setTextSize(60); //以px为单位 paint.setTextAlign(Paint.Align.LEFT); canvas.drawText("harvic\'s blog", baseLineX, baseLineY, paint); //计算各线在位置 Paint.FontMetrics fm = paint.getFontMetrics(); float ascent = baseLineY + fm.ascent; float descent = baseLineY + fm.descent; float top = baseLineY + fm.top; float bottom = baseLineY + fm.bottom; //画基线 paint.setColor(Color.RED); canvas.drawLine(baseLineX, baseLineY, 3000, baseLineY, paint); //画top paint.setColor(Color.BLUE); canvas.drawLine(baseLineX, top, 3000, top, paint); //画ascent paint.setColor(Color.GREEN); canvas.drawLine(baseLineX, ascent, 3000, ascent, paint); //画descent paint.setColor(Color.GREEN); canvas.drawLine(baseLineX, descent, 3000, descent, paint); //画bottom paint.setColor(Color.RED); canvas.drawLine(baseLineX, bottom, 3000, bottom, paint); } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <com.loaderman.customviewdemo.MinTextRectView android:layout_width="match_parent" android:layout_height="300dp"/> <com.loaderman.customviewdemo.MyTextView android:layout_width="match_parent" android:layout_height="300dp"/> </LinearLayout>
效果:
最后,关注【码上加油站】微信公众号后,有疑惑有问题想加油的小伙伴可以码上加入社群,让我们一起码上加油吧!!!