Skip to content

Commit b0cfd70

Browse files
committed
see 09/09 log
1 parent e246b62 commit b0cfd70

File tree

4 files changed

+84
-47
lines changed

4 files changed

+84
-47
lines changed

utilcode/src/main/java/com/blankj/utilcode/utils/ImageUtils.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -140,31 +140,38 @@ public static Bitmap getBitmapByFile(String filePath) {
140140
return BitmapFactory.decodeFile(filePath);
141141
}
142142

143+
/**
144+
* 根据文件路径获取bitmap
145+
*
146+
* @param res 资源对象
147+
* @param id 资源id
148+
* @return bitmap
149+
*/
150+
public static Bitmap getBitmapByResource(Resources res, int id) {
151+
return BitmapFactory.decodeResource(res, id);
152+
}
153+
143154
/**
144155
* @param filePath 文件路径
145156
* @return bitmap
146157
*/
147158
public static Bitmap getBitmapByFile(String filePath, int reqWidth, int reqHeight) {
148159
if (StringUtils.isSpace(filePath)) return null;
149-
final BitmapFactory.Options options = new BitmapFactory.Options();
160+
BitmapFactory.Options options = new BitmapFactory.Options();
150161
options.inJustDecodeBounds = true;
151162
BitmapFactory.decodeFile(filePath, options);
152163
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
153164
options.inJustDecodeBounds = false;
154165
return BitmapFactory.decodeFile(filePath, options);
155166
}
156167

157-
public static int calculateInSampleSize(
158-
BitmapFactory.Options options, int reqWidth, int reqHeight) {
159-
// Raw height and width of image
168+
public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
160169
final int height = options.outHeight;
161170
final int width = options.outWidth;
162171
int inSampleSize = 1;
163172
if (height > reqHeight || width > reqWidth) {
164-
final int halfHeight = height / 2;
165-
final int halfWidth = width / 2;
166-
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
167-
// height and width larger than the requested height and width.
173+
int halfHeight = height >> 1;
174+
int halfWidth = width >> 1;
168175
while ((halfHeight / inSampleSize) > reqHeight
169176
&& (halfWidth / inSampleSize) > reqWidth) {
170177
inSampleSize <<= 1;

utilcode/src/main/java/com/blankj/utilcode/utils/StringUtils.java

Lines changed: 56 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.blankj.utilcode.utils;
22

3+
import java.util.Arrays;
4+
import java.util.Collections;
5+
36
/**
47
* <pre>
58
* author: Blankj
@@ -17,80 +20,100 @@ private StringUtils() {
1720
/**
1821
* 判断字符串是否为null或长度为0
1922
*
20-
* @param string 待校验字符串
23+
* @param s 待校验字符串
2124
* @return {@code true}: 空<br> {@code false}: 不为空
2225
*/
23-
public static boolean isEmpty(CharSequence string) {
24-
return string == null || string.length() == 0;
26+
public static boolean isEmpty(CharSequence s) {
27+
return s == null || s.length() == 0;
2528
}
2629

2730
/**
2831
* 判断字符串是否为null或全为空格
2932
*
30-
* @param string 待校验字符串
33+
* @param s 待校验字符串
3134
* @return {@code true}: null或全空格<br> {@code false}: 不为null且不全空格
3235
*/
33-
public static boolean isSpace(String string) {
34-
return (string == null || string.trim().length() == 0);
36+
public static boolean isSpace(String s) {
37+
return (s == null || s.trim().length() == 0);
3538
}
3639

3740
/**
3841
* null转为长度为0的字符串
3942
*
40-
* @param string 待转字符串
41-
* @return string为null转为长度为0字符串,否则不改变
43+
* @param s 待转字符串
44+
* @return s为null转为长度为0字符串,否则不改变
4245
*/
43-
public static String null2Length0(String string) {
44-
return string == null ? "" : string;
46+
public static String null2Length0(String s) {
47+
return s == null ? "" : s;
4548
}
4649

4750
/**
4851
* 返回字符串长度
4952
*
50-
* @param string 字符串
53+
* @param s 字符串
5154
* @return null返回0,其他返回自身长度
5255
*/
53-
public static int length(CharSequence string) {
54-
return string == null ? 0 : string.length();
56+
public static int length(CharSequence s) {
57+
return s == null ? 0 : s.length();
5558
}
5659

5760
/**
5861
* 首字母大写
5962
*
60-
* @param string 待转字符串
63+
* @param s 待转字符串
6164
* @return 首字母大写字符串
6265
*/
63-
public static String upperFirstLetter(String string) {
64-
if (isEmpty(string) || !Character.isLowerCase(string.charAt(0))) {
65-
return string;
66+
public static String upperFirstLetter(String s) {
67+
if (isEmpty(s) || !Character.isLowerCase(s.charAt(0))) {
68+
return s;
6669
}
67-
return String.valueOf((char) (string.charAt(0) - 32)) + string.substring(1);
70+
return String.valueOf((char) (s.charAt(0) - 32)) + s.substring(1);
6871
}
6972

7073
/**
7174
* 首字母小写
7275
*
73-
* @param string 待转字符串
76+
* @param s 待转字符串
7477
* @return 首字母小写字符串
7578
*/
76-
public static String lowerFirstLetter(String string) {
77-
if (isEmpty(string) || !Character.isUpperCase(string.charAt(0))) {
78-
return string;
79+
public static String lowerFirstLetter(String s) {
80+
if (isEmpty(s) || !Character.isUpperCase(s.charAt(0))) {
81+
return s;
82+
}
83+
return String.valueOf((char) (s.charAt(0) + 32)) + s.substring(1);
84+
}
85+
86+
/**
87+
* 反转字符串
88+
*
89+
* @param s 待反转字符串
90+
* @return 反转字符串
91+
*/
92+
public static String reverse(String s) {
93+
int len = length(s);
94+
if (len <= 1) return s;
95+
int mid = len >> 1;
96+
char[] chars = s.toCharArray();
97+
char c;
98+
for (int i = 0; i < mid; ++i) {
99+
c = chars[i];
100+
chars[i] = chars[len - i - 1];
101+
chars[len - i - 1] = c;
79102
}
80-
return String.valueOf((char) (string.charAt(0) + 32)) + string.substring(1);
103+
return new String(chars);
81104
}
82105

83106
/**
84107
* 转化为半角字符
85108
*
86-
* @param string 待转字符串
109+
* @param s 待转字符串
87110
* @return 半角字符串
88111
*/
89-
public static String toDBC(String string) {
90-
if (isEmpty(string)) {
91-
return string;
112+
public static String toDBC(String s) {
113+
if (isEmpty(s)) {
114+
return s;
92115
}
93-
char[] chars = string.toCharArray();
116+
char[] chars = s.toCharArray();
94117
for (int i = 0, len = chars.length; i < len; i++) {
95118
if (chars[i] == 12288) {
96119
chars[i] = ' ';
@@ -106,14 +129,14 @@ public static String toDBC(String string) {
106129
/**
107130
* 转化为全角字符
108131
*
109-
* @param string 待转字符串
132+
* @param s 待转字符串
110133
* @return 全角字符串
111134
*/
112-
public static String toSBC(String string) {
113-
if (isEmpty(string)) {
114-
return string;
135+
public static String toSBC(String s) {
136+
if (isEmpty(s)) {
137+
return s;
115138
}
116-
char[] chars = string.toCharArray();
139+
char[] chars = s.toCharArray();
117140
for (int i = 0, len = chars.length; i < len; i++) {
118141
if (chars[i] == ' ') {
119142
chars[i] = (char) 12288;

utilcode/src/test/java/com/blankj/utilcode/utils/ImageUtilsTest.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
package com.blankj.utilcode.utils;
22

33
import android.graphics.Bitmap;
4+
import android.graphics.Canvas;
5+
import android.graphics.Color;
6+
import android.graphics.Paint;
47

58
import org.junit.Test;
69
import org.junit.runner.RunWith;
710
import org.robolectric.RobolectricTestRunner;
811
import org.robolectric.annotation.Config;
12+
import org.robolectric.shadows.ShadowBitmap;
913

1014
import static com.blankj.utilcode.utils.TestUtils.BASEPATH;
1115
import static com.blankj.utilcode.utils.TestUtils.SEP;
@@ -25,7 +29,7 @@ public class ImageUtilsTest {
2529
String path = BASEPATH + "image" + SEP;
2630

2731
@Test
28-
public void testBitmap2Bytes() throws Exception {
32+
public void testSaveBitmap() throws Exception {
2933
Bitmap bitmap = ImageUtils.getBitmapByFile(path + "lena.png");
3034
System.out.println(ImageUtils.save(bitmap, path + "new.png", Bitmap.CompressFormat.PNG));
3135
}
@@ -99,9 +103,4 @@ public void testAddFrame() throws Exception {
99103
public void testAddReflection() throws Exception {
100104

101105
}
102-
103-
@Test
104-
public void testSaveBitmap() throws Exception {
105-
106-
}
107106
}

utilcode/src/test/java/com/blankj/utilcode/utils/StringUtilsTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ public void testLowerFirstLetter() throws Exception {
5656
assertThat(lowerFirstLetter("1blankj")).isEqualTo("1blankj");
5757
}
5858

59+
@Test
60+
public void testReverse() throws Exception {
61+
assertThat(reverse("blankj")).isEqualTo("jknalb");
62+
assertThat(reverse("blank")).isEqualTo("knalb");
63+
assertThat(reverse("测试中文")).isEqualTo("文中试测");
64+
assertThat(reverse(null)).isNull();
65+
}
66+
5967
@Test
6068
public void testToDBC() throws Exception {
6169
assertThat(toDBC(" ,.&")).isEqualTo(" ,.&");

0 commit comments

Comments
 (0)