編輯:關於Android編程
Android 設備工具DeviceUtil dp轉換px px轉換dp 設備寬度 設備高度 SD卡判斷 網絡判斷 VersionName VersionCode DeviceId 手機品牌 手機型號 系統Android API等級 系統Android 版本 App進程id App進程Name 創建App緩存文件夾 Uri轉換File 獲取AndroidManifestxml裡meta data的值 DeviceUtil全部源碼
在自定義View中必須用到這個方法。自定義View的時候,不能像在xml那樣可以設置dp(密度),只能設置px(像素)。不同分辨率的設備,設置px,顯示就不會一樣了。為了結合設計圖設置dp,需要將dp轉換為px值,然後再設置。
/**
* dp 轉化為 px
* @param context
* @param dpValue
* @return
*/
public static int dp2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
/**
* px 轉化為 dp
* @param context
* @param pxValue
* @return
*/
public static int px2dp(Context context, float pxValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
}
/**
* 獲取設備寬度(px)
* @param context
* @return
*/
public static int deviceWidth(Context context) {
return context.getResources().getDisplayMetrics().widthPixels;
}
/**
* 獲取設備高度(px)
* @param context
* @return
*/
public static int deviceHeight(Context context) {
return context.getResources().getDisplayMetrics().heightPixels;
}
/**
* SD卡判斷
* @return
*/
public static boolean isSDCardAvailable() {
return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
}
/**
* 是否有網
* @param context
* @return
*/
public static boolean isNetworkConnected(Context context) {
if (context != null) {
ConnectivityManager mConnectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mNetworkInfo = mConnectivityManager
.getActiveNetworkInfo();
if (mNetworkInfo != null) {
return mNetworkInfo.isAvailable();
}
}
return false;
}
對應build.gradle中的versionName
/**
* 返回版本名字
* 對應build.gradle中的versionName
*
* @param context
* @return
*/
public static String getVersionName(Context context) {
String versionName = ;
try {
PackageManager packageManager = context.getPackageManager();
PackageInfo packInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
versionName = packInfo.versionName;
} catch (Exception e) {
e.printStackTrace();
}
return versionName;
}
對應build.gradle中的versionCode
/**
* 返回版本號
* 對應build.gradle中的versionCode
*
* @param context
* @return
*/
public static String getVersionCode(Context context) {
String versionCode = ;
try {
PackageManager packageManager = context.getPackageManager();
PackageInfo packInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
versionCode = String.valueOf(packInfo.versionCode);
} catch (Exception e) {
e.printStackTrace();
}
return versionCode;
}
每台設備都有唯一的標識,這就是DeviceId
/**
* 獲取設備的唯一標識,deviceId
*
* @param context
* @return
*/
public static String getDeviceId(Context context) {
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
String deviceId = tm.getDeviceId();
if (deviceId == null) {
return -;
} else {
return deviceId;
}
}
/**
* 獲取手機品牌
*
* @return
*/
public static String getPhoneBrand() {
return android.os.Build.BRAND;
}
/**
* 獲取手機型號
*
* @return
*/
public static String getPhoneModel() {
return android.os.Build.MODEL;
}
/**
* 獲取手機Android API等級(22、23 ...)
*
* @return
*/
public static int getBuildLevel() {
return android.os.Build.VERSION.SDK_INT;
}
/**
* 獲取手機Android 版本(4.4、5.0、5.1 ...)
*
* @return
*/
public static String getBuildVersion() {
return android.os.Build.VERSION.RELEASE;
}
/**
* 獲取當前App進程的id
*
* @return
*/
public static int getAppProcessId() {
return android.os.Process.myPid();
}
/**
* 獲取當前App進程的Name
*
* @param context
* @param processId
* @return
*/
public static String getAppProcessName(Context context, int processId) {
String processName = null;
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
// 獲取所有運行App的進程集合
List l = am.getRunningAppProcesses();
Iterator i = l.iterator();
PackageManager pm = context.getPackageManager();
while (i.hasNext()) {
ActivityManager.RunningAppProcessInfo info = (ActivityManager.RunningAppProcessInfo) (i.next());
try {
if (info.pid == processId) {
CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(info.processName, PackageManager.GET_META_DATA));
processName = info.processName;
return processName;
}
} catch (Exception e) {
Log.e(DeviceUtil.class.getName(), e.getMessage(), e);
}
}
return processName;
}
/**
* 創建App文件夾
*
* @param appName
* @param application
* @return
*/
public static String createAPPFolder(String appName, Application application) {
return createAPPFolder(appName, application, null);
}
/**
* 創建App文件夾
*
* @param appName
* @param application
* @param folderName
* @return
*/
public static String createAPPFolder(String appName, Application application, String folderName) {
File root = Environment.getExternalStorageDirectory();
File folder;
/**
* 如果存在SD卡
*/
if (DeviceUtil.isSDCardAvailable() && root != null) {
folder = new File(root, appName);
if (!folder.exists()) {
folder.mkdirs();
}
} else {
/**
* 不存在SD卡,就放到緩存文件夾內
*/
root = application.getCacheDir();
folder = new File(root, appName);
if (!folder.exists()) {
folder.mkdirs();
}
}
if (folderName != null) {
folder = new File(folder, folderName);
if (!folder.exists()) {
folder.mkdirs();
}
}
return folder.getAbsolutePath();
}
/**
* 通過Uri找到File
*
* @param context
* @param uri
* @return
*/
public static File uri2File(Activity context, Uri uri) {
File file;
String[] project = {MediaStore.Images.Media.DATA};
Cursor actualImageCursor = context.getContentResolver().query(uri, project, null, null, null);
if (actualImageCursor != null) {
int actual_image_column_index = actualImageCursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
actualImageCursor.moveToFirst();
String img_path = actualImageCursor
.getString(actual_image_column_index);
file = new File(img_path);
} else {
file = new File(uri.getPath());
}
if (actualImageCursor != null) actualImageCursor.close();
return file;
}
AndroidManifest.xml
比如自定義了一個meta-data
,名為DEBUG。
build.gradle
然後在build.gradle裡給這個DEBUG賦值了。
//渠道Flavors
productFlavors {
APPTest {
applicationId com.zyy.camnter.newlife
manifestPlaceholders = [DEBUG : debug]
}
}
可以通過一下方法取得AndroidManifest.xml裡DEBUG被賦上的值:
/**
* 獲取AndroidManifest.xml裡 的值
*
* @param context
* @param name
* @return
*/
public static String getMetaData(Context context, String name) {
String value = null;
try {
ApplicationInfo appInfo = context.getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
value = appInfo.metaData.getString(name);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return value;
}
public class DeviceUtil {
/**
* dp 轉化為 px
*
* @param context
* @param dpValue
* @return
*/
public static int dp2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
/**
* px 轉化為 dp
*
* @param context
* @param pxValue
* @return
*/
public static int px2dp(Context context, float pxValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
}
/**
* 獲取設備寬度(px)
*
* @param context
* @return
*/
public static int deviceWidth(Context context) {
return context.getResources().getDisplayMetrics().widthPixels;
}
/**
* 獲取設備高度(px)
*
* @param context
* @return
*/
public static int deviceHeight(Context context) {
return context.getResources().getDisplayMetrics().heightPixels;
}
/**
* SD卡判斷
*
* @return
*/
public static boolean isSDCardAvailable() {
return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
}
/**
* 是否有網
*
* @param context
* @return
*/
public static boolean isNetworkConnected(Context context) {
if (context != null) {
ConnectivityManager mConnectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mNetworkInfo = mConnectivityManager
.getActiveNetworkInfo();
if (mNetworkInfo != null) {
return mNetworkInfo.isAvailable();
}
}
return false;
}
/**
* 返回版本名字
* 對應build.gradle中的versionName
*
* @param context
* @return
*/
public static String getVersionName(Context context) {
String versionName = ;
try {
PackageManager packageManager = context.getPackageManager();
PackageInfo packInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
versionName = packInfo.versionName;
} catch (Exception e) {
e.printStackTrace();
}
return versionName;
}
/**
* 返回版本號
* 對應build.gradle中的versionCode
*
* @param context
* @return
*/
public static String getVersionCode(Context context) {
String versionCode = ;
try {
PackageManager packageManager = context.getPackageManager();
PackageInfo packInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
versionCode = String.valueOf(packInfo.versionCode);
} catch (Exception e) {
e.printStackTrace();
}
return versionCode;
}
/**
* 獲取設備的唯一標識,deviceId
*
* @param context
* @return
*/
public static String getDeviceId(Context context) {
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
String deviceId = tm.getDeviceId();
if (deviceId == null) {
return -;
} else {
return deviceId;
}
}
/**
* 獲取手機品牌
*
* @return
*/
public static String getPhoneBrand() {
return android.os.Build.BRAND;
}
/**
* 獲取手機型號
*
* @return
*/
public static String getPhoneModel() {
return android.os.Build.MODEL;
}
/**
* 獲取手機Android API等級(22、23 ...)
*
* @return
*/
public static int getBuildLevel() {
return android.os.Build.VERSION.SDK_INT;
}
/**
* 獲取手機Android 版本(4.4、5.0、5.1 ...)
*
* @return
*/
public static String getBuildVersion() {
return android.os.Build.VERSION.RELEASE;
}
/**
* 獲取當前App進程的id
*
* @return
*/
public static int getAppProcessId() {
return android.os.Process.myPid();
}
/**
* 獲取當前App進程的Name
*
* @param context
* @param processId
* @return
*/
public static String getAppProcessName(Context context, int processId) {
String processName = null;
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
// 獲取所有運行App的進程集合
List l = am.getRunningAppProcesses();
Iterator i = l.iterator();
PackageManager pm = context.getPackageManager();
while (i.hasNext()) {
ActivityManager.RunningAppProcessInfo info = (ActivityManager.RunningAppProcessInfo) (i.next());
try {
if (info.pid == processId) {
CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(info.processName, PackageManager.GET_META_DATA));
processName = info.processName;
return processName;
}
} catch (Exception e) {
Log.e(DeviceUtil.class.getName(), e.getMessage(), e);
}
}
return processName;
}
/**
* 創建App文件夾
*
* @param appName
* @param application
* @return
*/
public static String createAPPFolder(String appName, Application application) {
return createAPPFolder(appName, application, null);
}
/**
* 創建App文件夾
*
* @param appName
* @param application
* @param folderName
* @return
*/
public static String createAPPFolder(String appName, Application application, String folderName) {
File root = Environment.getExternalStorageDirectory();
File folder;
/**
* 如果存在SD卡
*/
if (DeviceUtil.isSDCardAvailable() && root != null) {
folder = new File(root, appName);
if (!folder.exists()) {
folder.mkdirs();
}
} else {
/**
* 不存在SD卡,就放到緩存文件夾內
*/
root = application.getCacheDir();
folder = new File(root, appName);
if (!folder.exists()) {
folder.mkdirs();
}
}
if (folderName != null) {
folder = new File(folder, folderName);
if (!folder.exists()) {
folder.mkdirs();
}
}
return folder.getAbsolutePath();
}
/**
* 通過Uri找到File
*
* @param context
* @param uri
* @return
*/
public static File uri2File(Activity context, Uri uri) {
File file;
String[] project = {MediaStore.Images.Media.DATA};
Cursor actualImageCursor = context.getContentResolver().query(uri, project, null, null, null);
if (actualImageCursor != null) {
int actual_image_column_index = actualImageCursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
actualImageCursor.moveToFirst();
String img_path = actualImageCursor
.getString(actual_image_column_index);
file = new File(img_path);
} else {
file = new File(uri.getPath());
}
if (actualImageCursor != null) actualImageCursor.close();
return file;
}
/**
* 獲取AndroidManifest.xml裡 的值
*
* @param context
* @param name
* @return
*/
public static String getMetaData(Context context, String name) {
String value = null;
try {
ApplicationInfo appInfo = context.getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
value = appInfo.metaData.getString(name);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return value;
}
}
Tween動畫Tween動畫又稱補間動畫。通過對view的位置、大小、透明度、角度的改變來實現動畫效果。補間動畫的基類是Animation。我們通常使用它的直接子類Rot
這是Android UI Fundamentals裡的內容 創建自定義視圖 創建自定義UI組件首先要繼承一個視圖類. 首先創建一個簡單的自定義視圖, 展示一條十
前言之前,我們介紹了下拉刷新上拉加載RecyclerView的使用,那麼現在,我們就來說一下這個下拉刷新是怎麼實現的。在開發過程中,我想了兩種方案。一是使用LinearL
1、Fragment的靜態使用Fragment是作為Activity的UI的一部分,它內嵌在Activity中,多個Fragment可以把一個Activity分成多個部分