編輯:關於Android編程
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.ThumbnailUtils;
/**
* 縮略圖生成工具類
* @author
*
*/
public class ThumbnailGenerateUtils {
private ThumbnailGenerateUtils(){};
/**
* 根據指定的圖像路徑和大小來獲取縮略圖
* 此方法有兩點好處:
* 1. 使用較小的內存空間,第一次獲取的bitmap實際上為null,只是為了讀取寬度和高度,
* 第二次讀取的bitmap是根據比例壓縮過的圖像,第三次讀取的bitmap是所要的縮略圖。
* 2. 縮略圖對於原圖像來講沒有拉伸,這裡使用了2.2版本的新工具ThumbnailUtils,使
* 用這個工具生成的圖像不會被拉伸。
* @param imagePath 圖像的路徑
* @param width 指定輸出圖像的寬度
* @param height 指定輸出圖像的高度
* @return 生成的縮略圖
*/
public static Bitmap getImageThumbnail(String imagePath, int width, int height) {
Bitmap bitmap = null;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
// 獲取這個圖片的寬和高,注意此處的bitmap為null
bitmap = BitmapFactory.decodeFile(imagePath, options);
options.inJustDecodeBounds = false; // 設為 false
// 計算縮放比
int h = options.outHeight;
int w = options.outWidth;
int beWidth = w / width;
int beHeight = h / height;
int be = 1;
if (beWidth < beHeight) {
be = beWidth;
} else {
be = beHeight;
}
if (be <= 0) {
be = 1;
}
options.inSampleSize = be;
// 重新讀入圖片,讀取縮放後的bitmap,注意這次要把options.inJustDecodeBounds 設為 false
bitmap = BitmapFactory.decodeFile(imagePath, options);
// 利用ThumbnailUtils來創建縮略圖,這裡要指定要縮放哪個Bitmap對象
bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height,
ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
return bitmap;
}
/**
* 獲取視頻的縮略圖
* 先通過ThumbnailUtils來創建一個視頻的縮略圖,然後再利用ThumbnailUtils來生成指定大小的縮略圖。
* 如果想要的縮略圖的寬和高都小於MICRO_KIND,則類型要使用MICRO_KIND作為kind的值,這樣會節省內存。
* @param videoPath 視頻的路徑
* @param width 指定輸出視頻縮略圖的寬度
* @param height 指定輸出視頻縮略圖的高度度
* @param kind 參照MediaStore.Images.Thumbnails類中的常量MINI_KIND和MICRO_KIND。
* 其中,MINI_KIND: 512 x 384,MICRO_KIND: 96 x 96
* @return 指定大小的視頻縮略圖
*/
public static Bitmap getVideoThumbnail(String videoPath, int width, int height,int kind) {
Bitmap bitmap = null;
// 獲取視頻的縮略圖
bitmap = ThumbnailUtils.createVideoThumbnail(videoPath, kind);
System.out.println("w"+bitmap.getWidth());
System.out.println("h"+bitmap.getHeight());
bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height,
ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
return bitmap;
}
}
/**
* 密度計算工具
*
* @author zbzhangc
*
*/
public class DensityUtils {
public static int dip2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
public static int px2dip(Context context, float pxValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
}
}
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.os.Environment;
/**
* 文件名稱操作工具類
* @author zhang
*
*/
public class FileNameOperationUtils {
private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd");
private FileNameOperationUtils(){};
/**
* 生成文件夾
* @return 文件夾路徑
*/
public static String generateFolderName(String projectName){
String folderPath = Environment.getExternalStorageDirectory()+"/Province"+"/"+projectName
+"/"+dateFormat.format(new Date(System.currentTimeMillis()));
File folder = new File(folderPath);
if(!folder.exists())//創建文件夾
folder.mkdirs();
return folderPath;
}
/**
* 獲取圖片文件名稱
* @return
*/
public static String getPictrueFileName(){
return System.currentTimeMillis()/1000+".jpg";
}
/**
* 獲取視頻文件名稱
* @return
*/
public static String getVideoFileName(){
return System.currentTimeMillis()/1000+".mp4";
}
/**
* 獲取音頻文件名稱
* @return
*/
public static String getAudioFileName(){
return System.currentTimeMillis()/1000+".3gp";
}
/**
* 獲取圖片文件的全路徑名稱
* @return
*/
public static String getPictureAbsoluteFileName(String projectName){
return generateFolderName(projectName)+"/"+getPictrueFileName();
}
/***
* 獲取音頻文件的全路徑名稱
* @param projectName
* @return
*/
public static String getAudioAbsoluteFileName(String projectName){
return generateFolderName(projectName)+"/"+getAudioFileName();
}
/**
* 替換文件夾名稱
* @param fileName
* @param newFolderName
* @return
*/
public static boolean renameFolder(String fileName,String newFolderName){
File file = new File(fileName);
if(!file.isDirectory()){
String folderPath = file.getPath().substring(0,file.getPath().lastIndexOf("\\"));//當前文件夾名稱
String oldFolderName = folderPath.substring(folderPath.lastIndexOf("\\")+1);//要替換文件夾名稱
return new File(folderPath).renameTo(new File(folderPath.replace(oldFolderName, newFolderName)));
}else{
System.out.println(file.getPath());
String oldFolderName = file.getPath().substring(file.getPath().lastIndexOf("\\")+1);
System.out.println(oldFolderName);
return file.renameTo(new File(file.getPath().replace(oldFolderName, newFolderName)));
}
}
}
package com.iss.starwish.util;
import android.content.Context;
import android.widget.Toast;
/**
* 防止按鈕連續點擊
* @author zhang
*
*/
public class Utils {
private static long lastClickTime;
/**
* 防止用戶在800ms裡面的連續點擊
**/
public static boolean isFastDoubleClick() {
long time = System.currentTimeMillis();
long timeD = time - lastClickTime;
if (0 < timeD && timeD < 800) {
return true;
}
lastClickTime = time;
return false;
}
/**
* 顯示Toast
* @param context
* @param content
*/
public static void show(Context context,String content){
Toast.makeText(context, content, Toast.LENGTH_SHORT).show();
}
/**
* 顯示Toast
* @param context
* @param content
*/
public static void show(Context context,int strId){
Toast.makeText(context, strId, Toast.LENGTH_SHORT).show();
}
}
package net.tianyouwang.utils;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
public class ChineseToPinyinUtil {
private ChineseToPinyinUtil() {
}
/***
* 漢子轉換成拼音
* @param src
* @return
*/
public static String getPingYin(String src) {
char[] t1 = null;
t1 = src.toCharArray();
String[] t2 = new String[t1.length];
HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();
t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);
t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
t3.setVCharType(HanyuPinyinVCharType.WITH_V);
String t4 = "";
int t0 = t1.length;
try {
for (int i = 0; i < t0; i++)
{
// 判斷是否為漢字字符
if (java.lang.Character.toString(t1[i]).matches(
"[\\u4E00-\\u9FA5]+"))
{
t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);
t4 += t2[0];
}
else
t4 += java.lang.Character.toString(t1[i]);
}
// System.out.println(t4);
return t4;
}
catch (BadHanyuPinyinOutputFormatCombination e1) {
e1.printStackTrace();
}
return t4;
}
// 返回中文的首字母
public static String getPinYinHeadChar(String str) {
String convert = "";
for (int j = 0; j < str.length(); j++) {
char word = str.charAt(j);
String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);
if (pinyinArray != null) {
convert += pinyinArray[0].charAt(0);
} else {
convert += word;
}
}
return convert;
}
}
package net.tianyouwang.utils;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;
public class NetUtils {
/***
* 判斷當前網絡是否連接
*
* @param con
* @return
*/
public static boolean isNetworkAvailable(Context context) {
ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (cm == null)
return false;
NetworkInfo netinfo = cm.getActiveNetworkInfo();
if (netinfo == null) {
return false;
}
if (netinfo.isConnected()) {
return true;
}
return false;
}
/****
* 獲取當前的網絡類型
* @param context
* @return
*/
public static String getNetWorkType(Context context){
ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (cm == null)
return "";
NetworkInfo netinfo = cm.getActiveNetworkInfo();
if(netinfo != null){
int type = netinfo.getType();
if(type == 0){//手機
int subtype = netinfo.getSubtype();
if(subtype == TelephonyManager.NETWORK_TYPE_CDMA){//電信2G
return "2g";
} else if(subtype == TelephonyManager.NETWORK_TYPE_EVDO_0 || subtype == TelephonyManager.NETWORK_TYPE_EVDO_A
|| subtype == TelephonyManager.NETWORK_TYPE_EVDO_B){//電信3G
return "3g";
} else if(subtype == TelephonyManager.NETWORK_TYPE_GPRS){//聯通2g
return "2g";
} else if(subtype == TelephonyManager.NETWORK_TYPE_EDGE){//移動2G
return "2g";
} else if(subtype == TelephonyManager.NETWORK_TYPE_HSDPA ||
subtype == TelephonyManager.NETWORK_TYPE_UMTS){//聯通3g
return "3g";
}
} else if(type == 1){//wifi
return "wifi";
}
}
return "3g";
}
}
react native 實戰系列教程之Navigator實現頁面跳轉
主界面開發上一節,我們已經完成了首頁的開發,現在,我們繼續完成主界面的開發,就是添加底部‘首頁’和‘我的’兩個tabbar
Android組件DrawerLayout仿網易新聞v4.4側滑菜單
概述 今天這篇博客將記錄一些關於DrawerLayout的基本用法,我想關於DrawerLayou
Android 圖片SD卡緩存 使用簡單 支持預取 支持多種緩存算法 支持不同網絡類型 支持序列化 (八)
本文主要介紹一個支持圖片自動預取、支持多種緩存算法、支持數據保存和恢復的圖片Sd卡緩存的使用、功能及網友反饋的常見問題解答。與AndroidLruCache相比主要特性:
百度地圖 Android SDK - 標注(Marker)的基本使用
標注(Marker)是開發者最常使用的地圖覆蓋物志一,今天就來向大家介紹一些標注(Marker)的最基本使用方法! 實現目標: 1、構建基礎地圖頁面; 2、在地圖的中心點