ResUtil.java
2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package com.xdy.util;
import android.content.Context;
import android.content.res.Resources;
import android.support.annotation.StringRes;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
/**
* @author jianghongbo
* @version 1.0
* @file ResUtil.java
* @brief 封装了获取资源的一些方法,方便的获得字符串,颜色等资源
* @date 2017/6/4
* Copyright (c) 2017
* All rights reserved.
*/
public class ResUtil {
private static Context ctx;
private ResUtil() {
}
public static void init(Context cc) {
ctx = cc;
}
public static ResUtil get() {
return SingletonHolder.INSTANCE;
}
private static class SingletonHolder {
private static final ResUtil INSTANCE = new ResUtil();
}
/**
* @return Resources
* @brief 获得当前APP的Resource的方法
*/
public static Resources getResource() {
return ctx.getResources();
}
/**
* 获取字符串
* @param stringId
* @return
*/
public String getString(@StringRes int stringId) {
return getResource().getString(stringId);
}
/**
* 获取字符串,带格式化方法
* @param stringId
* @param formatArgs
* @return
*/
public String getString(int stringId, Object... formatArgs) {
return getResource().getString(stringId, formatArgs);
}
/**
* 获取Dimen值对应的像素值
* @param dimenId
* @return
*/
public int getDimenPixel(int dimenId) {
return getResource().getDimensionPixelOffset(dimenId);
}
/**
* 获取颜色
* @param colorId
* @return
*/
public int getColor(int colorId) {
return getResource().getColor(colorId);
}
/**
* 获取资产中的字符串
* @param fileName
* @return
*/
public String getStringFromAssert(String fileName) {
InputStream in = null;
try {
in = getResource().getAssets().open(fileName);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in));
String line = null;
StringBuilder sb = new StringBuilder();
do {
line = bufferedReader.readLine();
if (line != null) {
sb.append(line);
}
} while (line != null);
bufferedReader.close();
in.close();
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
}
}
}
return null;
}
}