Android 开发工具类 11_ToolFor9Ge
1、缩放/ 裁剪图片;
2、判断有无网络链接;
3、从路径获取文件名;
4、通过路径生成 Base64 文件;
5、通过文件路径获取到 bitmap;
6、把 bitmap 转换成 base64;
7、把 base64 转换成 bitmap;
8、把 Stream 转换成 String;
9、修改整个界面所有控件的字体;
10、修改整个界面所有控件的字体大小;
11、不改变控件位置,修改控件大小;
12、修改控件的高。
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.ref.WeakReference; import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.Typeface;
import android.net.ConnectivityManager;
import android.net.NetworkInfo.State;
import android.util.Base64;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView; public class ToolFor9Ge
{
// 缩放/ 裁剪图片
public static Bitmap zoomImg(Bitmap bm, int newWidth ,int newHeight)
{
// 获得图片的宽高
int width = bm.getWidth();
int height = bm.getHeight();
// 计算缩放比例
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// 取得想要缩放的 matrix 参数
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
// 得到新的图片
Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);
return newbm;
} // 判断有无网络链接
public static boolean checkNetworkInfo(Context mContext) {
ConnectivityManager conMan = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
State mobile = conMan.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
State wifi = conMan.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
if (mobile == State.CONNECTED || mobile == State.CONNECTING)
return true;
if (wifi == State.CONNECTED || wifi == State.CONNECTING)
return true;
return false;
} // 从路径获取文件名
public static String getFileName(String pathandname){
int start = pathandname.lastIndexOf("/");
int end = pathandname.lastIndexOf(".");
if(start != -1 && end != -1){
return pathandname.substring(start+1,end);
}else{
return null;
}
} // 通过路径生成 Base64 文件
public static String getBase64FromPath(String path)
{
String base64 = "";
try
{
File file = new File(path);
byte[] buffer = new byte[(int) file.length() + 100];
@SuppressWarnings("resource")
int length = new FileInputStream(file).read(buffer);
base64 = Base64.encodeToString(buffer, 0, length, Base64.DEFAULT);
}
catch (IOException e) {
e.printStackTrace();
}
return base64;
} // 通过文件路径获取到 bitmap
public static Bitmap getBitmapFromPath(String path, int w, int h) {
BitmapFactory.Options opts = new BitmapFactory.Options();
// 设置为ture只获取图片大小
opts.inJustDecodeBounds = true;
opts.inPreferredConfig = Bitmap.Config.ARGB_8888;
// 返回为空
BitmapFactory.decodeFile(path, opts);
int width = opts.outWidth;
int height = opts.outHeight;
float scaleWidth = 0.f, scaleHeight = 0.f;
if (width > w || height > h) {
// 缩放
scaleWidth = ((float) width) / w;
scaleHeight = ((float) height) / h;
}
opts.inJustDecodeBounds = false;
float scale = Math.max(scaleWidth, scaleHeight);
opts.inSampleSize = (int)scale;
WeakReference<Bitmap> weak = new WeakReference<Bitmap>(BitmapFactory.decodeFile(path, opts));
return Bitmap.createScaledBitmap(weak.get(), w, h, true);
} // 把 bitmap 转换成 base64
public static String getBase64FromBitmap(Bitmap bitmap, int bitmapQuality)
{
ByteArrayOutputStream bStream = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, bitmapQuality, bStream);
byte[] bytes = bStream.toByteArray();
return Base64.encodeToString(bytes, Base64.DEFAULT);
} // 把 base64 转换成 bitmap
public static Bitmap getBitmapFromBase64(String string)
{
byte[] bitmapArray = null;
try {
bitmapArray = Base64.decode(string, Base64.DEFAULT);
} catch (Exception e) {
e.printStackTrace();
}
return BitmapFactory.decodeByteArray(bitmapArray, 0,bitmapArray.length);
} // 把 Stream 转换成 String
public static String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null; try {
while ((line = reader.readLine()) != null) {
sb.append(line + "/n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
} // 修改整个界面所有控件的字体
public static void changeFonts(ViewGroup root,String path, Activity act) {
// path 是字体路径
Typeface tf = Typeface.createFromAsset(act.getAssets(),path);
for (int i = 0; i < root.getChildCount(); i++) {
View v = root.getChildAt(i);
if (v instanceof TextView) {
((TextView) v).setTypeface(tf);
} else if (v instanceof Button) {
((Button) v).setTypeface(tf);
} else if (v instanceof EditText) {
((EditText) v).setTypeface(tf);
} else if (v instanceof ViewGroup) {
changeFonts((ViewGroup) v, path,act);
}
}
} // 修改整个界面所有控件的字体大小
public static void changeTextSize(ViewGroup root,int size, Activity act) {
for (int i = 0; i < root.getChildCount(); i++) {
View v = root.getChildAt(i);
if (v instanceof TextView) {
((TextView) v).setTextSize(size);
} else if (v instanceof Button) {
((Button) v).setTextSize(size);
} else if (v instanceof EditText) {
((EditText) v).setTextSize(size);
} else if (v instanceof ViewGroup) {
changeTextSize((ViewGroup) v,size,act);
}
}
} // 不改变控件位置,修改控件大小
public static void changeWH(View v,int W,int H)
{
LayoutParams params = (LayoutParams)v.getLayoutParams();
params.width = W;
params.height = H;
v.setLayoutParams(params);
} // 修改控件的高
public static void changeH(View v,int H)
{
LayoutParams params = (LayoutParams)v.getLayoutParams();
params.height = H;
v.setLayoutParams(params);
} }
Android 开发工具类 11_ToolFor9Ge的更多相关文章
- Android开发工具类
7种无须编程的DIY开发工具 你知道几个? 现如今,各种DIY开发工具不断的出现,使得企业和个人在短短几分钟内就能完成应用的创建和发布,大大节省了在时间和资金上的投入.此外,DIY工 具的出现,也帮助 ...
- android开发工具类之获得WIFI IP地址或者手机网络IP
有的时候我们需要获得WIFI的IP地址获得手机网络的IP地址,这是一个工具类,专门解决这个问题,这里需要两个权限: <uses-permission android:name="and ...
- android开发工具类总结(一)
一.日志工具类 Log.java public class L { private L() { /* 不可被实例化 */ throw new UnsupportedOperationException ...
- Android 开发工具类 35_PatchUtils
增量更新工具类[https://github.com/cundong/SmartAppUpdates] import java.io.File; import android.app.Activity ...
- Android 开发工具类 13_ SaxService
网络 xml 解析方式 package com.example.dashu_saxxml; import java.io.IOException; import java.io.InputStream ...
- Android 开发工具类 06_NetUtils
跟网络相关的工具类: 1.判断网络是否连接: 2.判断是否是 wifi 连接: 3.打开网络设置界面: import android.app.Activity; import android.cont ...
- Android 开发工具类 03_HttpUtils
Http 请求的工具类: 1.异步的 Get 请求: 2.异步的 Post 请求: 3.Get 请求,获得返回数据: 4.向指定 URL 发送 POST方法的请求. import java.io.Bu ...
- Android 开发工具类 19_NetworkStateReceiver
检测网络状态改变类: 1.注册网络状态广播: 2.检查网络状态: 3.注销网络状态广播: 4.获取当前网络状态,true为网络连接成功,否则网络连接失败: 5.注册网络连接观察者: 6.注销网络连接观 ...
- Android 开发工具类 27_多线程下载大文件
多线程下载大文件时序图 FileDownloader.java package com.wangjialin.internet.service.downloader; import java.io.F ...
随机推荐
- 8) pom.xml
http://maven.apache.org/ref/3.3.3/maven-model/maven.html 执行mvn命令的时候默认文件名pom.xml 也可以通过 -f 指定 比如 mvn - ...
- Get异步请求
1: 使用代理 //1.delegate //创建url NSURL *url = [[NSURL alloc] initWithString:@"http:ima ...
- 【翻译】追溯“typeof null”的历史
我的翻译小站:https://www.zcfy.cc/article/the-history-of-typeof-null 翻译原文链接:http://2ality.com/2013/10/typeo ...
- OpenGL中的旋转是可以叠加的?
OpenGL中的旋转是可以叠加的? 1. opengl中的旋转 如:glrogtate(45.0f, 0, 0, 1),是将当前坐标系顺时针旋转45度,然后绘制, 程序如下: ; float line ...
- (转)mmap和shm共享内存的区别和联系
共享内存的创建 根据理论: 1. 共享内存允许两个或多个进程共享一给定的存储区,因为数据不需要来回复制,所以是最快的一种进程间通信机制.共享内存可以通过mmap()映射普通文件 (特殊情况下还可以采用 ...
- Android SQLiteOpenHelper Sqlite数据库的创建与打开
Android Sqlite数据库是一个怎样的数据库? 答:是一种嵌入式小型设备,移动设备,的数据库,应用在穿戴设备(例如:智能手表,计算手环 等等),移动设备(例如:Android系统类型的手机 等 ...
- fleet中service之间的依赖关系
最近有人在topcoder上提出使用fleet在集群上部署service时有时候会发现,当启动依赖于整个集群服务的service时,只会检查那个service所在机器的依赖关系,这样就会造成一些问题, ...
- ActiveMq 总结(一)
1. 背景 当前,CORBA.DCOM.RMI等RPC中间件技术已广泛应用于各个领域.但是面对规模和复杂度都越来越高的分布式系统,这些技术也显示出其局限性:(1)同步通信:客户发出调用后,必须等待服务 ...
- DBCC--CHECKDB--结果收集
--由宋沄剑提供 CREATE TABLE [dbo].[dbcc_history]( [Error] [int] NULL, [Level] [int] NULL, [State] [int] NU ...
- Microsoft.Web.Administration操作IIS7时的权限设置
在用Microsoft.Web.Administration操作IIS7时,你可能会遇到如下权限错误: 文件名: redirection.config错误: 由于权限不足而无法读取配置文件 如下图: ...