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 ...
随机推荐
- 使用系统的CoreLocation定位
//// ViewController.m// LBS//// Created by tonnyhuang on 15/8/28.// Copyright (c) 2015年 tonnyhua ...
- HDU1459 非常可乐(BFS) 2016-07-24 15:00 165人阅读 评论(0) 收藏
非常可乐 Problem Description 大家一定觉的运动以后喝可乐是一件很惬意的事情,但是seeyou却不这么认为.因为每次当seeyou买了可乐以后,阿牛就要求和seeyou一起分享这一瓶 ...
- OpenGL中的旋转是可以叠加的?
OpenGL中的旋转是可以叠加的? 1. opengl中的旋转 如:glrogtate(45.0f, 0, 0, 1),是将当前坐标系顺时针旋转45度,然后绘制, 程序如下: ; float line ...
- springmvc 开涛 拦截器
拦截器有三个方法:preHandle, postHandle, afterCompletion ***-servlet.xml <bean name="/test" clas ...
- delphi 动态加载dll
引入文件 DLL比较复杂时,可以为它的声明专门创建一个引入单元,这会使该DLL变得更加容易维护和查看.引入单元的格式如下: unit MyDllImport; {Import unit for MyD ...
- 一起学习MVC(1)初步了解MVC
MVC 即模型视图控制器(Model View Controller) 利于团队开发.便于管理与维护.代码易读性强.未来的主流开发框架结构. 当然,缺点也显而易见,与传统开发框架相比有很大的不 ...
- jquery.cookie.js 删除cookie
简单交代一下背景:asp.net页面的上的切换登录按钮的点击事件实现cookie的删除. 但是好像没办法直接删除,通过网上提供的方法,可以使用jquery.cookie.js 来操作cookie的创建 ...
- IdentityServer4中文文档
欢迎IdentityServer4 IdentityServer4是ASP.NET Core 2的OpenID Connect和OAuth 2.0框架. 它在您的应用程序中启用以下功能: 认证即服务 ...
- (php)实现万年历
<?php //修改页面编码 header("content-type:text/html;charset=utf-8"); //获取当前年 $year=$_GET['y'] ...
- Eclipse (JavaEE版)中修改web项目的访问路径
访问路径,也就是指在浏览器中访问该web系统时的根路径,比如http://localhost:8080/xxxx/index.jsp 这里的xxxx. 也就是request.getContextPa ...