一:介绍
这篇博客主要内容是我在项目开发过程中常用的简单的工具类,包括检查网络工具类,Bitmap处理工具类,Toast工具类等,在这里分享给大家,多谢支持!
二:工具类
1.检查网络工具类
(1)检查网络是否连接
public class CheckNetUtils {
/**
* 检查手机网络连接状况
*
* @param context 上下文
* @return true 网络正常 false 网络异常
*/
public static boolean testNetWork(Context context) {
// 用上下文得到系统的连接管理器
ConnectivityManager connectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
// 用连接管理器得到手机当前网络信息
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
// 当网络信息不为空且为连接状态时,返回true
if (networkInfo != null && networkInfo.isConnected()) {
return true;
} else {
return false;
}
}
/**
* 打开网络设置界面
*/
public static void openSetting(Context context) {
if (android.os.Build.VERSION.SDK_INT > 10) {
//3.0以上打开设置界面,也可以直接用ACTION_WIRELESS_SETTINGS打开到wifi界面
context.startActivity(new Intent(android.provider.Settings.ACTION_SETTINGS));
} else {
context.startActivity(new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS));
}
}
}
public class NetworkBroadcast extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if (networkInfo == null) {
ToastUtils.showToast(context, "网络异常");
} else {
if (networkInfo.isConnected()) {
ToastUtil.showToast(context, "网络正常", Toast.LENGTH_SHORT);
if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
ToastUtils.showToast(context, "WIFI网络连接");
} else if (networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
ToastUtils.showToast(context, "移动网络连接");
} else if (networkInfo.getType() == ConnectivityManager.TYPE_ETHERNET) {
ToastUtils.showToast(context, "有线网络连接");
}
}
}
}
}
需要在清单文件里面注册此广播
<!-- 监听网络的广播 -->
<receiver android:name=".broadcast.NetworkBroadcast">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
2.Bitmap工具类
public class BitmapUtils {
private static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
while ((halfHeight / inSampleSize) > reqHeight
&& (halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
// 如果是放大图片,filter决定是否平滑,如果是缩小图片,filter无影响
private static Bitmap createScaleBitmap(Bitmap src, int dstWidth, int dstHeight) {
Bitmap dst = Bitmap.createScaledBitmap(src, dstWidth, dstHeight, false);
if (src != dst) { // 如果没有缩放,那么不回收
src.recycle(); // 释放Bitmap的native像素数组
}
return dst;
}
// 从Resources中加载图片
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options); // 读取图片长宽
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); // 计算inSampleSize
options.inJustDecodeBounds = false;
Bitmap src = BitmapFactory.decodeResource(res, resId, options); // 载入一个稍大的缩略图
return createScaleBitmap(src, reqWidth, reqHeight); // 进一步得到目标大小的缩略图
}
// 从sd卡上加载图片
public static Bitmap decodeSampledBitmapFromSd(String pathName, int reqWidth, int reqHeight) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(pathName, options);
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
options.inJustDecodeBounds = false;
Bitmap src = BitmapFactory.decodeFile(pathName, options);
return createScaleBitmap(src, reqWidth, reqHeight);
}
}
在这里贴一下Bitmap加载缩略图代码片段,这个缩略图是按比例压缩,从而保持图片的宽高比,效果就是图片不变形
//加载缩略图,参数p是图片路径
Bitmap bitmap = BitmapFactory.decodeFile(p);
Matrix matrix = new Matrix();
int w = bitmap.getWidth();
int h = bitmap.getHeight();
int t = w > h ? w : h;
float bi = 500.f / t;
matrix.postScale(bi, bi); //长和宽放大缩小的比例
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, false);
3.Toast工具类
(1).Toast全部自定义
public class ToastUtils {
private static Toast mToast;
/**
* 显示吐司
*
* @param context 上下文
* @param text 吐司文本
*/
public static void showToast(Context context, String text) {
//Toast要加载的view
View view = view = View.inflate(context, R.layout.toast, null);
TextView txt = ((TextView) view.findViewById(R.id.txt_toast));
if (mToast == null) {
//自定义Toast
mToast = new Toast(context);
txt.setText(text);
//给Toast设置布局
mToast.setView(view);
mToast.setDuration(Toast.LENGTH_SHORT);
//Toast在屏幕中间显示
mToast.setGravity(Gravity.CENTER, 0, 0);
} else {
txt.setText(text);
mToast.setView(view);
}
mToast.show();
}
}
下面是布局文件,只有一个TextView,可以自定义shape
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/txt_toast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/shape_txt_toast"
android:gravity="center"
android:paddingBottom="10dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="10dp"
android:text="吐司"
android:textColor="@color/bleak"
android:textSize="16sp"
android:textStyle="bold" />
</LinearLayout>
(2).简单的Toast封装,可以避免多次点击重复出现Toast
public class ToastUtils {
private static Toast mToast = null;
/**
* 显示Toast
*
* @param context 上下文
* @param text Toast文本
*/
public static void showToast(Context context, String text) {
if (mToast == null) {
mToast = Toast.makeText(context, text, Toast.LENGTH_SHORT);
} else {
mToast.setText(text);
mToast.setDuration(Toast.LENGTH_SHORT);
}
mToast.show();
}
}
3.SharedPreferences共享偏好工具类
共享偏好的名称都定义在工具类中,然后定义一个拿到共享偏好的方法,方便统一管理
package com.zidiv.medicine.utils;
import android.content.Context;
import android.content.SharedPreferences;
/**
* 共享偏好工具类
* Created by Administrator on 2016/2/29.
*/
public class SPUtils {
//是否是第一次打开的共享偏好名称
public static final String FIRSTSP = "firstSP";
//是否检查网络
public static final String NETSP = "netSP";
//是否启用缓存
public static final String CACHESP = "cacheSP";
/**
* 得到一个共享偏好
*
* @param context 上下文
* @param spName 共享偏好名称
* @return
*/
public static SharedPreferences getSP(Context context, String spName) {
SharedPreferences sp = context.getSharedPreferences(spName, Context.MODE_PRIVATE);
return sp;
}
}<strong>
</strong>
然后这样使用就可以了
sp = SPUtils.getSP(context, SPUtils.FIRSTSP);