码迷,mamicode.com
首页 > 移动开发 > 详细

Android 开发工具类 11_ToolFor9Ge

时间:2015-05-28 15:57:24      阅读:224      评论:0      收藏:0      [点我收藏+]

标签:

1、缩放/ 裁剪图片;

2、判断有无网络链接;

3、从路径获取文件名;

4、通过路径生成 Base64 文件;

5、通过文件路径获取到 bitmap;

6、把 bitmap 转换成 base64;

7、把 base64 转换成 bitmap;

8、把 Stream 转换成 String;

9、修改整个界面所有控件的字体;

10、修改整个界面所有控件的字体大小;

11、不改变控件位置,修改控件大小;

12、修改控件的高。

  1 import java.io.BufferedReader;
  2 import java.io.ByteArrayOutputStream;
  3 import java.io.File;
  4 import java.io.FileInputStream;
  5 import java.io.IOException;
  6 import java.io.InputStream;
  7 import java.io.InputStreamReader;
  8 import java.lang.ref.WeakReference;
  9 
 10 import android.app.Activity;
 11 import android.content.Context;
 12 import android.graphics.Bitmap;
 13 import android.graphics.BitmapFactory;
 14 import android.graphics.Matrix;
 15 import android.graphics.Bitmap.CompressFormat;
 16 import android.graphics.Typeface;
 17 import android.net.ConnectivityManager;
 18 import android.net.NetworkInfo.State;
 19 import android.util.Base64;
 20 import android.view.View;
 21 import android.view.ViewGroup;
 22 import android.view.ViewGroup.LayoutParams;
 23 import android.widget.Button;
 24 import android.widget.EditText;
 25 import android.widget.TextView;
 26 
 27 public class ToolFor9Ge 
 28 {
 29     // 缩放/ 裁剪图片
 30      public static Bitmap zoomImg(Bitmap bm, int newWidth ,int newHeight)
 31      { 
 32          // 获得图片的宽高
 33          int width = bm.getWidth();
 34          int height = bm.getHeight();
 35          // 计算缩放比例
 36          float scaleWidth = ((float) newWidth) / width;
 37          float scaleHeight = ((float) newHeight) / height;
 38          // 取得想要缩放的 matrix 参数
 39          Matrix matrix = new Matrix();
 40          matrix.postScale(scaleWidth, scaleHeight);
 41          // 得到新的图片
 42          Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);
 43          return newbm;
 44      }
 45      
 46      // 判断有无网络链接
 47      public static boolean checkNetworkInfo(Context mContext) {
 48            ConnectivityManager conMan = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
 49            State mobile = conMan.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
 50            State wifi = conMan.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
 51            if (mobile == State.CONNECTED || mobile == State.CONNECTING)
 52               return true;
 53            if (wifi == State.CONNECTED || wifi == State.CONNECTING)
 54               return true;
 55            return false;
 56      }
 57      
 58      // 从路径获取文件名
 59      public static String getFileName(String pathandname){ 
 60          int start = pathandname.lastIndexOf("/");  
 61          int end = pathandname.lastIndexOf(".");  
 62          if(start != -1 && end != -1){  
 63              return pathandname.substring(start+1,end);    
 64          }else{  
 65              return null;  
 66          }             
 67      }
 68      
 69      // 通过路径生成 Base64 文件
 70      public static String getBase64FromPath(String path)
 71      {
 72          String base64 = "";
 73          try
 74          {
 75              File file = new File(path);
 76              byte[] buffer = new byte[(int) file.length() + 100];  
 77             @SuppressWarnings("resource")
 78             int length = new FileInputStream(file).read(buffer);  
 79              base64 = Base64.encodeToString(buffer, 0, length,  Base64.DEFAULT);
 80          }
 81          catch (IOException e) {
 82             e.printStackTrace();
 83         }
 84          return base64;
 85      }
 86      
 87      // 通过文件路径获取到 bitmap
 88      public static Bitmap getBitmapFromPath(String path, int w, int h) {
 89         BitmapFactory.Options opts = new BitmapFactory.Options();
 90         // 设置为ture只获取图片大小
 91         opts.inJustDecodeBounds = true;
 92         opts.inPreferredConfig = Bitmap.Config.ARGB_8888;
 93         // 返回为空
 94         BitmapFactory.decodeFile(path, opts);
 95         int width = opts.outWidth;
 96         int height = opts.outHeight;
 97         float scaleWidth = 0.f, scaleHeight = 0.f;
 98         if (width > w || height > h) {
 99             // 缩放
100             scaleWidth = ((float) width) / w;
101             scaleHeight = ((float) height) / h;
102         }
103         opts.inJustDecodeBounds = false;
104         float scale = Math.max(scaleWidth, scaleHeight);
105         opts.inSampleSize = (int)scale;
106         WeakReference<Bitmap> weak = new WeakReference<Bitmap>(BitmapFactory.decodeFile(path, opts));
107         return Bitmap.createScaledBitmap(weak.get(), w, h, true);
108     }
109      
110      // 把 bitmap 转换成 base64
111      public static String getBase64FromBitmap(Bitmap bitmap, int bitmapQuality)
112      {
113          ByteArrayOutputStream bStream = new ByteArrayOutputStream();
114          bitmap.compress(CompressFormat.PNG, bitmapQuality, bStream);
115          byte[] bytes = bStream.toByteArray();
116          return Base64.encodeToString(bytes, Base64.DEFAULT);
117      }
118      
119      // 把 base64 转换成 bitmap
120      public static Bitmap getBitmapFromBase64(String string)
121      {
122          byte[] bitmapArray = null;
123          try {
124          bitmapArray = Base64.decode(string, Base64.DEFAULT);
125          } catch (Exception e) {
126          e.printStackTrace();
127          }
128          return BitmapFactory.decodeByteArray(bitmapArray, 0,bitmapArray.length);
129      }
130      
131      // 把 Stream 转换成 String
132      public static String convertStreamToString(InputStream is) {
133          BufferedReader reader = new BufferedReader(new InputStreamReader(is));
134             StringBuilder sb = new StringBuilder();
135             String line = null;
136 
137             try {
138                 while ((line = reader.readLine()) != null) {
139                     sb.append(line + "/n");
140                 }
141             } catch (IOException e) {
142                 e.printStackTrace();
143             } finally {
144                 try {
145                     is.close();
146                 } catch (IOException e) {
147                     e.printStackTrace();
148                 }
149             }
150             return sb.toString();    
151      }    
152      
153      // 修改整个界面所有控件的字体
154      public static void changeFonts(ViewGroup root,String path, Activity act) {  
155         // path 是字体路径
156          Typeface tf = Typeface.createFromAsset(act.getAssets(),path);  
157         for (int i = 0; i < root.getChildCount(); i++) {  
158             View v = root.getChildAt(i); 
159             if (v instanceof TextView) {  
160                ((TextView) v).setTypeface(tf);  
161             } else if (v instanceof Button) {  
162                ((Button) v).setTypeface(tf);  
163             } else if (v instanceof EditText) {  
164                ((EditText) v).setTypeface(tf);  
165             } else if (v instanceof ViewGroup) {  
166                changeFonts((ViewGroup) v, path,act);  
167             } 
168         }  
169      }
170      
171      // 修改整个界面所有控件的字体大小
172       public static void changeTextSize(ViewGroup root,int size, Activity act) {  
173          for (int i = 0; i < root.getChildCount(); i++) {  
174              View v = root.getChildAt(i);  
175              if (v instanceof TextView) {  
176                 ((TextView) v).setTextSize(size);
177              } else if (v instanceof Button) {  
178                 ((Button) v).setTextSize(size);
179              } else if (v instanceof EditText) {  
180                 ((EditText) v).setTextSize(size);  
181              } else if (v instanceof ViewGroup) {  
182                 changeTextSize((ViewGroup) v,size,act);  
183              }  
184          }  
185       }
186       
187       // 不改变控件位置,修改控件大小
188     public static void changeWH(View v,int W,int H)
189     {
190         LayoutParams params = (LayoutParams)v.getLayoutParams();
191         params.width = W;
192         params.height = H;
193         v.setLayoutParams(params);
194     }
195     
196     // 修改控件的高
197     public static void changeH(View v,int H)
198     {
199         LayoutParams params = (LayoutParams)v.getLayoutParams();
200         params.height = H;
201         v.setLayoutParams(params);
202     }
203         
204 }

 

Android 开发工具类 11_ToolFor9Ge

标签:

原文地址:http://www.cnblogs.com/renzimu/p/4535736.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!