編輯:關於 Android 程式設計
在開發專案時,解析度適配往往是個令人頭痛的老大難問題。本文分享一套以「基準解析度 + 比例縮放」為核心的自動適配方案。這個方法未必萬能,但起碼解決了解析度與螢幕密度的關聯問題。需要注意的是,由於圖片資源偏小可能導致布局失真,因此也需要美工人員的充分配合。
這個類用來存放每個 View 的寬、高、邊距以及父布局類型等資訊:
package com.zte.layout.adapter;
import android.view.View;
/**
* 存儲 View 資訊的 JavaBean 類
*/
public class LayoutInformation
{
/** View 的對象 */
private View view;
/** View 的寬度 */
private double viewWidth;
/** View 的高度 */
private double viewHeight;
/** View 距左邊的距離,即 marginLeft */
private double viewMarginLeft;
/** View 距頂部的距離,即 MarginTop */
private double viewMarginTop;
/** 父類布局的類型為相對布局 */
public static int R=-1;
/** 父類布局的類型為線性布局 */
public static int L=-2;
/** 此 View 的父類布局的類型 */
private int parentLayoutType;
public LayoutInformation(View view, double viewWidth, double viewHeight,
double viewMarginLeft, double viewMarginTop, int parentLayoutType)
{
this.view = view;
this.viewWidth = viewWidth;
this.viewHeight = viewHeight;
this.viewMarginLeft = viewMarginLeft;
this.viewMarginTop = viewMarginTop;
this.parentLayoutType=parentLayoutType;
}
public View getView() { return view; }
public void setView(View view) { this.view = view; }
public double getViewWidth() { return viewWidth; }
public void setViewWidth(double viewWidth) { this.viewWidth = viewWidth; }
public double getViewHeight() { return viewHeight; }
public void setViewHeight(double viewHeight) { this.viewHeight = viewHeight; }
public double getViewMarginLeft() { return viewMarginLeft; }
public void setViewMarginLeft(double viewMarginLeft) { this.viewMarginLeft = viewMarginLeft; }
public double getViewMarginTop() { return viewMarginTop; }
public void setViewMarginTop(double viewMarginTop) { this.viewMarginTop = viewMarginTop; }
public int getParentLayoutType() { return parentLayoutType; }
public void setParentLayoutType(int parentLayoutType) { this.parentLayoutType = parentLayoutType; }
}
MyLayoutAdapter 類負責讀取當前螢幕解析度與密度,並按比例縮放各 View 的尺寸與邊距:
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
/**
* 分辨率通配類
*/
public class MyLayoutAdapter
{
/** 基准分辨率的寬 */
public double STANDARD_SCREEN_WIDTH;
/** 基准分辨率的高 */
public double STANDARD_SCREEN_HEIGHT;
/** 系統當前的分辨率的寬 */
public double CURRENT_SCREEN_WIDTH;
/** 系統當前的分辨率的高 */
public double CURRENT_SCREEN_HEIGHT;
/** 基准屏幕密度 */
public static final double STANDARD_DENSITY = 160;
/** 當前屏幕密度 */
private double CURRENT_DENSITY;
/** 屏幕密度比例 */
private double DENSITY_RATIO;
/** 屏幕寬度比例 */
private double WIDTH_RATIO;
/** 屏幕高度比例 */
private double HEIGHT_RATIO;
private double viewStandardWidth;
private double viewStandardHeight;
private double viewStandardMarginLeft;
private double viewStandardMarginTop;
private double viewCurrentWidth;
private double viewCurrentHeight;
private double viewCurrentMarginLeft;
private double viewCurrentMarginTop;
private View view;
private int parentLayoutType;
private final int LAYOUT_TYPE_RELATiVELAYOUT = LayoutInformation.R;
private final int LAYOUT_TYPE_LINEARLAYOUT = LayoutInformation.L;
private final int LAYOUTPARAMS_WARP_CONTENT = LayoutParams.WRAP_CONTENT;
private final int LAYOUTPARAMS_FILL_PARENT = LayoutParams.FILL_PARENT;
private Context context;
/**
* 類對象實例化時,設置基准屏幕寬度、高度
*/
public MyLayoutAdapter(Context context, double standardWidth,
double standardHeight)
{
this.context = context;
getScreenSize();
STANDARD_SCREEN_HEIGHT = standardHeight;
STANDARD_SCREEN_WIDTH = standardWidth;
// 計算寬高比率
WIDTH_RATIO = CURRENT_SCREEN_WIDTH / STANDARD_SCREEN_WIDTH;
HEIGHT_RATIO = CURRENT_SCREEN_HEIGHT / STANDARD_SCREEN_HEIGHT;
}
/** 獲取當前屏幕大小和密度 */
private void getScreenSize()
{
DisplayMetrics displayMetrics = new DisplayMetrics();
((Activity) context).getWindowManager().getDefaultDisplay()
getMetrics(displayMetrics);
CURRENT_SCREEN_WIDTH = displayMetrics.widthPixels;
CURRENT_SCREEN_HEIGHT = displayMetrics.heightPixels;
CURRENT_DENSITY = displayMetrics.densityDpi;
DENSITY_RATIO = STANDARD_DENSITY / CURRENT_DENSITY;
}
/** 進行通配 */
public void setViewLayout(List<LayoutInformation> listdata)
{
for (int i = 0; i < listdata.size(); i++)
{
view = listdata.get(i).getView();
viewStandardWidth = listdata.get(i).getViewWidth();
viewStandardHeight = listdata.get(i).getViewHeight();
viewStandardMarginLeft = listdata.get(i).getViewMarginLeft();
viewStandardMarginTop = listdata.get(i).getViewMarginTop();
setLayoutParams();
viewCurrentMarginLeft = viewStandardMarginLeft * WIDTH_RATIO;
viewCurrentMarginTop = viewStandardMarginTop * HEIGHT_RATIO;
parentLayoutType = listdata.get(i).getParentLayoutType();
setLayoutByParentLayoutType();
}
}
/** 判斷布局屬性的值,設置布局的屬性 */
private void setLayoutParams()
{
// 如果基准的寬是 wrap_content 或 fill_parent 則使用原值,否則進行計算
if (viewStandardWidth == LAYOUTPARAMS_WARP_CONTENT
|| viewStandardWidth == LAYOUTPARAMS_FILL_PARENT)
{
viewCurrentWidth = viewStandardWidth;
} else
{
viewCurrentWidth = viewStandardWidth * WIDTH_RATIO;
}
if (viewStandardHeight == LAYOUTPARAMS_WARP_CONTENT
|| viewStandardHeight == LAYOUTPARAMS_FILL_PARENT)
{
viewCurrentHeight = viewStandardHeight;
} else
{
viewCurrentHeight = viewStandardHeight * HEIGHT_RATIO;
}
}
/** 通過判斷此 View 父類的布局類型,給此 View 設置布局 */
private void setLayoutByParentLayoutType()
{
if (parentLayoutType == LAYOUT_TYPE_RELATiVELAYOUT)
{
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
(int) viewCurrentWidth, (int) viewCurrentHeight);
params.setMargins((int) viewCurrentMarginLeft,
(int) viewCurrentMarginTop, 0, 0);
view.setLayoutParams(params);
} else if (parentLayoutType == LAYOUT_TYPE_LINEARLAYOUT)
{
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
(int) viewCurrentWidth, (int) viewCurrentHeight);
params.setMargins((int) viewCurrentMarginLeft,
(int) viewCurrentMarginTop, 0, 0);
view.setLayoutParams(params);
}
}
/** 設置字體大小 */
public int setTextSize(int standardSize)
{
int currentSize;
currentSize = (int) (standardSize * WIDTH_RATIO * DENSITY_RATIO);
return currentSize;
}
}
public interface InitAllView{
/** 初始化控件的大小 */
public void initAllView();
}
在 Activity 中,以基準解析度(例如 320x480)建立 MyLayoutAdapter,將需要適配的 View 資訊加入列表,再統一進行縮放:
/** 通配方法 */
private void initWildcard() {
myLayout = new MyLayoutAdapter(this, 320, 480);
listInfo = new ArrayList<LayoutInformation>();
listInfo.add(new LayoutInformation(mBtn1, LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, 0, 0, LayoutInformation.R));
listInfo.add(new LayoutInformation(mNowRegisterBtn, 80, 27.3, 14.7, 0,
LayoutInformation.R));
listInfo.add(new LayoutInformation(mNextRegisterBtn, 80, 27.3, 14.7, 0,
LayoutInformation.R));
mBtn1.setTextSize(myLayout.setTextSize(12));
mNowRegisterBtn.setTextSize(myLayout.setTextSize(12));
mNextRegisterBtn.setTextSize(myLayout.setTextSize(12));
myLayout.setViewLayout(listInfo);
}
這套方案的核心思路是:以一個固定基準解析度(如 320x480)設計介面,執行時再根據實際螢幕的寬高比與密度比,等比例縮放每個 View 的尺寸、邊距與字型大小,從而讓同一套布局適配不同解析度的裝置。
Launcher3 壁紙機制解析:從選擇、裁切到動態壁紙
壁紙是系統能力 Launcher 的壁紙設定並非 Launcher 獨有,而是透過系統 WallpaperManager 完成。因此,不只是 Launcher,許多第三方
深入 Android 執行緒模型:一個進程為什麼只有一條執行緒在跑
預設情況下,一個進程只有一條執行緒 這點跟 C/C++、Java 等語言的預設模型一致:在 Android 應用程式裡,一個進程只有一條執行緒,所有組件都跑在它上面。 應
Android 圖片選取實戰:調起系統相冊並取得合適尺寸的 Bitmap
做問題反饋頁面時,需要讓用戶上傳截圖。最初的念頭是把設備裡所有圖片都掃描出來自己列清單,寫到一半才反應過來——系統相冊本來就有現成的 API 可以調起,何必自己造輪子。
Android View 的繪製機制:Canvas 與 Paint 怎麼用
前面談過了 Android 如何測量,但只知道一個控制項長到哪裡、有多寬多高,當然還不夠。只要簡單覆寫 onDraw,再拿 Canvas(畫布)配上一支 Paint(畫筆