├── README.md
├── Screenshot_2016-06-20.png
├── app
├── build.gradle
├── proguard-rules.pro
└── src
│ ├── androidTest
│ └── java
│ │ └── cn
│ │ └── rosen
│ │ └── sticktextview
│ │ └── ApplicationTest.java
│ ├── main
│ ├── AndroidManifest.xml
│ ├── java
│ │ └── cn
│ │ │ └── rosen
│ │ │ └── sticktextview
│ │ │ ├── MainActivity.java
│ │ │ ├── utils
│ │ │ ├── CommonUtils.java
│ │ │ └── PrintUtils.java
│ │ │ └── view
│ │ │ ├── StickerTextView.java
│ │ │ └── TextViewItem.java
│ └── res
│ │ ├── drawable
│ │ ├── bg.png
│ │ └── bg_test.png
│ │ ├── layout
│ │ ├── activity_main.xml
│ │ ├── content_main.xml
│ │ └── text_layout.xml
│ │ ├── mipmap-hdpi
│ │ └── ic_launcher.png
│ │ ├── mipmap-mdpi
│ │ └── ic_launcher.png
│ │ ├── mipmap-xhdpi
│ │ ├── edit_wz_bg_btn1.png
│ │ ├── edit_wz_bg_btn2.png
│ │ ├── edit_wz_bg_btn3.png
│ │ ├── ic_launcher.png
│ │ ├── text_button_02.png
│ │ └── txt_button_0.png
│ │ ├── mipmap-xxhdpi
│ │ └── ic_launcher.png
│ │ ├── mipmap-xxxhdpi
│ │ └── ic_launcher.png
│ │ ├── values-v21
│ │ └── styles.xml
│ │ ├── values-w820dp
│ │ └── dimens.xml
│ │ └── values
│ │ ├── colors.xml
│ │ ├── dimens.xml
│ │ ├── strings.xml
│ │ └── styles.xml
│ └── test
│ └── java
│ └── cn
│ └── rosen
│ └── sticktextview
│ └── ExampleUnitTest.java
├── build.gradle
├── gradle.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── local.properties
└── settings.gradle
/README.md:
--------------------------------------------------------------------------------
1 | ### StickTextView
2 | #### Android图片文字贴纸功能
3 | #### 感谢http://blog.csdn.net/rosenluo/article/details/51099138
4 | > 可以设置自定义背景,自定义设置文字的位置,可以限制文字字数,可以设置单行多行模式
5 |
6 | > 
7 |
8 |
--------------------------------------------------------------------------------
/Screenshot_2016-06-20.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hanbaokun/StickTextView/e0dab12f8806208d5c016f24cafa8a0d679db035/Screenshot_2016-06-20.png
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 23
5 | buildToolsVersion '23.0.1'
6 |
7 | defaultConfig {
8 | applicationId "cn.rosen.sizeadjusttextstickview"
9 | minSdkVersion 15
10 | targetSdkVersion 18
11 | versionCode 1
12 | versionName "1.0"
13 | }
14 | buildTypes {
15 | release {
16 | minifyEnabled false
17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18 | }
19 | }
20 | }
21 |
22 | dependencies {
23 | compile fileTree(include: ['*.jar'], dir: 'libs')
24 | testCompile 'junit:junit:4.12'
25 | compile 'com.android.support:appcompat-v7:23.0.1'
26 | compile 'com.android.support:design:23.0.1'
27 | }
28 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in D:\D\android-sdks-64/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/cn/rosen/sticktextview/ApplicationTest.java:
--------------------------------------------------------------------------------
1 | package cn.rosen.sticktextview;
2 |
3 | import android.app.Application;
4 | import android.test.ApplicationTestCase;
5 |
6 | /**
7 | * Testing Fundamentals
8 | */
9 | public class ApplicationTest extends ApplicationTestCase {
10 | public ApplicationTest() {
11 | super(Application.class);
12 | }
13 | }
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
11 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/app/src/main/java/cn/rosen/sticktextview/MainActivity.java:
--------------------------------------------------------------------------------
1 | package cn.rosen.sticktextview;
2 |
3 | import android.graphics.Bitmap;
4 | import android.graphics.BitmapFactory;
5 | import android.graphics.Color;
6 | import android.os.Bundle;
7 | import android.support.design.widget.FloatingActionButton;
8 | import android.support.design.widget.Snackbar;
9 | import android.support.v7.app.AppCompatActivity;
10 | import android.support.v7.widget.Toolbar;
11 | import android.text.Editable;
12 | import android.text.TextWatcher;
13 | import android.util.TypedValue;
14 | import android.view.View;
15 | import android.view.ViewGroup;
16 | import android.widget.EditText;
17 | import android.widget.LinearLayout;
18 | import android.widget.RelativeLayout;
19 | import android.widget.TextView;
20 | import android.widget.Toast;
21 |
22 | import cn.rosen.sticktextview.view.StickerTextView;
23 | import cn.rosen.sticktextview.view.TextViewItem;
24 | import cn.rosen.sticktextview.utils.CommonUtils;
25 | import cn.rosen.sticktextview.utils.PrintUtils;
26 |
27 | public class MainActivity extends AppCompatActivity implements StickerTextView.OnStickerTextTouchListener {
28 |
29 | RelativeLayout contentLayout;
30 | View editLayout;
31 | View doButton;
32 | private EditText editText;
33 | private StickerTextView stickerTextView;
34 | private TextViewItem textViewItem;
35 |
36 | @Override
37 | protected void onCreate(Bundle savedInstanceState) {
38 | super.onCreate(savedInstanceState);
39 | setContentView(R.layout.activity_main);
40 | Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
41 | setSupportActionBar(toolbar);
42 |
43 | FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
44 | fab.setOnClickListener(new View.OnClickListener() {
45 | @Override
46 | public void onClick(View view) {
47 | Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
48 | .setAction("Action", null).show();
49 | }
50 | });
51 | contentLayout = (RelativeLayout) findViewById(R.id.content_layout);
52 | editLayout = findViewById(R.id.do_reset_text_layout);
53 | doButton = findViewById(R.id.do_reset_finish);
54 | editText = (EditText) findViewById(R.id.edit_text);
55 | addStikerTextView();
56 | doButton.setOnClickListener(new View.OnClickListener() {
57 | @Override
58 | public void onClick(View v) {
59 | editLayout.setVisibility(View.GONE);
60 | CommonUtils.hideInputMethod(MainActivity.this, editText);
61 | }
62 | });
63 | editText.addTextChangedListener(new TextWatcher() {
64 | @Override
65 | public void beforeTextChanged(CharSequence s, int start, int count, int after) {
66 |
67 | }
68 |
69 | @Override
70 | public void onTextChanged(CharSequence s, int start, int before, int count) {
71 |
72 | }
73 |
74 | @Override
75 | public void afterTextChanged(Editable s) {
76 | int nums = textViewItem.getNums();
77 | if(s.length()<=nums){
78 | textViewItem.getTextView().setText(s.toString(),TextView.BufferType.NORMAL);
79 | stickerTextView.updateTextDraw(textViewItem.getTextView(), textViewItem.isSingleLine());
80 | }else {
81 | Toast.makeText(MainActivity.this, "字数限制"+nums, Toast.LENGTH_SHORT).show();
82 | }
83 | }
84 | });
85 | }
86 |
87 | private void addStikerTextView() {
88 | stickerTextView = new StickerTextView(getBaseContext());
89 | stickerTextView.setOnStickerTextTouchListener(this);
90 | //设置显示位置
91 | RelativeLayout.LayoutParams rl = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
92 | rl.addRule(RelativeLayout.CENTER_IN_PARENT);
93 | //设置背景
94 | Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bg_test);
95 | stickerTextView.setBackgroundBitmap(bitmap);
96 | //添加文字元素
97 | TextView s1 = new TextView(getBaseContext());
98 | s1.setText("静", TextView.BufferType.NORMAL);
99 | s1.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
100 | s1.setBackgroundColor(Color.parseColor("#00000000"));
101 | s1.setTextColor(Color.BLACK);
102 | s1.setTextSize(getTextSize(10));
103 |
104 | TextView s2 = new TextView(getBaseContext());
105 | s2.setText("默", TextView.BufferType.NORMAL);
106 | s2.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
107 | s2.setBackgroundColor(Color.parseColor("#00000000"));
108 | s2.setTextColor(Color.BLACK);
109 | s2.setTextSize(getTextSize(10));
110 |
111 | TextView s3 = new TextView(getBaseContext());
112 | s3.setText("时", TextView.BufferType.NORMAL);
113 | s3.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
114 | s3.setBackgroundColor(Color.parseColor("#00000000"));
115 | s3.setTextColor(Color.BLACK);
116 | s3.setTextSize(getTextSize(10));
117 |
118 | TextView s4 = new TextView(getBaseContext());
119 | s4.setText("光", TextView.BufferType.NORMAL);
120 | s4.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
121 | s4.setBackgroundColor(Color.parseColor("#00000000"));
122 | s4.setTextColor(Color.BLACK);
123 | s4.setTextSize(getTextSize(10));
124 |
125 | TextView s5 = new TextView(getBaseContext());
126 | s5.setText("我们在一起/\n不需要孤独/\n独处时的静默时光/", TextView.BufferType.NORMAL);
127 | s5.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
128 | s5.setBackgroundColor(Color.parseColor("#00000000"));
129 | s5.setTextColor(Color.WHITE);
130 | s5.setLines(5);
131 | s5.setLineSpacing(1.0f,2.0f);
132 | s5.setTextSize(getTextSize(5));
133 |
134 |
135 | stickerTextView.addTextDraw(s1, 10, 10, 50, 50,true,1);
136 | stickerTextView.addTextDraw(s2, 60, 10, 100, 50,true,1);
137 | stickerTextView.addTextDraw(s3, 110, 10, 150, 50,true,1);
138 | stickerTextView.addTextDraw(s4, 160, 10, 200, 50,true,1);
139 | stickerTextView.addTextDraw(s5, 15, 60, 180, 200,false,30);
140 |
141 | contentLayout.addView(stickerTextView, rl);
142 | }
143 |
144 | private float getTextSize(float size) {
145 | return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, size, getResources().getDisplayMetrics());
146 | }
147 |
148 | @Override
149 | public void onTextCopy(StickerTextView stickerView) {
150 | PrintUtils.println("点击了复制");
151 | }
152 |
153 | @Override
154 | public void onTextDelete(StickerTextView stickerView) {
155 | PrintUtils.println("点击了删除");
156 | contentLayout.removeView(stickerView);
157 | }
158 |
159 | @Override
160 | public void onTextMoveToHead(StickerTextView stickerView) {
161 | PrintUtils.println("触摸到控件时会调用该方法");
162 | }
163 |
164 | @Override
165 | public void onTextClickCurrentText(TextViewItem textViewItem) {
166 | this.textViewItem = textViewItem;
167 | PrintUtils.println("单击调用");
168 | editLayout.setVisibility(View.VISIBLE);
169 | editText.setText(textViewItem.getTextView().getText());
170 | CommonUtils.showInputMethod(MainActivity.this, editText);
171 | }
172 | }
173 |
--------------------------------------------------------------------------------
/app/src/main/java/cn/rosen/sticktextview/utils/CommonUtils.java:
--------------------------------------------------------------------------------
1 | package cn.rosen.sticktextview.utils;
2 |
3 | import android.app.Activity;
4 | import android.app.ActivityManager;
5 | import android.app.PendingIntent;
6 | import android.app.PendingIntent.CanceledException;
7 | import android.content.ComponentName;
8 | import android.content.Context;
9 | import android.content.Intent;
10 | import android.content.pm.ApplicationInfo;
11 | import android.content.pm.PackageInfo;
12 | import android.content.pm.PackageManager;
13 | import android.content.pm.PackageManager.NameNotFoundException;
14 | import android.content.pm.ResolveInfo;
15 | import android.database.Cursor;
16 | import android.graphics.Bitmap;
17 | import android.graphics.BitmapFactory;
18 | import android.graphics.Paint;
19 | import android.graphics.drawable.Drawable;
20 | import android.location.LocationManager;
21 | import android.media.MediaScannerConnection;
22 | import android.net.ConnectivityManager;
23 | import android.net.NetworkInfo;
24 | import android.net.Uri;
25 | import android.os.Bundle;
26 | import android.os.Environment;
27 | import android.os.StatFs;
28 | import android.telephony.TelephonyManager;
29 | import android.text.ClipboardManager;
30 | import android.text.TextUtils;
31 | import android.util.DisplayMetrics;
32 | import android.util.Log;
33 | import android.view.Display;
34 | import android.view.View;
35 | import android.view.Window;
36 | import android.view.WindowManager;
37 | import android.view.animation.TranslateAnimation;
38 | import android.view.inputmethod.InputMethodManager;
39 | import android.webkit.WebSettings;
40 | import android.webkit.WebView;
41 | import android.widget.EditText;
42 |
43 | import java.io.InputStream;
44 | import java.io.UnsupportedEncodingException;
45 | import java.net.URLDecoder;
46 | import java.net.URLEncoder;
47 | import java.text.Collator;
48 | import java.util.Collections;
49 | import java.util.Comparator;
50 | import java.util.List;
51 | import java.util.Locale;
52 | import java.util.Map;
53 | import java.util.UUID;
54 |
55 | public class CommonUtils{
56 |
57 | /**
58 | * 获取系统版本
59 | */
60 | public static int version = android.os.Build.VERSION.SDK_INT;
61 |
62 | /**
63 | * 获取屏幕宽度
64 | * @param act
65 | * @return
66 | */
67 | public static int getDisplayWidth(Activity act)
68 | {
69 | return act.getWindowManager().getDefaultDisplay().getWidth();
70 | }
71 | /**
72 | * 获取屏幕高度
73 | * @param act
74 | * @return
75 | */
76 | public static int getDisplayHeight(Activity act)
77 | {
78 | return act.getWindowManager().getDefaultDisplay().getHeight();
79 | }
80 | /**
81 | * DisplayMetrics dm = new DisplayMetrics();
82 | * getWindowManager().getDefaultDisplay().getMetrics(dm);
83 | * int screenWidth = dm.widthPixels;
84 | * int screenHeight = dm.heightPixels;
85 | */
86 | /**
87 | * 获取屏幕宽度
88 | * @param context
89 | * @return
90 | */
91 | public static int getScreenWidth(Context context) {
92 | WindowManager manager = (WindowManager) context
93 | .getSystemService(Context.WINDOW_SERVICE);
94 | Display display = manager.getDefaultDisplay();
95 | return display.getWidth();
96 | }
97 | /**
98 | * 获取屏幕高度
99 | * @param context
100 | * @return
101 | */
102 | public static int getScreenHeight(Context context) {
103 | WindowManager manager = (WindowManager) context
104 | .getSystemService(Context.WINDOW_SERVICE);
105 | Display display = manager.getDefaultDisplay();
106 | return display.getHeight();
107 | }
108 | /**
109 | * 获取屏幕最小宽度(相对高度来说)
110 | * @param act
111 | * @return
112 | */
113 | public static int getMinWidth(Activity act)
114 | {
115 | return Math.min(getDisplayWidth(act), getDisplayHeight(act));
116 | }
117 |
118 | /**
119 | * 获取屏幕像素宽度
120 | * @param context
121 | * @return
122 | */
123 | public static int getWidthPixels(Context context)
124 | {
125 | DisplayMetrics dm = new DisplayMetrics();
126 | dm = context.getResources().getDisplayMetrics();
127 |
128 | return dm.widthPixels;
129 | }
130 | /**
131 | * 获取屏幕像素高度
132 | * @param context
133 | * @return
134 | */
135 | public static int getHeightPixels(Context context)
136 | {
137 | DisplayMetrics dm = new DisplayMetrics();
138 | dm = context.getResources().getDisplayMetrics();
139 | return dm.heightPixels;
140 | }
141 | /**
142 | * 以dp形式获取屏幕像素高度
143 | * @param context
144 | * @return
145 | */
146 | public static final int getHeightInDp(Context context) {
147 | final float height = getHeightPixels(context);
148 | int heightInDp = pxToDip(context, height);
149 | return heightInDp;
150 | }
151 | /**
152 | * 以dp形式获取屏幕像素宽度
153 | * @param context
154 | * @return
155 | */
156 | public static final int getWidthInDp(Context context) {
157 | final float width = getWidthPixels(context);
158 | int widthInDp = pxToDip(context, width);
159 | return widthInDp;
160 | }
161 | /**
162 | * 获取屏幕密度
163 | * @param activity
164 | * @return
165 | */
166 | public static float getScreenDensity(Activity activity){
167 | DisplayMetrics metric = new DisplayMetrics();
168 | activity.getWindow().getWindowManager().getDefaultDisplay().getMetrics(metric);
169 |
170 | return metric.density;
171 | }
172 | /**
173 | * 获取根Activity
174 | * @param activity
175 | * @return
176 | */
177 | public static Activity getRootContext(Activity activity) {
178 | while(activity.getParent() != null){
179 | activity = activity.getParent();
180 | }
181 | return activity;
182 | }
183 | /**
184 | * 判断横竖屏
185 | * @param context
186 | * @return true为横屏,false为竖屏
187 | */
188 | public static boolean isOrientationLandscape(Context context)
189 | {
190 | return getWidthPixels(context) > getHeightPixels(context);
191 | }
192 | /**
193 | * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
194 | * @param context
195 | * @param dpValue
196 | * @return
197 | */
198 | public static int dipToPx(Context context, float dpValue) {
199 | final float density = context.getResources().getDisplayMetrics().density;
200 | return (int) (dpValue * density + 0.5f);
201 | }
202 | /**
203 | * 根据手机的分辨率从 px(像素)的单位 转成为 dp
204 | * @param context
205 | * @param pxValue
206 | * @return
207 | */
208 | public static int pxToDip(Context context,float pxValue){
209 | final float density = context.getResources().getDisplayMetrics().density;
210 | return (int)(pxValue/density +0.5f);
211 |
212 | }
213 | /**
214 | * px转sp
215 | * @param context
216 | * @param pxValue
217 | * @return
218 | */
219 | public static int px2sp(Context context, float pxValue) {
220 | final float scale = context.getResources().getDisplayMetrics().density;
221 | return (int) (pxValue / scale + 0.5f);
222 | }
223 | /**
224 | * sp转px
225 | * @param context
226 | * @param spValue
227 | * @return
228 | */
229 | public static int sp2px(Context context, float spValue) {
230 | final float scale = context.getResources().getDisplayMetrics().density;
231 | return (int) (spValue * scale + 0.5f);
232 | }
233 |
234 | /**
235 | * 隐藏输入法
236 | * @param act
237 | */
238 | public static void hideInputMethod(Activity act) {
239 | if(act == null){
240 | return;
241 | }
242 | try {
243 | InputMethodManager inputMethodManager = (InputMethodManager) act.getSystemService(Context.INPUT_METHOD_SERVICE);
244 | if (act.getWindow() != null) {
245 | if (act.getWindow().getCurrentFocus() == null) {
246 | inputMethodManager.hideSoftInputFromWindow(null, 0);
247 | } else {
248 | inputMethodManager.hideSoftInputFromWindow(act.getWindow().getCurrentFocus().getWindowToken(), 0);
249 | }
250 | }
251 | } catch (Exception e) {
252 | PrintUtils.println(e);
253 | }
254 | }
255 |
256 | public static void hideInputMethod(Activity act, EditText etext) {
257 | try {
258 | InputMethodManager inputMethodManager = (InputMethodManager) act.getSystemService(Context.INPUT_METHOD_SERVICE);
259 | inputMethodManager.hideSoftInputFromWindow(etext.getWindowToken(),0);
260 | } catch (Exception e) {
261 | PrintUtils.println(e);
262 | }
263 | }
264 |
265 | /**
266 | * 显示输入法
267 | * @param act
268 | */
269 | public static void showInputMethod(Activity act){
270 | if(act == null){
271 | return;
272 | }
273 | try {
274 | InputMethodManager inputMethodManager = (InputMethodManager) act.getSystemService(Context.INPUT_METHOD_SERVICE);
275 | if (act.getWindow() != null && act.getWindow().getCurrentFocus() != null) {
276 | inputMethodManager.showSoftInputFromInputMethod(act.getWindow().getCurrentFocus().getWindowToken(), 0);
277 | }
278 | } catch (Exception e) {
279 | PrintUtils.println(e);
280 | }
281 | }
282 |
283 | /**
284 | * 显示输入法(建议使用这个,这个更能确保弹出)
285 | * @param act
286 | * @param mEditView
287 | */
288 | public static void showInputMethod(Activity act, View mEditView) {
289 | try {
290 | mEditView.requestFocus();
291 | InputMethodManager imm = (InputMethodManager) act.getSystemService(Context.INPUT_METHOD_SERVICE);
292 | imm.showSoftInput(mEditView, 0);
293 | } catch (Exception e) {
294 | PrintUtils.println(e);
295 | }
296 | }
297 |
298 | /**
299 | * 隐藏标题栏(用于没有在配置里设置主题为Theme.Black.NoTitleBar)
300 | * @param act
301 | */
302 | public static void hideFeatureTitle(Activity act){
303 | act.requestWindowFeature(Window.FEATURE_NO_TITLE);
304 | }
305 | /**
306 | * 打电话,调用前先验证phone,同时检查是否有添加权限
307 | * @param context
308 | * @param phone
309 | */
310 | public static void callNumber(Context context, String phone) {
311 | try {
312 | // 传入服务, parse()解析号码
313 | Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phone));
314 | // 通知activtity处理传入的call服务
315 | context.startActivity(intent);
316 | } catch (Exception ex) {
317 | PrintUtils.println(ex);
318 | }
319 | }
320 | /**
321 | * 判断GPS是否开启,GPS或者AGPS开启一个就认为是开启的
322 | *
323 | * @param context
324 | * @return true 表示开启
325 | */
326 | public static final boolean isGPSOpen(final Context context) {
327 | LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
328 | // 通过GPS卫星定位,定位级别可以精确到街(通过24颗卫星定位,在室外和空旷的地方定位准确、速度快)
329 | boolean gps = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
330 | // 通过WLAN或移动网络(3G/2G)确定的位置(也称作AGPS,辅助GPS定位。主要用于在室内或遮盖物(建筑群或茂密的深林等)密集的地方定位)
331 | boolean network = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
332 | if (gps || network) {
333 | return true;
334 | }
335 |
336 | return false;
337 | }
338 | /**
339 | * 强制帮用户打开GPS
340 | * @param context
341 | */
342 | public static final void openGPS(Context context) {
343 | Intent GPSIntent = new Intent();
344 | GPSIntent.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
345 | GPSIntent.addCategory("android.intent.category.ALTERNATIVE");
346 | GPSIntent.setData(Uri.parse("custom:3"));
347 | try {
348 | PendingIntent.getBroadcast(context, 0, GPSIntent, 0).send();
349 | } catch (CanceledException e) {
350 | PrintUtils.println(e);
351 | }
352 | }
353 | /**
354 | * 判断wifi是否可用
355 | *
356 | * @param context
357 | * @return
358 | */
359 | public static boolean isWifiAvilable(Context context) {
360 | ConnectivityManager manager = (ConnectivityManager) context
361 | .getSystemService(Context.CONNECTIVITY_SERVICE);
362 | if (manager == null) {
363 | return false;
364 | }
365 | NetworkInfo wifiInfo = manager
366 | .getNetworkInfo(ConnectivityManager.TYPE_WIFI);
367 | if (wifiInfo != null) {
368 | return wifiInfo.isAvailable();
369 | }
370 | return false;
371 | }
372 |
373 | /**
374 | * 判断wifi是否处于连接
375 | * @param context
376 | * @return
377 | */
378 | public static boolean isWiftConnected(Context context){
379 | ConnectivityManager manager = (ConnectivityManager) context
380 | .getSystemService(Context.CONNECTIVITY_SERVICE);
381 | if (manager == null) {
382 | return false;
383 | }
384 | NetworkInfo wifiInfo = manager
385 | .getNetworkInfo(ConnectivityManager.TYPE_WIFI);
386 | if(wifiInfo != null){
387 | return wifiInfo.isConnected();
388 | }
389 | return false;
390 | }
391 | /**
392 | * 判断网络是否可用
393 | *
394 | * @param context
395 | * @return
396 | */
397 | public static boolean isNetworkAvilable(Context context) {
398 | if (context == null) {
399 | return false;
400 | }
401 | try {
402 | ConnectivityManager con = (ConnectivityManager) context
403 | .getSystemService(Context.CONNECTIVITY_SERVICE);
404 | if(con == null){
405 | return false;
406 | }
407 | boolean wifi=con.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnectedOrConnecting();
408 | boolean internet=con.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnectedOrConnecting();
409 | boolean isAvilable = con.getActiveNetworkInfo().isAvailable();
410 | if(wifi || internet || isAvilable){
411 | return true;
412 | }
413 | // if (connectivity != null) {
414 | // // 获取网络连接管理的对象
415 | // NetworkInfo info = connectivity.getActiveNetworkInfo();
416 | // if (info != null) {
417 | // // 判断当前网络是否已经连接
418 | // return info.isConnected()
419 | // }
420 | // }
421 | } catch (Exception e) {
422 | PrintUtils.println(e);
423 | return false;
424 | }
425 | return false;
426 | }
427 | /**
428 | * 判断网络是否链接成功
429 | * @param context
430 | * @return
431 | */
432 | public static boolean hasInternet(Context context){
433 | ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
434 | NetworkInfo info = manager.getActiveNetworkInfo();
435 | if (info != null && info.isConnected()) {
436 | if (info.getState() == NetworkInfo.State.CONNECTED) {
437 | return true;
438 | }
439 | }
440 | return false;
441 | }
442 | public static boolean exterStorageReady() {
443 | return Environment.MEDIA_MOUNTED.equals(Environment
444 | .getExternalStorageState()) || !Environment.isExternalStorageRemovable();
445 | }
446 | /**
447 | * 获取缓存路径
448 | * @param context
449 | * @return
450 | */
451 | public static String getDiskCacheDir(Context context){
452 | String cachePath = null;
453 | try {
454 | if(exterStorageReady()){
455 | cachePath = context.getExternalCacheDir().getPath();
456 | }else{
457 | cachePath = context.getCacheDir().getPath();
458 | }
459 | } catch (Exception e) {
460 | cachePath = getCacheDir(context);
461 | e.printStackTrace();
462 | }
463 | return cachePath;
464 | }
465 |
466 | /**
467 | * 获取文件目录
468 | * @param context
469 | * @return
470 | */
471 | public static String getDiskFileDir(Context context){
472 | String filePath = null;
473 | try {
474 | if(exterStorageReady()){
475 | filePath = context.getExternalFilesDir(null).getPath();
476 | }else {
477 | filePath = context.getFilesDir().getPath();
478 | }
479 | } catch (Exception e) {
480 | filePath = getFileDir(context);
481 | e.printStackTrace();
482 | }
483 | return filePath;
484 | }
485 | /**
486 | * 获取缓存目录
487 | *
488 | * @param context
489 | * @return
490 | */
491 | public static String getCacheDir(Context context) {
492 | return context == null ? "" : context.getCacheDir().getAbsolutePath();
493 | }
494 |
495 | /**
496 | * 获取文件缓存目录
497 | *
498 | * @param context
499 | * @return
500 | */
501 | public static String getFileDir(Context context) {
502 | return context == null ? "" : context.getFilesDir().getAbsolutePath();
503 | }
504 | public static String getAvilablePath(Context context){
505 | if(exterStorageReady()){
506 | return getExCardPath();
507 | }else{
508 | return getFileDir(context);
509 | }
510 | }
511 | /**
512 | * 获取扩展卡路径
513 | */
514 | public static String getExCardPath() {
515 | if (exterStorageReady()){
516 | return Environment.getExternalStorageDirectory().toString();
517 | }
518 | return "";
519 | }
520 |
521 | /**
522 | * 获取手机剩余空间
523 | * @return
524 | */
525 | public static long getRemainSaveSize() {
526 | //有sd卡
527 | if (exterStorageReady()) {
528 | String sdcard = Environment.getExternalStorageDirectory().getPath();
529 | StatFs statFs = new StatFs(sdcard);
530 | // 获取一个文件的存储大小
531 | long blockSize = statFs.getBlockSize();
532 | // 获取剩下可用的文件大小
533 | long blocks = statFs.getFreeBlocks();
534 | return blocks * blockSize;
535 | }
536 | //木有sd卡
537 | else {
538 | String rootPath = Environment.getRootDirectory().getPath();
539 | StatFs statFs = new StatFs(rootPath);
540 | // 获取一个文件的存储大小
541 | long blockSize = statFs.getBlockSize();
542 | // 获取剩下可用的文件大小
543 | long blocks = statFs.getFreeBlocks();
544 | return blocks * blockSize;
545 | }
546 | }
547 | /**
548 | * 判断某应用是否安装
549 | * @param context
550 | * @param pagckageName
551 | * @return
552 | */
553 | public boolean isAppAvilible(Context context,String pagckageName){
554 | if(pagckageName == null || "".equals(pagckageName)){
555 | return false;
556 | }
557 | PackageManager packageManager = context.getPackageManager();
558 | //获取所有已安装包信息
559 | List pagckageInfos = packageManager.getInstalledPackages(0);
560 | for (int i = 0; i < pagckageInfos.size(); i++) {
561 | if(pagckageInfos.get(i).packageName.equalsIgnoreCase(pagckageName)){
562 | return true;
563 | }
564 | }
565 | return false;
566 | }
567 | /**
568 | * 跳转到包名对应应用的启动页。
569 | * @param context
570 | * @param pagckageName
571 | * @param activityName
572 | */
573 | public static void goApplication(Context context,String pagckageName,String activityName){
574 | Intent it = new Intent();
575 | ComponentName cn = new ComponentName(pagckageName,activityName);
576 | it.setComponent(cn);
577 | context.startActivity(it);
578 | }
579 | /**
580 | * 根据包名打开应用
581 | * @param context
582 | * @param pagckageName
583 | */
584 | public static void goApplication(Context context,String pagckageName){
585 | PackageManager packageManager = context.getPackageManager();
586 | Intent it = packageManager.getLaunchIntentForPackage(pagckageName);
587 | context.startActivity(it);
588 | }
589 | /**
590 | * 用来判断服务是否运行.
591 | * @param context
592 | * @param className 判断的服务名字:包名+类名
593 | * @return true 在运行, false 不在运行
594 | */
595 | public static boolean isServiceRunning(Context context,String className) {
596 | if(context == null || TextUtils.isEmpty(className)){
597 | return false;
598 | }
599 |
600 | boolean isRunning = false;
601 | ActivityManager activityManager =
602 | (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
603 | List serviceList
604 | = activityManager.getRunningServices(Integer.MAX_VALUE);
605 | if (!(serviceList.size()>0)) {
606 | return false;
607 | }
608 | for (int i=0; i 0) {
668 | int len = str.length();
669 | float[] widths = new float[len];
670 | paint.getTextWidths(str, widths);
671 | for (int j = 0; j < len; j++) {
672 | iRet += (int) Math.ceil(widths[j]);
673 | }
674 | }
675 | return iRet;
676 | }
677 | /**
678 | * 获取配置文件里的版本名称
679 | * @param context
680 | * @return
681 | */
682 | public static String getVersionName(Context context)
683 | {
684 | PackageManager packageManager = context.getPackageManager();
685 | //GetPackageName () is your current class package name, 0 stands for is to get version information
686 | PackageInfo packInfo;
687 | try {
688 | packInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
689 | return packInfo.versionName;
690 | } catch (NameNotFoundException e) {
691 | PrintUtils.println(e);
692 | return "";
693 | }
694 | }
695 | /**
696 | * 获取配置文件里的版本号
697 | * @param context
698 | * @return
699 | */
700 | public static int getVersionCode(Context context)
701 | {
702 | PackageManager packageManager = context.getPackageManager();
703 | //GetPackageName () is your current class package name, 0 stands for is to get version information
704 | PackageInfo packInfo;
705 | try {
706 | packInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
707 | return packInfo.versionCode;
708 | } catch (NameNotFoundException e) {
709 | PrintUtils.println(e);
710 | return 0;
711 | }
712 | }
713 | /**
714 | * 获取metaData数据集
715 | * @param context
716 | * @return
717 | */
718 | public static Bundle getMetaData(Context context){
719 | ApplicationInfo info;
720 | Bundle bundle;
721 | try {
722 | info = context.getPackageManager().getApplicationInfo(
723 | context.getPackageName(), PackageManager.GET_META_DATA);
724 | bundle = info.metaData;
725 | if(bundle == null){
726 | bundle = new Bundle();
727 | }
728 | } catch (NameNotFoundException e) {
729 | bundle = new Bundle();
730 | e.printStackTrace();
731 | }
732 | return bundle;
733 | }
734 | /**
735 | * 获取设备id(IMEI number)
736 | * @param context
737 | * @return
738 | */
739 | public static String getDeviceID(Context context)
740 | {
741 | String id = ((TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId();
742 | if(TextUtils.isEmpty(id)){
743 | id = "000000000000000";
744 | }
745 | return id;
746 | }
747 | /**
748 | * 获取设备的UA
749 | *
750 | * @param context
751 | * @return
752 | */
753 | public static String getUserAgent(Context context) {
754 | try {
755 | WebView webview = new WebView(context);
756 | webview.layout(0, 0, 0, 0);
757 | WebSettings settings = webview.getSettings();
758 | String ua = settings.getUserAgentString();
759 | return ua;
760 | } catch (Exception e) {
761 | PrintUtils.println(e);
762 | }
763 | return null;
764 | }
765 | /**
766 | * 去设置默认浏览器
767 | * @param context
768 | */
769 | public static void setDefaultBrowser(Context context,String tip){
770 | Intent intent = new Intent();
771 | intent.setAction("android.intent.action.VIEW");
772 | intent.addCategory("android.intent.category.BROWSABLE");
773 | intent.setData(Uri.parse(tip));
774 | intent.setComponent(new ComponentName("android","com.android.internal.app.ResolverActivity"));
775 | context.startActivity(intent);
776 | }
777 | /**
778 | * 检查是否已经有了默认浏览器
779 | * @param context
780 | * @return
781 | */
782 | public static String checkHasDefaultBrowser(Context context) {
783 | Intent intent = new Intent(Intent.ACTION_VIEW);
784 | intent.setData(Uri.parse("http://www.baidu.com"));
785 | PackageManager pm = context.getPackageManager();
786 | ResolveInfo info = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
787 | return info.activityInfo.packageName;
788 | }
789 | /**
790 | * 去清除包名所对应的默认浏览器
791 | * @param context
792 | * @param packgeName
793 | */
794 | public static void clearDefaultBrowser(Context context,String packgeName){
795 | Intent intent = new Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS, Uri.parse("package:"+packgeName));
796 | context.startActivity(intent);
797 | }
798 | /**
799 | * 清除从默认浏览器中自己浏览器
800 | * @param context
801 | */
802 | public static void clearMyDefaultBrowser(Context context){
803 | PackageManager pm = context.getPackageManager();
804 | pm.clearPackagePreferredActivities(context.getPackageName());
805 | }
806 | /**
807 | * 调整显示窗体
808 | * @param act
809 | * @param alpha 0.4f变暗 1f恢复亮度
810 | */
811 | public static void ajustWindow(Activity act,float alpha){
812 | WindowManager.LayoutParams lp= act.getWindow().getAttributes();
813 | lp.alpha = alpha;
814 | act.getWindow().setAttributes(lp);
815 | }
816 | /**
817 | * 复制文本
818 | * @param context
819 | * @param text
820 | */
821 | @SuppressWarnings("deprecation")
822 | public static void copyText(Context context,String text){
823 | ClipboardManager cm =(ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
824 | cm.setText(text);
825 | }
826 |
827 | /**
828 | * 判断当前设置的语言
829 | * @return
830 | */
831 | public static boolean IsLanguageCN() {
832 | String language = Locale.getDefault().getLanguage();
833 | if (language.equalsIgnoreCase("zh") || language.equalsIgnoreCase("cn"))
834 | return true;
835 | return false;
836 | }
837 |
838 | /**
839 | * 随机的数值(这是一串32位的十六进制的数字,它产生重复值的概率是16的32次方之一)
840 | * @return
841 | */
842 | public static String GetGUID() {
843 | return UUID.randomUUID().toString();
844 | }
845 |
846 | /**
847 | * 将url转为文件名
848 | * @param url
849 | * @return
850 | */
851 | // public static String GetUrlName(String url) {
852 | // if(TextUtils.isEmpty(url)){
853 | // return GetGUID();
854 | // }
855 | //
856 | // String name = URegex.Match(url.trim(), URegex.RegUrlName);
857 | // if (TextUtils.isEmpty(name)){
858 | // return FileMD5Utils.stringMD5(url);
859 | // }
860 | // return name;
861 | // }
862 |
863 | @SuppressWarnings("rawtypes")
864 | public static void Sort(List