android 分辨率适配的方法

android 分辨率适配的方法

首先说明一点:这个方法不能说万能的,可是最起码它解决了分辨率跟密集度的关系,就是全部分辨率,只要传了第一次的参数,后面都不须要改动了,可是也引来一个问题,就是布局会由于图片资源小而失真,因此这也须要美工的同志多多配合的,废话不说,贴代码: html

第一步,先建立一个view信息的javabean类: java

复制代码

package com.zte.layout.adapter; import android.view.View; /** * 存储View信息的JavaBean类 * * @author * */ 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; /** * 存储View信息的JavaBean类 * * @param view * View的对象 * @param viewWidth * View的宽 * @param viewHeight * View的高 * @param viewMarginLeft * View距左边的距离 * @param viewMargdoubleop * View距上部的距离 * @param parentLayoutType * 父类布局的类型,LayoutInformation.R * (表示相对布局)或者LayoutInformation.L(表示线性布局) */ 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; } /** * 获取View的对象 * * @return View对象 */ public View getView() { return view; } /** * 设置View的对象 */ public void setView(View view) { this.view = view; } /** * 获取View的宽度 * * @return View的宽度,double型 */ public double getViewWidth() { return viewWidth; } /** * 设置View的宽度,double型 * * @param viewWidth */ public void setViewWidth(double viewWidth) { this.viewWidth = viewWidth; } /** * 获取View的高度 * * @return View的高度,double型 */ public double getViewHeight() { return viewHeight; } /** * 设置View的高度,double型 * * @param viewHeight */ public void setViewHeight(double viewHeight) { this.viewHeight = viewHeight; } /** * 获取View距离左边的距离 * * @return View距离左边的距离,double型 */ public double getViewMarginLeft() { return viewMarginLeft; } /** * 设置View距离左边的距离,double型 * * @param viewMarginLeft */ public void setViewMarginLeft(double viewMarginLeft) { this.viewMarginLeft = viewMarginLeft; } /** * 获取View距离上部的距离 * * @return View距离上部的距离,double型 */ public double getViewMarginTop() { return viewMarginTop; } /** * 设置View距离上部的距离,double型 * * @param viewMargdoubleop */ public void setViewMarginTop(double viewMarginTop) { this.viewMarginTop = viewMarginTop; } /** * 获取父类布局的类型 * @return parentLayoutType,int型 */ public int getParentLayoutType() { return parentLayoutType; } /** * 设置父类布局的类型 * @param parentLayoutType */ public void setParentLayoutType(int parentLayoutType) { this.parentLayoutType = parentLayoutType; } }

复制代码

第二步:建立一个计算方法: android

复制代码

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; /** * 分配率通配类 * * @author * */ 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; /** * UI组件的对象 */ private View view; /** * 此View的父类布局的类型 */ private int parentLayoutType; /** * 父类布局的类型为相对布局 */ private final int LAYOUT_TYPE_RELATiVELAYOUT = LayoutInformation.R; /** * 父类布局的类型为线性布局 */ private final int LAYOUT_TYPE_LINEARLAYOUT = LayoutInformation.L; /** * 布局属性为wrap_content */ private final int LAYOUTPARAMS_WARP_CONTENT = LayoutParams.WRAP_CONTENT; /** * 布局属性为fill_parent */ private final int LAYOUTPARAMS_FILL_PARENT = LayoutParams.FILL_PARENT; private Context context; /** * 类对象实例化时,设置 基准屏幕宽度,高度 * * @param context * Context * @param standardWidth * 基准屏幕的宽 * @param standardHeight * 基准屏幕的高 */ 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; } /** * 进行通配 * * @param listdata */ 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; } // 若是基准的宽是wrap_content或者fill_parent则使用原值,不然进行计算获得通配后的值 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); } } /** * 设置字体大小 * * @param standardSize * 原始大小 * @return int */ public int setTextSize(int standardSize) { int currentSize; currentSize = (int) (standardSize * WIDTH_RATIO * DENSITY_RATIO); return currentSize; } }

复制代码

第三步,写一个接口: app

复制代码

public interface InitAllView{ /** * 初始化控件的大小 */ public void initAllView(); }

复制代码

第四步:代码控制: 布局

复制代码

/** * 通配方法 */ 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)); // listInfo.add(new LayoutInformation(mCheckBtn, 17.3,17.3, 14.7, 0, // LayoutInformation.L)); mBtn1.setTextSize(myLayout.setTextSize(12)); mNowRegisterBtn.setTextSize(myLayout.setTextSize(12)); mNextRegisterBtn.setTextSize(myLayout.setTextSize(12)); myLayout.setViewLayout(listInfo); }

复制代码