1. 程式人生 > >使用Java程式碼的方式實現簡易的走勢圖(1)

使用Java程式碼的方式實現簡易的走勢圖(1)

這是我第二篇CSDN文章了,寫這篇文章的時候,心態已經發生了些變化。因為最近看了一個朋友介紹的一本書《奇特的一生》。在很多人看來,書中的主人翁柳比歇夫生活、工作的方法飽受爭議。但是,在我看來,如果一個普通人,想要在一生之中有點成就,想要給這個世界留點什麼有價值的東西,柳比歇夫的方法或許可以起到很大的作用。當我嘗試去按照他的方法去做的時候,我的內心變得很純淨,絲毫不受外界各種瑣碎之事的影響。另外看過一本書裡面的一句話:當你安靜的時候,智慧就會升起來。所以,在很多的時候,我就會去靜靜的思考,思考多了,慢慢地就看開了一些事情,對時間、生命的本質也會有自己的理解。閒話扯遠了,今天我想要給大家帶來的是關於純程式碼實現的簡易的的走勢圖。下面是效果圖:

這裡寫圖片描述
1.為了減輕我們UI的壓力,我們只出了一套圖片是針對解析度為1280 *720,這樣為了解決螢幕的適配的問題,我們必須對不同的手機的螢幕的解析度對圖片進行處理。下面是對控制元件載入顯示的核心的類:

package com.poker175.activity;

import java.io.File;
import java.lang.ref.WeakReference;
import java.math.BigInteger;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import
java.util.WeakHashMap; import android.app.Activity; import android.app.Service; import android.content.Context; import android.content.pm.ApplicationInfo; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.graphics.Bitmap; import android.graphics.Typeface; import
android.os.Build.VERSION; import android.os.Bundle; import android.os.Environment; import android.os.Handler; import android.os.Message; import android.telephony.TelephonyManager; import android.text.TextUtils; import android.util.DisplayMetrics; import android.util.Log; import android.util.TypedValue; import android.view.Display; import android.view.View; import android.view.ViewGroup; import android.view.ViewGroup.LayoutParams; import android.view.WindowManager; import android.widget.FrameLayout; import android.widget.LinearLayout; import android.widget.RadioButton; import android.widget.RelativeLayout; import android.widget.TextView; import com.poker175.util.StringUtil; import static android.content.Context.WINDOW_SERVICE; public class BaseCommand { public static String verName; public static int verCode; public static String pkg; public static String imei; public static String imsi; public static int osver; public static String phone; public static String sim; public static String model; public static int sW; public static int sH; public static String duoapp_channelid; public static String sdkVercode; public static float radio = 1, radio1 = 1, radio_s = 1; public static float radioH; public static float radioW; public static int deltaX = 0; public static int deltaY = 0; public static float W = 1280.F; public static float H = 720.F; static WeakHashMap<String, WeakReference<Bitmap>> bmpCacheMap = new WeakHashMap<String, WeakReference<Bitmap>>(); /** * @param a */ public static void getDeviceInfo(Activity a) { if (TextUtils.isEmpty(verName)) { PackageManager pm = a.getPackageManager(); try { PackageInfo info = pm.getPackageInfo(a.getPackageName(), 0); pkg = info.applicationInfo.packageName; verName = info.versionName; verCode = info.versionCode; TelephonyManager tm = (TelephonyManager) a .getSystemService(Context.TELEPHONY_SERVICE); imei = tm.getDeviceId(); imsi = tm.getSubscriberId(); phone = tm.getLine1Number(); sim = tm.getSimSerialNumber(); osver = VERSION.SDK_INT; model = android.os.Build.MODEL; // @////@System.out.println("model:" + model); DisplayMetrics dm = new DisplayMetrics(); a.getWindowManager().getDefaultDisplay().getMetrics(dm); sW = dm.widthPixels; sH = dm.heightPixels; float scaleX = (float) sW / 1280;// 以1280*720為標準進行縮放 float scaleY = (float) sH / 720; radio = scaleX >= scaleY ? scaleY : scaleX; duoapp_channelid = DuoCommond.duoapp_channelid; sdkVercode = DuoCommond.sdkVercode; } catch (Exception ex) { ex.printStackTrace(); } } } /** * @param a */ public static void getDeviceInfo(Service a) { if (TextUtils.isEmpty(verName)) { PackageManager pm = a.getPackageManager(); try { PackageInfo info = pm.getPackageInfo(a.getPackageName(), 0); pkg = info.applicationInfo.packageName; verName = info.versionName; verCode = info.versionCode; TelephonyManager tm = (TelephonyManager) a .getSystemService(Context.TELEPHONY_SERVICE); imei = tm.getDeviceId(); imsi = tm.getSubscriberId(); phone = tm.getLine1Number(); sim = tm.getSimSerialNumber(); osver = VERSION.SDK_INT; model = android.os.Build.MODEL; WindowManager wm = (WindowManager) a.getSystemService(WINDOW_SERVICE); // 具體返回物件是WindowMangerIml類 Display display = wm.getDefaultDisplay(); DisplayMetrics dm = new DisplayMetrics(); display.getMetrics(dm); sW = dm.widthPixels; sH = dm.heightPixels; duoapp_channelid = DuoCommond.duoapp_channelid; sdkVercode = DuoCommond.sdkVercode; } catch (Exception ex) { ex.printStackTrace(); } } } /** * @param context * @param name * @return */ public static String getMeataStringData(Context context, String name) { try { ApplicationInfo ai = context.getPackageManager() .getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA); Bundle bundle = ai.metaData; return bundle.getString(name); } catch (Exception e) { } return ""; } /** * @param context * @param name * @return */ public static int getMeataIntData(Context context, String name) { try { ApplicationInfo ai = context.getPackageManager() .getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA); Bundle bundle = ai.metaData; return bundle.getInt(name, 0); } catch (Exception e) { } return 0; } /** * @param context * @param dir * @return */ public static String getCachePath(Context context, String dir) { String path = null; if (TextUtils.isEmpty(dir)) dir = context.getPackageName(); String tmp = Environment.getExternalStorageState(); if (Environment.MEDIA_MOUNTED.equals(tmp)) { // path = "/sdcard/" + dir + "/"; path = Environment.getExternalStorageDirectory().getPath() + "/" + dir + "/"; File file = new File(path); if (!file.exists()) { file.mkdir(); } file = null; // } else { path = context.getCacheDir().getAbsolutePath() + File.separator; } // @////@System.out.println("getCachePath:" + path); return path; } /** * @param url * @return */ public static String appendUrl(String url) { String newurl = url; newurl = StringUtil.appendNameValue(newurl, "duoid", duoapp_channelid); newurl = StringUtil.appendNameValue(newurl, "sdkVercode", sdkVercode); newurl = StringUtil.appendNameValue(newurl, "lang", StringUtil.getLocaleLanguage()); newurl = StringUtil.appendNameValue(newurl, "width", String.valueOf(sW)); newurl = StringUtil.appendNameValue(newurl, "height", String.valueOf(sH)); newurl = StringUtil.appendNameValue(newurl, "ver", verName); newurl = StringUtil.appendNameValue(newurl, "vercode", String.valueOf(verCode)); newurl = StringUtil.appendNameValue(newurl, "imei", imei); newurl = StringUtil.appendNameValue(newurl, "imsi", imsi); newurl = StringUtil.appendNameValue(newurl, "osver", String.valueOf(osver)); newurl = StringUtil.appendNameValue(newurl, "pkg", pkg); if (!TextUtils.isEmpty(model)) newurl = StringUtil.appendNameValue(newurl, "model", URLEncoder.encode(model)); newurl = StringUtil.appendNameValue(newurl, "phone", phone == null ? "" : URLEncoder.encode(phone)); newurl = StringUtil.appendNameValue(newurl, "sim", sim == null ? "" : URLEncoder.encode(sim)); return newurl; } /** * @param input * @return */ public static String getMd5Hash(String input) { try { MessageDigest md = MessageDigest.getInstance("MD5"); byte[] messageDigest = md.digest(input.getBytes()); BigInteger number = new BigInteger(1, messageDigest); String md5 = number.toString(16); // // while (md5.length() < 32) // md5 = "0" + md5; return md5; } catch (NoSuchAlgorithmException e) { // Log.e("MD5", e.getMessage()); return null; } } /** * @param handler */ public static void sendProcessingMessage(Handler handler) { Message msg = new Message(); Bundle b = new Bundle(); b.putInt(BaseContant.MESSAGE_DATA_RESULT, BaseContant.MESSAGE_PROCESSINGFLAG); msg.setData(b); handler.sendMessage(msg); } /** * @param handler */ public static void sendProcessedMessage(Handler handler, int pageid, String url, boolean isClear, boolean isRefresh, boolean isRemote) { Message msg = new Message(); Bundle b = new Bundle(); b.putInt(BaseContant.MESSAGE_DATA_RESULT, BaseContant.MESSAGE_PROCESSEDFLAG); b.putString(BaseContant.MESSAGE_URL_RESULT, url); b.putInt(BaseContant.MESSAGE_PAGEID_RESULT, pageid); b.putBoolean(BaseContant.MESSAGE_ISCLEAR_RESULT, isClear); b.putBoolean(BaseContant.MESSAGE_ISREFRESH_RESULT, isRefresh); b.putBoolean(BaseContant.MESSAGE_ISREMOTE_RESULT, isRemote); msg.setData(b); handler.sendMessage(msg); } /** * 清理早於指定的時間的問題 */ public static void clearSavedFile(String rootPath, int day) { File dir = new File(rootPath); String[] filenames = dir.list(); if (filenames == null) return; // //@////@System.out.println("clearSavedFile 111:"); long m1 = day * 86400000;// 24 * 60 * 60 * 1000; long m2 = System.currentTimeMillis(); long m = m2 - m1; // //@////@System.out.println("filenames.length:" + filenames.length); for (String filename : filenames) { // //@////@System.out.println("filename:" + filename); String[] tmp = filename.split("_"); if (tmp.length == 2 && TextUtils.isDigitsOnly(tmp[1])) { long m3 = Long.parseLong(tmp[1]); // Debug.d("m3:" + m3); if (m > m3) { // //@////@System.out.println("del:" + filename); File file = new File(rootPath + filename); // file.deleteOnExit(); file.delete(); file = null; } } else if (!filename.toLowerCase().endsWith(".apk") && !filename.toLowerCase().endsWith(".ap0")) { File file = new File(rootPath + filename); // file.deleteOnExit(); file.delete(); } } } /** * 刪除下載在手機中(非SDCARD)的安裝包 */ public static void clearDownedTempFile(Context context) { File file = context.getFilesDir(); for (String filename : file.list()) { if (filename.startsWith("sharetemp_")) { // //@////@System.out.println("clearDownedTempFile:" + // filename); context.deleteFile(filename); } } } /** * 重置控制元件寬高 * * @param view * @param w * @param h */ public static void setViewWH(View view, float w, float h) { LayoutParams para = view.getLayoutParams(); para.width = (int) (w * BaseCommond.radio);// 修改寬度 para.height = (int) (h * BaseCommond.radio);// 修改高度 view.setLayoutParams(para); // 設定修改後的佈局。 } /** * 適配控制元件相關寬高 * * @param container 父容器 * @param view 控制元件 * @param w 控制元件寬度 * @param h 控制元件高度 * @param leftMargin 以螢幕左上角為原點 的左邊距 * @param topMargin 以螢幕左上角為原點 的上邊距 * @param isRoot 是否為根容器 */ public static void setPositionAndWH(ViewGroup container, View view, int w, int h, int leftMargin, int topMargin, Boolean isRoot) { FrameLayout.LayoutParams layoutParams; layoutParams = new FrameLayout.LayoutParams((int) (w * radio_s), (int) (h * radio_s)); if (isRoot) { layoutParams.leftMargin = (int) (leftMargin * radio_s) + deltaX; layoutParams.topMargin = (int) (topMargin * radio_s) + deltaY; } else { layoutParams.leftMargin = (int) (leftMargin * radio_s); layoutParams.topMargin = (int) (topMargin * radio_s); } container.addView(view, layoutParams); } /** * 設定大廳的玩家的位置和大小 * * @param container * @param view * @param w * @param h * @param leftMargin * @param topMargin * @param isRoot */ public static void setNewHallPlayerInfoAndWH(ViewGroup container, View view, int w, int h, int leftMargin, int topMargin, Boolean isRoot) { FrameLayout.LayoutParams layoutParams; layoutParams = new FrameLayout.LayoutParams((int) (w * radio_s), (int) (h * radio_s)); if (isRoot) { layoutParams.leftMargin = (int) (leftMargin * radio_s) + deltaX; layoutParams.topMargin = (int) (topMargin * radio_s) + deltaY; } else { layoutParams.leftMargin = (int) (leftMargin * radio_s); if (radioH == radioW) { layoutParams.topMargin = (int) (topMargin * radio_s); } else { layoutParams.topMargin = (int) (topMargin * radio_s / 1.4); } } container.addView(view, layoutParams); } /** * 設定萬人場的底部的玩家的位置和大小 * * @param container * @param view * @param w * @param h * @param leftMargin * @param topMargin * @param isRoot */ public static void setWanrenBottomPlayerInfo(ViewGroup container, View view, int w, int h, int leftMargin, int topMargin, Boolean isRoot) { FrameLayout.LayoutParams layoutParams; layoutParams = new FrameLayout.LayoutParams((int) (w * radio_s), (int) (h * radio_s)); if (isRoot) { layoutParams.leftMargin = (int) (leftMargin * radio_s) + deltaX; layoutParams.topMargin = (int) (topMargin * radio_s) + deltaY; } else { layoutParams.leftMargin = (int) (leftMargin * radio_s); if (radioH == radioW) { layoutParams.topMargin = (int) (topMargin * radio_s); } else { layoutParams.topMargin = (int) (topMargin * radioH); } } container.addView(view, layoutParams); } /** * 設定今日活動的RadioButton的位置和大小 * * @param container * @param view * @param w * @param h * @param leftMargin * @param topMargin * @param isRoot */ public static void setRadioButtonPositionAndWH(ViewGroup container, View view, int w, int h, int leftMargin, int topMargin, Boolean isRoot) { FrameLayout.LayoutParams layoutParams; layoutParams = new FrameLayout.LayoutParams((int) (w * radio_s), (int) (h * radio_s)); if (isRoot) { layoutParams.leftMargin = (int) (leftMargin * radio_s) + deltaX; layoutParams.topMargin = (int) (topMargin * radio_s) + deltaY; } else { layoutParams.leftMargin = (int) (leftMargin * radio_s); if (radioH == radioW) { layoutParams.topMargin = (int) (topMargin * radio_s); } else { layoutParams.topMargin = topMargin; } } container.addView(view, layoutParams); } /** * 設定頂部的廣播和訊息的位置和大小 * * @param container * @param view * @param w * @param h * @param leftMargin * @param topMargin * @param isRoot */ public static void setTopBroadcastMessageAndWH(ViewGroup container, View view, int w, int h, int leftMargin, int topMargin, Boolean isRoot) { RelativeLayout.LayoutParams layoutParams; layoutParams = new RelativeLayout.LayoutParams((int) (w * radio_s), (int) (h * radio_s)); if (isRoot) { layoutParams.leftMargin = (int) (leftMargin * radio_s) + deltaX; } else { layoutParams.leftMargin = (int) (leftMargin * radio_s); } layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP); container.addView(view, layoutParams); } /** * 設定底部的選單的位置和大小 * * @param container * @param view * @param w * @param h * @param leftMargin * @param topMargin * @param isRoot */ public static void setBottomMenuPositionAndWH(ViewGroup container, View view, int w, int h, int leftMargin, int topMargin, Boolean isRoot) { RelativeLayout.LayoutParams layoutParams; layoutParams = new RelativeLayout.LayoutParams((int) (w * radio_s), (int) (h * radio_s)); if (isRoot) { layoutParams.leftMargin = (int) (leftMargin * radio_s) + deltaX; } else { layoutParams.leftMargin = (int) (leftMargin * radio_s); } layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); container.addView(view, layoutParams); } /** * 設定大廳訊息和客服訊息的位置和大小 * * @param container * @param view * @param w * @param h * @param leftMargin * @param topMargin * @param isRoot */ public static void setHallServiceMessageAndWH(ViewGroup container, View view, int w, int h, int leftMargin, int topMargin, Boolean isRoot) { FrameLayout.LayoutParams layoutParams; layoutParams = new FrameLayout.LayoutParams((int) (w * radio_s), (int) (h * radio_s)); if (isRoot) { layoutParams.leftMargin = (int) (leftMargin * radio_s) + deltaX; layoutParams.topMargin = (int) (topMargin * radio_s) + deltaY; } else { layoutParams.leftMargin = (int) (leftMargin * radio_s); if (radioH == radioW) { layoutParams.topMargin = (int) (topMargin * radio_s); } else { layoutParams.topMargin = (int) (topMargin * radioH); } } container.addView(view, layoutParams); } public static void reSetPositionAndWH(ViewGroup container, View view, View newView, int w, int h, int leftMargin, int topMargin, Boolean isRoot) { if (view != null) { container.removeView(view); } FrameLayout.LayoutParams layoutParams; layoutParams = new FrameLayout.LayoutParams((int) (w * radio_s), (int) (h * radio_s)); if (isRoot) { layoutParams.leftMargin = (int) (leftMargin * radio_s) + deltaX; layoutParams.topMargin = (int) (topMargin * radio_s) + deltaY; } else { layoutParams.leftMargin = (int) (leftMargin * radio_s); layoutParams.topMargin = (int) (topMargin * radio_s); } container.addView(newView, layoutParams); } /** * 適配控制元件相關寬高 * * @param container 父容器 * @param view 控制元件 * @param w 控制元件寬度 * @param h 控制元件高度 * @param leftMargin 以螢幕左上角為原點 的左邊距 * @param topMargin 以螢幕左上角為原點 的上邊距 * @param textSize 字型大小 * @param isRoot 是否為根容器 */ public static void setPositionAndWH(ViewGroup container, View view, int w, int h, int leftMargin, int topMargin, int textSize, Boolean isRoot) { FrameLayout.LayoutParams layoutParams; layoutParams = new FrameLayout.LayoutParams((int) (w * radio_s), (int) (h * radio_s)); if (isRoot) { layoutParams.leftMargin = (int) (leftMargin * radio_s) + deltaX; layoutParams.topMargin = (int) (topMargin * radio_s) + deltaY; } else { layoutParams.leftMargin = (int) (leftMargin * radio_s); layoutParams.topMargin = (int) (topMargin * radio_s); } if (textSize > 0) { if (view instanceof TextView) { ((TextView) view).setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize * radio_s); ((TextView) view).setTypeface(Typeface .defaultFromStyle(Typeface.BOLD)); } else if (view instanceof RadioButton) { ((RadioButton) view).setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize * radio_s); } } container.addView(view, layoutParams); } /** * 適配控制元件相關寬高 * * @param container 父容器 * @param view 控制元件 * @param w 控制元件寬度 * @param h 控制元件高度 * @param leftMargin 以螢幕左上角為原點 的左邊距 * @param topMargin 以螢幕左上角為原點 的上邊距 * @param textSize 字型大小 * @param isRoot 是否為根容器 */ public static void setMsgPositionAndWH(ViewGroupcontainer, View view, int w, int h,int leftMargin, int topMargin, int textSize, Boolean isRoot) { FrameLayout.LayoutParams layoutParams; layoutParams = new FrameLayout.LayoutParams((int) (w * radio_s), (int) (h * radio_s)); if (isRoot) { layoutParams.leftMargin = (int) (leftMargin * radio_s) + deltaX; layoutParams.topMargin = (int) (topMargin * radio_s) + deltaY; } else { if (radioH == radio) { layoutParams.leftMargin = (int) (leftMargin * radio_s); layoutParams.topMargin = (int) (topMargin * radio_s); } else { layoutParams.leftMargin = (int) (leftMargin * radio_s); layoutParams.topMargin = (int) (topMargin * radio_s) + 50; } } if (textSize > 0) { if (view instanceof TextView) { ((TextView) view).setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize * radio_s); ((TextView) view).setTypeface(Typeface .defaultFromStyle(Typeface.BOLD)); } else if (view instanceof RadioButton) { ((RadioButton) view).setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize * radio_s); } } container.addView(view, layoutParams); } /** * 設定首頁的大廳的玩家的資訊的位置和大小 * * @param container * @param view * @param w * @param h * @param leftMargin * @param topMargin * @param textSize * @param isRoot */ public static void setNewHallPlayerInfoAndWH(ViewGroup container, View view, int w, int h, int leftMargin, int topMargin, int textSize, Boolean isRoot) { FrameLayout.LayoutParams layoutParams; layoutParams = new FrameLayout.LayoutParams((int) (w * radio_s), (int) (h * radio_s)); if (isRoot) { layoutParams.leftMargin = (int) (leftMargin * radio_s) + deltaX; layoutParams.topMargin = (int) (topMargin * radio_s) + deltaY; } else { layoutParams.leftMargin = (int) (leftMargin * radio_s); if (radioW == radioH) { layoutParams.topMargin = (int) (topMargin * radio_s); } else { layoutParams.topMargin = (int) (topMargin * radio_s / 1.4); } } if (textSize > 0) { if (view instanceof TextView) { ((TextView) view).setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize * radio_s); ((TextView) view).setTypeface(Typeface .defaultFromStyle(Typeface.BOLD)); } else if (view instanceof RadioButton) { ((RadioButton) view).setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize * radio_s); } } container.addView(view, layoutParams); } /** * @param container * @param view * @param w * @param h * @param leftMargin * @param topMargin * @param textSize * @param isRoot */ public static void setWanrenBottomPlayerInfo(ViewGroup container, View view, int w, int h, int leftMargin, int topMargin, int textSize, Boolean isRoot) { FrameLayout.LayoutParams layoutParams; layoutParams = new FrameLayout.LayoutParams((int) (w * radio_s), (int) (h * radio_s)); if (isRoot) { layoutParams.leftMargin = (int) (leftMargin * radio_s) + deltaX; layoutParams.topMargin = (int) (topMargin * radio_s) + deltaY; } else { layoutParams.leftMargin = (int) (leftMargin * radio_s); if (radioH == radioW) { layoutParams.topMargin = (int) (topMargin * radio_s); } else { layoutParams.topMargin = (int) (topMargin * radioH); } } if (textSize > 0) { if (view instanceof TextView) { ((TextView) view).setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize * radio_s); ((TextView) view).setTypeface(Typeface .defaultFromStyle(Typeface.BOLD)); } else if (view instanceof RadioButton) { ((RadioButton) view).setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize * radio_s); } } container.addView(view, layoutParams); } /** * 設定購物車中心的RadioButton的位置和大小 * * @param container * @param view * @param w * @param h * @param leftMargin * @param textSize * @param isRoot */ public static void setShoppingCartRadioButtonAndWH(ViewGroup container, View view, int w, int h, int leftMargin, int textSize, Boolean isRoot) { FrameLayout.LayoutParams layoutParams; layoutParams = new FrameLayout.LayoutParams((int) (w * radio_s), (int) (h * radio_s)); if (isRoot) { layoutParams.leftMargin = (int) (leftMargin * radio_s) + deltaX; } else { layoutParams.leftMargin = (int) (leftMargin * radio_s); } if (textSize > 0) { if (view instanceof TextView) { ((TextView) view).setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize * radio_s); ((TextView) view).setTypeface(Typeface .defaultFromStyle(Typeface.BOLD)); } else if (view instanceof RadioButton) { ((RadioButton) view).setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize * radio_s); } } container.addView(view, layoutParams); } /** * 設定RadioButton的大小和位置 * * @param container * @param view * @param w * @param h * @param leftMargin * @param topMargin * @param textSize * @param isRoot */ public static void setRadioButtonPositionAndWH(ViewGroup container, View view, int w, int h, int leftMargin, int topMargin, int textSize, Boolean isRoot) { FrameLayout.LayoutParams layoutParams; layoutParams = new FrameLayout.LayoutParams((int) (w * radio_s), (int) (h * radio_s)); if (isRoot) { layoutParams.leftMargin = (int) (leftMargin * radio_s) + deltaX; layoutParams.topMargin = (int) (topMargin * radio_s); } else { layoutParams.leftMargin = (int) (leftMargin * radio_s); layoutParams.topMargin = (int) (topMargin * radio_s); } if (textSize > 0) { if (view instanceof TextView) { ((TextView) view).setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize * radio_s); ((TextView) view).setTypeface(Typeface .defaultFromStyle(Typeface.BOLD)); } else if (view instanceof RadioButton) { ((RadioButton) view).setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize * radio_s); } } container.addView(view, layoutParams); } /** * 初始化螢幕適配的相關引數 從大廳開始 */ public static void initScreenParam(Activity act) { DisplayMetrics dm = new DisplayMetrics(); act.getWindowManager().getDefaultDisplay().getMetrics(dm); sW = dm.widthPixels; sH = dm.heightPixels; Log.d("Base", "sW" + sW + "sH" + sH); radioH = sH / H; radioW = sW / W; if (radioH > radioW) { deltaY = (int) ((sH - radioW * H) / 2); radio_s = radioW; } else { deltaX = (int) ((sW - radioH * W) / 2); radio_s = radioH; } } /** * 判斷系統版本是否高於3.0 * * @return */ public static boolean canPerformPropertyAnim() { try { int API_LEVEL = VERSION.SDK_INT; Log.i("ACT00", API_LEVEL + ""); if (API_LEVEL >= 11) { return true; } return false; } catch (Exception e) { return false; } } public static void setFloatPositionAndWH(ViewGroup container, View view, int w, int h, int leftMargin, float topMargin, Boolean isRight) { FrameLayout.LayoutParams layoutParams; layoutParams = new FrameLayout.LayoutParams( (int) (w * BaseCommond.radio_s), (int) (h * BaseCommond.radio_s)); if (isRight) { layoutParams.leftMargin = (int) (leftMargin * BaseCommond.radio_s) + BaseCommond.deltaX * 2; layoutParams.topMargin = (int) (topMargin * BaseCommond.radio_s) + BaseCommond.deltaY; } else { layoutParams.leftMargin = (int) (leftMargin * BaseCommond.radio_s) + BaseCommond.deltaX; layoutParams.topMargin = (int) (topMargin * BaseCommond.radio_s) + BaseCommond.deltaY * 2; } container.addView(view, layoutParams); } /** * * @param container * @param view * @param w * @param h * @param isRight */ public static void setViewPagerFloatPositionAndWH(ViewGroup container, View view, int w, int h, boolean isRight) { LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams( (int) (w * BaseCommond.radio_s), (int) (h * BaseCommond.radio_s)); if (!isRight) { layoutParams.rightMargin = (int) (20 * BaseCommond.radio_s); } container.addView(view, layoutParams); } /** * * @param container * @param view * @param w * @param h */ public static void setLeftViewPagerFloatPositionAndWH(ViewGroup container, View view, int w, int h) { LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams( (int) (w * BaseCommond.radio_s), (int) (h * BaseCommond.radio_s)); layoutParams.rightMargin = (int) (20 * BaseCommond.radio_s); layoutParams.leftMargin = (int) (20 * BaseCommond.radio_s); container.addView(view, layoutParams); } public static void setStorePositionAndWH(ViewGroup container, View view, int w, int h, int left, int top, int textSize) { FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams( (int) (w * BaseCommond.radio_s), (int) (h * BaseCommond.radio_s)); layoutParams.leftMargin = (int) (left * BaseCommond.radio_s); layoutParams.topMargin = (int) (top * BaseCommond.radio_s); if (textSize > 0) { if (view instanceof TextView) { ((TextView) view).setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize * radio_s); ((TextView) view).setTypeface(Typeface.defaultFromStyle(Typeface.BOLD)); } } container.addView(view, layoutParams); } }

下面我對上面的類的核心的方法進行解讀:

/**
     * 初始化螢幕適配的相關引數  從大廳開始
     */
    public static void initScreenParam(Activity act) {
        DisplayMetrics dm = new DisplayMetrics();
        act.getWindowManager().getDefaultDisplay()
                              .getMetrics(dm);
        sW = dm.widthPixels;
        sH = dm.heightPixels;
        Log.d("Base", "sW" + sW + "sH" + sH);
        radioH = sH / H;
        radioW = sW / W;
        if (radioH > radioW) {
            deltaY = (int) ((sH - radioW * H) / 2);
            radio_s = radioW;
        } else {
            deltaX = (int) ((sW - radioH * W) /