Skip to content

Commit 21bb356

Browse files
committed
新增Paint工具类,支持测量字体宽度和字体高度
1 parent c05e542 commit 21bb356

File tree

7 files changed

+86
-15
lines changed

7 files changed

+86
-15
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ allprojects {
2323

2424
```
2525
dependencies {
26-
compile 'com.github.AllenCoder.SuperUtils:apputils:1.0.4'
26+
compile 'com.github.AllenCoder.SuperUtils:apputils:1.0.5'
2727
}
2828
```
2929

@@ -53,7 +53,7 @@ dependencies {
5353
| SecretUtils | 3DES 加密/解密 | [SecretUtils][24] | |
5454
| ToastUtils | Toast工具类(需要Utils.init(context)) | [ToastUtils][25] | |
5555
| IOUtil | IOUtil (文件操作工具) | [IOUtil][26] | |
56-
56+
| PaintUtil | PaintUtil (测量字体大小和字体高度) | [PaintUtil][27] | |
5757

5858

5959
### 2.Android 数据库处理工具类
@@ -65,7 +65,7 @@ dependencies {
6565

6666
```
6767
dependencies {
68-
compile 'com.github.AllenCoder.SuperUtils:dbutils:1.0.4'
68+
compile 'com.github.AllenCoder.SuperUtils:dbutils:1.0.5'
6969
}
7070
```
7171

@@ -79,7 +79,7 @@ dependencies {
7979

8080
```
8181
dependencies {
82-
compile 'com.github.AllenCoder.SuperUtils:mediautil:1.0.4'
82+
compile 'com.github.AllenCoder.SuperUtils:mediautil:1.0.5'
8383
}
8484
8585
```
@@ -130,3 +130,4 @@ dependencies {
130130
[24]: https://github.com/AllenCoder/SuperUtils/blob/master/apputils/src/main/java/com/allen/apputils/SecretUtils.java
131131
[25]: https://github.com/AllenCoder/SuperUtils/blob/master/apputils/src/main/java/com/allen/apputils/ToastUtils.java
132132
[26]: https://github.com/AllenCoder/SuperUtils/blob/master/apputils/src/main/java/com/allen/apputils/IOUtil.java
133+
[27]: https://github.com/AllenCoder/SuperUtils/blob/master/apputils/src/main/java/com/allen/apputils/PaintUtil.java

app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ android {
2323
applicationId "com.allen.supperutils"
2424
minSdkVersion rootProject.ext.minSdkVersion
2525
targetSdkVersion rootProject.ext.targetSdkVersion
26-
versionCode 1
27-
versionName "1.0"
26+
versionCode rootProject.ext.versionCode
27+
versionName rootProject.ext.versionName
2828
}
2929
buildTypes {
3030
release {

apputils/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ android {
2424
defaultConfig {
2525
minSdkVersion rootProject.ext.minSdkVersion
2626
targetSdkVersion rootProject.ext.targetSdkVersion
27-
versionCode 1
28-
versionName "1.0"
27+
versionCode rootProject.ext.versionCode
28+
versionName rootProject.ext.versionName
2929
}
3030
buildTypes {
3131
release {
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright 2017 [AllenCoderr]
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.allen.apputils;
18+
19+
import android.graphics.Paint;
20+
import android.graphics.Rect;
21+
22+
/**
23+
* 文 件 名: PaintUtil
24+
* 创 建 人: Allen
25+
* 创建日期: 2017/5/3 11:15
26+
* 修改时间:
27+
* 修改备注:
28+
*/
29+
30+
public class PaintUtil {
31+
private PaintUtil() {
32+
}
33+
34+
/**
35+
* calculates the approximate width of a text, depending on a demo text
36+
* avoid repeated calls (e.g. inside drawing methods)
37+
*
38+
* @param paint
39+
* @param demoText
40+
* @return
41+
*/
42+
public static int calcTextWidth(Paint paint, String demoText) {
43+
return (int) paint.measureText(demoText);
44+
}
45+
/**
46+
* calculates the approximate height of a text, depending on a demo text
47+
* avoid repeated calls (e.g. inside drawing methods)
48+
*
49+
* @param paint
50+
* @param demoText
51+
* @return
52+
*/
53+
public static int calcTextHeight(Paint paint, String demoText) {
54+
55+
Rect r = new Rect();
56+
paint.getTextBounds(demoText, 0, demoText.length(), r);
57+
return r.height();
58+
}
59+
60+
public static float getLineHeight(Paint paint) {
61+
Paint.FontMetrics metrics = paint.getFontMetrics();
62+
return metrics.descent - metrics.ascent;
63+
}
64+
65+
public static float getLineSpacing(Paint paint) {
66+
Paint.FontMetrics metrics = paint.getFontMetrics();
67+
return metrics.ascent - metrics.top + metrics.bottom;
68+
}
69+
}

build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ ext {
4545

4646
minSdkVersion = 14
4747
targetSdkVersion = 25
48-
48+
versionCode =2
49+
versionName="1.0.5"
4950
supportVersion = "25.2.0"
5051
}

dbutils/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ android {
2323
defaultConfig {
2424
minSdkVersion rootProject.ext.minSdkVersion
2525
targetSdkVersion rootProject.ext.targetSdkVersion
26-
versionCode 1
27-
versionName "1.0"
26+
versionCode rootProject.ext.versionCode
27+
versionName rootProject.ext.versionName
2828

2929
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
3030

mediautil/build.gradle

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ android {
2121
buildToolsVersion rootProject.ext.buildToolsVersion
2222

2323
defaultConfig {
24-
minSdkVersion 15
25-
targetSdkVersion 25
26-
versionCode 1
27-
versionName "1.0"
24+
minSdkVersion rootProject.ext.minSdkVersion
25+
targetSdkVersion rootProject.ext.targetSdkVersion
26+
versionCode rootProject.ext.versionCode
27+
versionName rootProject.ext.versionName
2828

2929
}
3030
buildTypes {

0 commit comments

Comments
 (0)