用自定義View繪制帶刻度尺的正方體

前言:

在接觸Android這么長(zhǎng)時(shí)間,看到很多大牛都在和大家分享自己的知識(shí),深有體會(huì),剛好前段時(shí)間寫了一個(gè)Demo,在此分享給大家。

下面就直接進(jìn)入主題:

1.繪制的邏輯:

首先繪制一個(gè)矩形,然后獲取矩形長(zhǎng)、寬各自的起始位置且劃分為n等份,就成了帶刻度尺的正方形,然后根據(jù)長(zhǎng)、寬劃分等份的坐標(biāo)點(diǎn)及正方體的角度,用drawLine()方法繪制線,最后就成了帶刻度尺的正方體。

2.代碼詳解:

2.1.繪制X、Y軸

public void drawExample(Canvas canvas, Rect rect) {

/** 繪制背景 */

canvas.drawRect(rect, paintBack);

// 需要根據(jù)矩形繪制出兩條線 (即所謂的X、Y軸線)

int sx = rect.left, sy = rect.top, ex = rect.left, ey = rect.bottom;

canvas.drawLine(sx, sy, ex, ey, linePaint);// Y縱向的線段 左

/** sx2為表格起始點(diǎn) */

int sx2 = rect.left, sy2 = rect.bottom, ex2 = rect.right, ey2 = rect.bottom;

canvas.drawLine(sx2, sy2, ex2, ey2, linePaint);// 橫向的線段 下

int sx3 = rect.left, sy3 = rect.top, ex3 = rect.right, ey3 = rect.top;

canvas.drawLine(sx3, sy3, ex3, ey3, linePaint);// 上

int sx4 = rect.right, sy4 = rect.top, ex4 = rect.right, ey4 = rect.bottom;

canvas.drawLine(sx4, sy4, ex4, ey4, linePaint);// 右

/** X軸刻度間距 */

float spaceX = (rect.right - rect.left) / xNum;

for (int i = 0; i <= xNum; i++) {// 12是X軸共12個(gè)空隙

canvas.drawLine(rect.left + spaceX * i, rect.bottom, rect.left

+ spaceX * i, rect.bottom - 8, linePaint);// 刻度

canvas.drawText(xArr[i], rect.left + spaceX * i - 8,

rect.bottom + 25, paintDegree);// 文字

}

/** Y軸刻度間距 */

float spaceY = (rect.bottom - rect.top) / yNum;

for (int i = 0; i <= yNum; i++) {// 8是Y軸共8個(gè)空隙

canvas.drawLine(rect.left, rect.top + spaceY * i, rect.left + 8,

rect.top + spaceY * i, linePaint);// 刻度

canvas.drawText(yArr[i], rect.left - 35, rect.bottom - spaceY * i

+ 8, paintDegree);// 文字

}

/** 每點(diǎn)值的距離 */

xPxSpace = (rect.right - rect.left) / (xEnd - xStart);

/** 每點(diǎn)值的距離 */

yPxSpace = (rect.bottom - rect.top) / (yEnd - yStart);

if (isShowGraticule) {// 是否需要顯示標(biāo)線

// 50 zuo===555 you===355xia===20 shang

// 下面的值 (60、80、90、100 90、120、140、160 是標(biāo)線值)

canvas.drawLine(rect.left, rect.bottom - (yMarkingOne - yStart)

* yPxSpace, rect.left + (xMarkingOne - xStart) * xPxSpace,

rect.bottom - (yMarkingOne - yStart) * yPxSpace, linePaint);// 3

canvas.drawLine(rect.left + (xMarkingOne - xStart) * xPxSpace,

rect.bottom, rect.left + (xMarkingOne - xStart) * xPxSpace,

rect.bottom - (yMarkingOne - yStart) * yPxSpace, linePaint);// 4

canvas.drawLine(rect.left + (xMarkingOne - xStart) * xPxSpace,

rect.bottom - (yMarkingOne - yStart) * yPxSpace,

rect.right, rect.top, linePaint);// 11

canvas.drawLine(rect.left, rect.bottom - (yMarkingOne - yStart)

* yPxSpace,

(rect.right - (xMarkingOne - xStart) * xPxSpace), rect.top,

linePaint);// 9

canvas.drawLine(rect.left, rect.bottom,

(rect.right - (xMarkingOne - xStart) * xPxSpace),

(yMarkingOne - yStart) * yPxSpace, linePaint);// 10

canvas.drawLine(rect.left + (xMarkingOne - xStart) * xPxSpace,

rect.bottom, (rect.right), (yMarkingOne - yStart)

* yPxSpace, linePaint);// 12

canvas.drawLine((rect.right - (xMarkingOne - xStart) * xPxSpace),

(yMarkingOne - yStart) * yPxSpace, (rect.right),

(yMarkingOne - yStart) * yPxSpace, linePaint);// 5

canvas.drawLine((rect.right - (xMarkingOne - xStart) * xPxSpace),

(yMarkingOne - yStart) * yPxSpace,

(rect.right - (xMarkingOne - xStart) * xPxSpace), rect.top,

linePaint);// 6

?? }

}

2.2.獲得一個(gè)字符串的高度

public static int getStringHeight(Paint paint, String str) {

int height = 0;

Rect rect = new Rect();

paint.getTextBounds(str, 0, str.length(), rect);// 用一個(gè)矩形去"套"字符串,獲得能完全套住字符串的最小矩形

height = rect.height();// 字符串的高度

return height;

}

2.3.繪制帶刻度尺的正方全部代碼及效果圖

package com.example.scatter;

import android.content.Context;

import android.graphics.Canvas;

import android.graphics.Color;

import android.graphics.DashPathEffect;

import android.graphics.Paint;

import android.graphics.Path;

import android.graphics.PathEffect;

import android.graphics.Rect;

import android.util.AttributeSet;

import android.view.View;

public class CustomScatterView extends View {

/** 數(shù)據(jù)接收數(shù)組 -- X軸上值 */

private int[] xData /*

* =

* {75,135,85,94,105,111,122,128,126,135,137,124,88,135

* ,148,154,172}

*/;

/** 數(shù)據(jù)接收數(shù)組 -- Y軸上值 */

private int[] yData /*

* =

* {35,45,85,64,75,81,92,88,86,85,87,84,88,75,88,94,102}

*/;

/** 繪制背景底色Paint */

private Paint paintBack;

/** 繪制刻度Paint(邊沿的刻度值) */

private Paint paintDegree, paintX, paintY, paintZ;

/** 繪制線Paint(所謂的X、Y軸線) */

private Paint linePaint;

private Paint redlinePaint;

private Paint greedlinePaint;

/** 繪制虛線Paint(即介于XY中間的線條) */

private Paint dottedPaint;

/** 繪制圓點(diǎn)Paint */

private Paint circlePaint;

/** 承載分析圖的矩形背景--矩形背景大小 */

private Rect rectBack;

/** X軸顯示值 */

private String[] xArr /*

* =

* {"60","70","80","90","100","110","120","130","140","150"

* ,"160","170","180"}

*/;

/** Y軸顯示值 */

private String[] yArr /* = {"30","40","50","60","70","80","90","100","110"} */;

/** X軸刻度數(shù)值間空隙數(shù) 默認(rèn)12 */

private float xNum = 12;

/** Y軸刻度數(shù)值間空隙數(shù) 默認(rèn)8 */

private float yNum = 8;

/** X軸的起始值 默認(rèn)60 */

private float xStart = 60;

/** X軸的結(jié)束值 默認(rèn)180 */

private float xEnd = 180;

/** Y軸的起始值 默認(rèn)30 */

private float yStart = 30;

/** Y軸的結(jié)束值 默認(rèn)110 */

private float yEnd = 110;

/** X軸值點(diǎn)之間的物理間距 */

private float xSpace;

/** Y軸值點(diǎn)之間的物理間距 */

private float ySpace;

/** X軸值點(diǎn)之間的像素間距 */

private float xPxSpace;

/** Y軸值點(diǎn)之間的像素間距 */

private float yPxSpace;

/** 是否需要顯示標(biāo)線 */

private boolean isShowGraticule = false;

/** 是否需要顯示標(biāo)線 */

public void setShowGraticule(boolean isShowGraticule) {

this.isShowGraticule = isShowGraticule;

}

// 下面的值 (60、80、90、100 90、120、140、160 是標(biāo)線值)

/** X軸的第一個(gè)標(biāo)線值 */

private int xMarkingOne = 100;

/** Y軸的第一個(gè)標(biāo)線值 */

private int yMarkingOne = 100;

/**

* 設(shè)置X、Y軸上的標(biāo)線值點(diǎn) (下面的值 均 大于0 且不等于0 否則為默認(rèn)值)

*

* @param xMarkingOne

*? ? ? ? ? ? 默認(rèn)為90

* @param xMarkingTwo

*? ? ? ? ? ? 默認(rèn)為120

* @param xMarkingThree

*? ? ? ? ? ? 默認(rèn)為140

* @param xMarkingFour

*? ? ? ? ? ? 默認(rèn)為160

* @param yMarkingOne

*? ? ? ? ? ? 默認(rèn)為60

* @param yMarkingTwo

*? ? ? ? ? ? 默認(rèn)為80

* @param yMarkingThree

*? ? ? ? ? ? 默認(rèn)為90

* @param yMarkingFour

*? ? ? ? ? ? 默認(rèn)為100

*/

public void setMarkingVlaue(int xMarkingOne, int xMarkingTwo,

int xMarkingThree, int xMarkingFour, int yMarkingOne,

int yMarkingTwo, int yMarkingThree, int yMarkingFour) {

if (xMarkingOne > 0)

this.xMarkingOne = xMarkingOne;

if (yMarkingOne > 0)

this.yMarkingOne = yMarkingOne;

}

/***

* 設(shè)置畫圖需要的值 (下面的值 均 大于0 且不等于0 否則為默認(rèn)值)

*

* @param xNum

*? ? ? ? ? ? X軸需要顯示多少(數(shù)據(jù)點(diǎn)值-1)(即數(shù)據(jù)點(diǎn)值的間隔空隙) 默認(rèn)為12

* @param yNum

*? ? ? ? ? ? Y軸需要顯示多少(數(shù)據(jù)點(diǎn)值-1)(即數(shù)據(jù)點(diǎn)值的間隔空隙) 默認(rèn)為8

* @param xStart

*? ? ? ? ? ? X軸開始值 默認(rèn)為 60

* @param xEnd

*? ? ? ? ? ? X軸結(jié)束值 默認(rèn)為 180

* @param yStart

*? ? ? ? ? ? Y軸開始值 默認(rèn)為 30

* @param yEnd

*? ? ? ? ? ? Y軸結(jié)束值 默認(rèn)為 110

*/

public void setValue(float xNum, float yNum, float xStart, float xEnd,

float yStart, float yEnd) {

if (xNum != 0.0 && xNum > 0)

this.xNum = xNum;

if (yNum != 0.0 && yNum > 0)

this.yNum = yNum;

if (xStart <= xEnd && xStart >= 0) {

this.xStart = xStart;

this.xEnd = xEnd;

}

if (yStart <= yEnd && yStart >= 0) {

this.yStart = yStart;

this.yEnd = yEnd;

}

}

/**

* 給圖形設(shè)置值

*

* @param xData

*? ? ? ? ? ? X軸需要顯示的值

* @param yData

*? ? ? ? ? ? Y軸需要顯示的值

*/

public void setVlaueData(int[] xData, int[] yData) {

this.xData = xData;

this.yData = yData;

invalidate();

}

public CustomScatterView(Context context) {

super(context, null, 0);

}

public CustomScatterView(Context context, AttributeSet attrs) {

super(context, attrs, 0);

}

public CustomScatterView(Context context, AttributeSet attrs, int defStyle) {

super(context, attrs, defStyle);

}

/** 初始化信息 */

private void init() {

// 初始化默認(rèn)值

paintBack = new Paint();// 畫筆

paintBack.setColor(getResources().getColor(R.color.bar_view_bg));// 默認(rèn)顏色

paintBack.setStyle(Paint.Style.FILL);// 填充色

redlinePaint = new Paint();

redlinePaint.setColor(Color.RED);

redlinePaint.setStrokeWidth(2);

redlinePaint.setStyle(Paint.Style.STROKE);

redlinePaint.setAntiAlias(true);

PathEffect effect = new DashPathEffect(new float[] { 1, 2, 4, 8 }, 1);

redlinePaint.setPathEffect(effect);

greedlinePaint = new Paint();

greedlinePaint.setColor(Color.GREEN);

greedlinePaint.setStrokeWidth(2);

greedlinePaint.setStrokeCap(Paint.Cap.ROUND);

greedlinePaint.setAntiAlias(true);

// 繪制刻度

paintDegree = new Paint();

paintDegree.setColor(Color.BLACK);

paintDegree.setTextSize(18);

paintDegree.setAntiAlias(true);// 鋸齒不顯示

paintX = new Paint();

paintX.setColor(Color.BLACK);

paintX.setTextSize(18);

paintX.setAntiAlias(true);// 鋸齒不顯示

paintY = new Paint();

paintY.setColor(Color.BLUE);

paintY.setTextSize(18);

paintY.setAntiAlias(true);// 鋸齒不顯示

paintZ = new Paint();

paintZ.setColor(Color.BLACK);

paintZ.setTextSize(18);

paintZ.setAntiAlias(true);// 鋸齒不顯示

// 繪制線

linePaint = new Paint();//

linePaint.setColor(Color.BLACK);

/** 設(shè)置線的寬度 **/

linePaint.setStrokeWidth(2);

/** 設(shè)置畫筆變?yōu)閳A滑狀 **/

linePaint.setStrokeCap(Paint.Cap.ROUND);

linePaint.setAntiAlias(true);// 鋸齒不顯示

/** 繪制虛線 */

dottedPaint = new Paint();

dottedPaint.setAntiAlias(true);

dottedPaint.setStyle(Paint.Style.STROKE);

dottedPaint.setColor(Color.GRAY);

PathEffect effects = new DashPathEffect(new float[] { 5, 5, 5, 5 }, 1);

dottedPaint.setPathEffect(effects);

// 繪制比例小矩形

circlePaint = new Paint();

circlePaint.setColor(Color.RED);// 默認(rèn)顏色

circlePaint.setStyle(Paint.Style.FILL);// 填充色

circlePaint.setAntiAlias(true);// 鋸齒不顯示

xSpace = (xEnd - xStart) / xNum;

ySpace = (yEnd - yStart) / yNum;

xArr = new String[(int) (xNum + 1)];

yArr = new String[(int) (yNum + 1)];

for (int i = 0; i <= xNum; i++) {

xArr[i] = (int) (xStart + xSpace * i) + "";

}

for (int i = 0; i <= yNum; i++) {

yArr[i] = (int) (yStart + ySpace * i) + "";

}

rectBack = new Rect(50, 20, getWidth() - 45, getHeight() - 45);// 默認(rèn)矩形大小位置

}

@Override

protected void onLayout(boolean changed, int left, int top, int right,

int bottom) {

super.onLayout(changed, left, top, right, bottom);

init();

}

@Override

protected void onDraw(Canvas canvas) {

try {

super.onDraw(canvas);

// 設(shè)置畫布背景顏色

canvas.drawColor(Color.WHITE);

drawExample(canvas, rectBack);

// drawCircle(canvas,rectBack,xData,yData);

drawLine3(canvas, rectBack, x, y, z);

} catch (Exception e) {

e.printStackTrace();

}

}

private float x, y, z;

public void setVlaueData(float x, float y, float z) {

this.x = x;

this.y = y;

this.z = z;

invalidate();

}

private static float xishu = (float) (30 / 45.0);// sina

private static float xishu1 = (float) (38 / 45.0);// cosa

/**

* 畫3根線

*

* @param yData

* @param xData

*/

private void drawLine3(Canvas canvas, Rect rect, float x, float y, float z) {

// 繪制3根線

// float x0 = (float) (x + z * Math.cos(45*Math.PI/180));

// float y0 = (float) (y + z * Math.sin(45*Math.PI/180));

//

// float x1 = (float) (0 + z * Math.cos(45*Math.PI/180));

// float y1 = (float) (0 + z * Math.sin(45*Math.PI/180));

//

// float x2 = (float) (0 + 0 * Math.cos(45*Math.PI/180));

// float y2 = (float) (y + 0 * Math.sin(45*Math.PI/180));

//

// float x3 = (float) (x + 0 * Math.cos(45*Math.PI/180));

// float y3 = (float) (0 + 0 * Math.sin(45*Math.PI/180));

float x0 = (float) (x + z * xishu);

float y0 = (float) (y + z * xishu1);

float x1 = (float) (x + 0 * xishu);

float y1 = (float) (y + 0 * xishu1);

float x2 = (float) (0 + z * xishu);

float y2 = (float) (y + z * xishu1);

float x3 = (float) (x + z * xishu);

float y3 = (float) (0 + z * xishu1);

float x11 = (float) (x + 0 * xishu);

float y11 = (float) (0 + 0 * xishu1);

float x22 = (float) (0 + 0 * xishu);

float y22 = (float) (y + 0 * xishu1);

float x33 = (float) (0 + z * xishu);

float y33 = (float) (0 + z * xishu1);

// canvas.drawLine(rect.left + (x1-xStart)*xPxSpace, rect.bottom -

// (y1-yStart)*yPxSpace, rect.left + (x0-xStart)*xPxSpace, rect.bottom -

// (y0-yStart)*yPxSpace, redlinePaint);//x=0

// canvas.drawLine(rect.left + (x2-xStart)*xPxSpace, rect.bottom -

// (y2-yStart)*yPxSpace, rect.left + (x0-xStart)*xPxSpace, rect.bottom -

// (y0-yStart)*yPxSpace, redlinePaint);//y=0

// canvas.drawLine(rect.left + (x3-xStart)*xPxSpace, rect.bottom -

// (y3-yStart)*yPxSpace, rect.left + (x0-xStart)*xPxSpace, rect.bottom -

// (y0-yStart)*yPxSpace, redlinePaint);//z=0

DashPathEffect pathEffect = new DashPathEffect(new float[] { 3, 2 }, 0);

redlinePaint.setPathEffect(pathEffect);

Path path1 = new Path();

path1.moveTo(rect.left + (x1 - xStart) * xPxSpace, rect.bottom

- (y1 - yStart) * yPxSpace);

path1.lineTo(rect.left + (x0 - xStart) * xPxSpace, rect.bottom

- (y0 - yStart) * yPxSpace);

canvas.drawPath(path1, redlinePaint);

Path path2 = new Path();

path2.moveTo(rect.left + (x2 - xStart) * xPxSpace, rect.bottom

- (y2 - yStart) * yPxSpace);

path2.lineTo(rect.left + (x0 - xStart) * xPxSpace, rect.bottom

- (y0 - yStart) * yPxSpace);

canvas.drawPath(path2, redlinePaint);

Path path3 = new Path();

path3.moveTo(rect.left + (x3 - xStart) * xPxSpace, rect.bottom

- (y3 - yStart) * yPxSpace);

path3.lineTo(rect.left + (x0 - xStart) * xPxSpace, rect.bottom

- (y0 - yStart) * yPxSpace);

canvas.drawPath(path3, redlinePaint);

String xyz = "(" + x + "," + y + "," + z + ")";

// canvas.drawText(xyz,rect.left + (x0-xStart)*xPxSpace+20, rect.bottom

// - (y0-yStart)*yPxSpace-20, paintDegree);// 文字

canvas.drawText("(" + x + ",", rect.left + (x0 - xStart) * xPxSpace

+ 20, rect.bottom - (y0 - yStart) * yPxSpace - 20, paintX);// 文字

canvas.drawText(y + "", rect.left + (x0 - xStart) * xPxSpace + 70,

rect.bottom - (y0 - yStart) * yPxSpace - 20, paintY);// 文字

canvas.drawText("," + z + ")", rect.left + (x0 - xStart) * xPxSpace

+ 110, rect.bottom - (y0 - yStart) * yPxSpace - 20, paintZ);// 文字

canvas.drawLine(rect.left + (x1 - xStart) * xPxSpace, rect.bottom

- (y1 - yStart) * yPxSpace, rect.left + (x11 - xStart)

* xPxSpace, rect.bottom - (y11 - yStart) * yPxSpace,

greedlinePaint);// z=0

canvas.drawLine(rect.left + (x1 - xStart) * xPxSpace, rect.bottom

- (y1 - yStart) * yPxSpace, rect.left + (x22 - xStart)

* xPxSpace, rect.bottom - (y22 - yStart) * yPxSpace,

greedlinePaint);// z=0

canvas.drawLine(rect.left + (x3 - xStart) * xPxSpace, rect.bottom

- (y3 - yStart) * yPxSpace, rect.left + (x11 - xStart)

* xPxSpace, rect.bottom - (y11 - yStart) * yPxSpace,

greedlinePaint);// z=0

canvas.drawLine(rect.left + (x2 - xStart) * xPxSpace, rect.bottom

- (y2 - yStart) * yPxSpace, rect.left + (x22 - xStart)

* xPxSpace, rect.bottom - (y22 - yStart) * yPxSpace,

greedlinePaint);// z=0

canvas.drawLine(rect.left + (x3 - xStart) * xPxSpace, rect.bottom

- (y3 - yStart) * yPxSpace, rect.left + (x33 - xStart)

* xPxSpace, rect.bottom - (y33 - yStart) * yPxSpace,

greedlinePaint);// z=0

canvas.drawLine(rect.left + (x2 - xStart) * xPxSpace, rect.bottom

- (y2 - yStart) * yPxSpace, rect.left + (x33 - xStart)

* xPxSpace, rect.bottom - (y33 - yStart) * yPxSpace,

greedlinePaint);// z=0

}

/**

* 畫圓

*

* @param yData

* @param xData

*/

private void drawCircle(Canvas canvas, Rect rect, int[] xData, int[] yData) {

if (xData == null || yData == null) {

return;

}

for (int i = 0; i < xData.length; i++) {

canvas.drawCircle(rect.left + (xData[i] - xStart) * xPxSpace,

rect.bottom - (yData[i] - yStart) * yPxSpace, 5,

circlePaint);

}

}

/**

* 繪制一個(gè)完整背景圖形

*

* @param canvas

*? ? ? ? ? ? 畫布

* @param rect

*? ? ? ? ? ? 區(qū)域

*/

public void drawExample(Canvas canvas, Rect rect) {

/** 繪制背景 */

canvas.drawRect(rect, paintBack);

// 需要根據(jù)矩形繪制出兩條線 (即所謂的X、Y軸線)

int sx = rect.left, sy = rect.top, ex = rect.left, ey = rect.bottom;

canvas.drawLine(sx, sy, ex, ey, linePaint);// Y縱向的線段 左

/** sx2為表格起始點(diǎn) */

int sx2 = rect.left, sy2 = rect.bottom, ex2 = rect.right, ey2 = rect.bottom;

canvas.drawLine(sx2, sy2, ex2, ey2, linePaint);// 橫向的線段 下

int sx3 = rect.left, sy3 = rect.top, ex3 = rect.right, ey3 = rect.top;

canvas.drawLine(sx3, sy3, ex3, ey3, linePaint);// 上

int sx4 = rect.right, sy4 = rect.top, ex4 = rect.right, ey4 = rect.bottom;

canvas.drawLine(sx4, sy4, ex4, ey4, linePaint);// 右

/** X軸刻度間距 */

float spaceX = (rect.right - rect.left) / xNum;

for (int i = 0; i <= xNum; i++) {// 12是X軸共12個(gè)空隙

canvas.drawLine(rect.left + spaceX * i, rect.bottom, rect.left

+ spaceX * i, rect.bottom - 8, linePaint);// 刻度

canvas.drawText(xArr[i], rect.left + spaceX * i - 8,

rect.bottom + 25, paintDegree);// 文字

}

/** Y軸刻度間距 */

float spaceY = (rect.bottom - rect.top) / yNum;

for (int i = 0; i <= yNum; i++) {// 8是Y軸共8個(gè)空隙

canvas.drawLine(rect.left, rect.top + spaceY * i, rect.left + 8,

rect.top + spaceY * i, linePaint);// 刻度

canvas.drawText(yArr[i], rect.left - 35, rect.bottom - spaceY * i

+ 8, paintDegree);// 文字

}

/** 每點(diǎn)值的距離 */

xPxSpace = (rect.right - rect.left) / (xEnd - xStart);

/** 每點(diǎn)值的距離 */

yPxSpace = (rect.bottom - rect.top) / (yEnd - yStart);

if (isShowGraticule) {// 是否需要顯示標(biāo)線

// 50 zuo===555 you===355xia===20 shang

// 下面的值 (60、80、90、100 90、120、140、160 是標(biāo)線值)

canvas.drawLine(rect.left, rect.bottom - (yMarkingOne - yStart)

* yPxSpace, rect.left + (xMarkingOne - xStart) * xPxSpace,

rect.bottom - (yMarkingOne - yStart) * yPxSpace, linePaint);// 3

canvas.drawLine(rect.left + (xMarkingOne - xStart) * xPxSpace,

rect.bottom, rect.left + (xMarkingOne - xStart) * xPxSpace,

rect.bottom - (yMarkingOne - yStart) * yPxSpace, linePaint);// 4

canvas.drawLine(rect.left + (xMarkingOne - xStart) * xPxSpace,

rect.bottom - (yMarkingOne - yStart) * yPxSpace,

rect.right, rect.top, linePaint);// 11

canvas.drawLine(rect.left, rect.bottom - (yMarkingOne - yStart)

* yPxSpace,

(rect.right - (xMarkingOne - xStart) * xPxSpace), rect.top,

linePaint);// 9

canvas.drawLine(rect.left, rect.bottom,

(rect.right - (xMarkingOne - xStart) * xPxSpace),

(yMarkingOne - yStart) * yPxSpace, linePaint);// 10

canvas.drawLine(rect.left + (xMarkingOne - xStart) * xPxSpace,

rect.bottom, (rect.right), (yMarkingOne - yStart)

* yPxSpace, linePaint);// 12

canvas.drawLine((rect.right - (xMarkingOne - xStart) * xPxSpace),

(yMarkingOne - yStart) * yPxSpace, (rect.right),

(yMarkingOne - yStart) * yPxSpace, linePaint);// 5

canvas.drawLine((rect.right - (xMarkingOne - xStart) * xPxSpace),

(yMarkingOne - yStart) * yPxSpace,

(rect.right - (xMarkingOne - xStart) * xPxSpace), rect.top,

linePaint);// 6

}

}

/** 獲得一個(gè)字符串的高度 */

public static int getStringHeight(Paint paint, String str) {

int height = 0;

Rect rect = new Rect();

paint.getTextBounds(str, 0, str.length(), rect);// 用一個(gè)矩形去"套"字符串,獲得能完全套住字符串的最小矩形

height = rect.height();// 字符串的高度

return height;

??? }

}

效果圖:


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容