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

前言:

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

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

1.繪制的邏輯:

首先繪制一個(gè)矩形,然后獲取矩形長获茬、寬各自的起始位置且劃分為n等份港庄,就成了帶刻度尺的正方形,然后根據(jù)長恕曲、寬劃分等份的坐標(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)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市彬犯,隨后出現(xiàn)的幾起案子向楼,更是在濱河造成了極大的恐慌,老刑警劉巖谐区,帶你破解...
    沈念sama閱讀 206,126評論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件蜜自,死亡現(xiàn)場離奇詭異,居然都是意外死亡卢佣,警方通過查閱死者的電腦和手機(jī)参萄,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,254評論 2 382
  • 文/潘曉璐 我一進(jìn)店門泳炉,熙熙樓的掌柜王于貴愁眉苦臉地迎上來误算,“玉大人轨淌,你說我怎么就攤上這事坦胶⊙傧溃” “怎么了蓬痒?”我有些...
    開封第一講書人閱讀 152,445評論 0 341
  • 文/不壞的土叔 我叫張陵丛肢,是天一觀的道長诈乒。 經(jīng)常有香客問我罩扇,道長,這世上最難降的妖魔是什么怕磨? 我笑而不...
    開封第一講書人閱讀 55,185評論 1 278
  • 正文 為了忘掉前任喂饥,我火速辦了婚禮,結(jié)果婚禮上肠鲫,老公的妹妹穿的比我還像新娘员帮。我一直安慰自己,他們只是感情好导饲,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,178評論 5 371
  • 文/花漫 我一把揭開白布捞高。 她就那樣靜靜地躺著氯材,像睡著了一般。 火紅的嫁衣襯著肌膚如雪硝岗。 梳的紋絲不亂的頭發(fā)上氢哮,一...
    開封第一講書人閱讀 48,970評論 1 284
  • 那天,我揣著相機(jī)與錄音型檀,去河邊找鬼冗尤。 笑死,一個(gè)胖子當(dāng)著我的面吹牛贱除,可吹牛的內(nèi)容都是我干的生闲。 我是一名探鬼主播,決...
    沈念sama閱讀 38,276評論 3 399
  • 文/蒼蘭香墨 我猛地睜開眼月幌,長吁一口氣:“原來是場噩夢啊……” “哼碍讯!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起扯躺,我...
    開封第一講書人閱讀 36,927評論 0 259
  • 序言:老撾萬榮一對情侶失蹤捉兴,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后录语,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體倍啥,經(jīng)...
    沈念sama閱讀 43,400評論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 35,883評論 2 323
  • 正文 我和宋清朗相戀三年澎埠,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了虽缕。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 37,997評論 1 333
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡蒲稳,死狀恐怖氮趋,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情江耀,我是刑警寧澤剩胁,帶...
    沈念sama閱讀 33,646評論 4 322
  • 正文 年R本政府宣布,位于F島的核電站祥国,受9級特大地震影響昵观,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜舌稀,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,213評論 3 307
  • 文/蒙蒙 一啊犬、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧壁查,春花似錦椒惨、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,204評論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽领斥。三九已至,卻和暖如春沃暗,著一層夾襖步出監(jiān)牢的瞬間月洛,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 31,423評論 1 260
  • 我被黑心中介騙來泰國打工孽锥, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留嚼黔,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 45,423評論 2 352
  • 正文 我出身青樓惜辑,卻偏偏與公主長得像唬涧,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子盛撑,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,722評論 2 345

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