读书人

分辨率适配的步骤

发布时间: 2012-11-23 00:03:43 作者: rapoo

分辨率适配的方法

它解决了分辨率跟密集度的关系,但是也引来一个问题,就是布局会因为图片资源小而失真,所以这也需要美工的同志多多配合的,贴代码:

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



  1. package com.zte.layout.adapter;

  2. import android.view.View;

  3. /**
  4. * 存储View信息的JavaBean类
  5. *
  6. * @author
  7. *
  8. */
  9. public class LayoutInformation
  10. {
  11. /**
  12. * View的对象
  13. */
  14. private View view;

  15. /**
  16. * View的宽度
  17. */
  18. private double viewWidth;

  19. /**
  20. * View的高度
  21. */
  22. private double viewHeight;

  23. /**
  24. * View距左边的距离,即marginLeft
  25. */
  26. private double viewMarginLeft;

  27. /**
  28. * View距顶部的距离,即MarginTop;
  29. */
  30. private double viewMarginTop;

  31. /**
  32. * 父类布局的类型为相对布局
  33. */
  34. public static int R=-1;

  35. /**
  36. * 父类布局的类型为线性布局
  37. */
  38. public static int L=-2;

  39. /**
  40. * 此View的父类布局的类型
  41. */
  42. private int parentLayoutType;

  43. /**
  44. * 存储View信息的JavaBean类
  45. *
  46. * @param view
  47. * View的对象
  48. * @param viewWidth
  49. * View的宽
  50. * @param viewHeight
  51. * View的高
  52. * @param viewMarginLeft
  53. * View距左边的距离
  54. * @param viewMargdoubleop
  55. * View距上部的距离
  56. * @param parentLayoutType
  57. * 父类布局的类型,LayoutInformation.R
  58. * (表示相对布局)或者LayoutInformation.L(表示线性布局)
  59. */
  60. public LayoutInformation(View view, double viewWidth, double viewHeight,
  61. double viewMarginLeft, double viewMarginTop, int parentLayoutType)
  62. {

  63. this.view = view;
  64. this.viewWidth = viewWidth;
  65. this.viewHeight = viewHeight;
  66. this.viewMarginLeft = viewMarginLeft;
  67. this.viewMarginTop = viewMarginTop;
  68. this.parentLayoutType=parentLayoutType;
  69. }

  70. /**
  71. * 获取View的对象
  72. *
  73. * [url=home.php?mod=space&uid=7300]@return[/url] View对象
  74. */
  75. public View getView()
  76. {
  77. return view;
  78. }

  79. /**
  80. * 设置View的对象
  81. */
  82. public void setView(View view)
  83. {
  84. this.view = view;
  85. }

  86. /**
  87. * 获取View的宽度
  88. *
  89. * @return View的宽度,double型
  90. */
  91. public double getViewWidth()
  92. {
  93. return viewWidth;
  94. }

  95. /**
  96. * 设置View的宽度,double型
  97. *
  98. * @param viewWidth
  99. */
  100. public void setViewWidth(double viewWidth)
  101. {
  102. this.viewWidth = viewWidth;
  103. }

  104. /**
  105. * 获取View的高度
  106. *
  107. * @return View的高度,double型
  108. */
  109. public double getViewHeight()
  110. {
  111. return viewHeight;
  112. }

  113. /**
  114. * 设置View的高度,double型
  115. *
  116. * @param viewHeight
  117. */
  118. public void setViewHeight(double viewHeight)
  119. {
  120. this.viewHeight = viewHeight;
  121. }

  122. /**
  123. * 获取View距离左边的距离
  124. *
  125. * @return View距离左边的距离,double型
  126. */
  127. public double getViewMarginLeft()
  128. {
  129. return viewMarginLeft;
  130. }

  131. /**
  132. * 设置View距离左边的距离,double型
  133. *
  134. * @param viewMarginLeft
  135. */
  136. public void setViewMarginLeft(double viewMarginLeft)
  137. {
  138. this.viewMarginLeft = viewMarginLeft;
  139. }

  140. /**
  141. * 获取View距离上部的距离
  142. *
  143. * @return View距离上部的距离,double型
  144. */
  145. public double getViewMarginTop()
  146. {
  147. return viewMarginTop;
  148. }

  149. /**
  150. * 设置View距离上部的距离,double型
  151. *
  152. * @param viewMargdoubleop
  153. */
  154. public void setViewMarginTop(double viewMarginTop)
  155. {
  156. this.viewMarginTop = viewMarginTop;
  157. }

  158. /**
  159. * 获取父类布局的类型
  160. * @return parentLayoutType,int型
  161. */
  162. public int getParentLayoutType()
  163. {
  164. return parentLayoutType;
  165. }

  166. /**
  167. * 设置父类布局的类型
  168. * @param parentLayoutType
  169. */
  170. public void setParentLayoutType(int parentLayoutType)
  171. {
  172. this.parentLayoutType = parentLayoutType;
  173. }

  174. }
  1. package com.zte.layout.adapter;

  2. import android.view.View;

  3. /**
  4. * 存储View信息的JavaBean类
  5. *
  6. * @author
  7. *
  8. */
  9. public class LayoutInformation
  10. {
  11. /**
  12. * View的对象
  13. */
  14. private View view;

  15. /**
  16. * View的宽度
  17. */
  18. private double viewWidth;

  19. /**
  20. * View的高度
  21. */
  22. private double viewHeight;

  23. /**
  24. * View距左边的距离,即marginLeft
  25. */
  26. private double viewMarginLeft;

  27. /**
  28. * View距顶部的距离,即MarginTop;
  29. */
  30. private double viewMarginTop;

  31. /**
  32. * 父类布局的类型为相对布局
  33. */
  34. public static int R=-1;

  35. /**
  36. * 父类布局的类型为线性布局
  37. */
  38. public static int L=-2;

  39. /**
  40. * 此View的父类布局的类型
  41. */
  42. private int parentLayoutType;

  43. /**
  44. * 存储View信息的JavaBean类
  45. *
  46. * @param view
  47. * View的对象
  48. * @param viewWidth
  49. * View的宽
  50. * @param viewHeight
  51. * View的高
  52. * @param viewMarginLeft
  53. * View距左边的距离
  54. * @param viewMargdoubleop
  55. * View距上部的距离
  56. * @param parentLayoutType
  57. * 父类布局的类型,LayoutInformation.R
  58. * (表示相对布局)或者LayoutInformation.L(表示线性布局)
  59. */
  60. public LayoutInformation(View view, double viewWidth, double viewHeight,
  61. double viewMarginLeft, double viewMarginTop, int parentLayoutType)
  62. {

  63. this.view = view;
  64. this.viewWidth = viewWidth;
  65. this.viewHeight = viewHeight;
  66. this.viewMarginLeft = viewMarginLeft;
  67. this.viewMarginTop = viewMarginTop;
  68. this.parentLayoutType=parentLayoutType;
  69. }

  70. /**
  71. * 获取View的对象
  72. *
  73. * [url=home.php?mod=space&uid=7300]@return[/url] View对象
  74. */
  75. public View getView()
  76. {
  77. return view;
  78. }

  79. /**
  80. * 设置View的对象
  81. */
  82. public void setView(View view)
  83. {
  84. this.view = view;
  85. }

  86. /**
  87. * 获取View的宽度
  88. *
  89. * @return View的宽度,double型
  90. */
  91. public double getViewWidth()
  92. {
  93. return viewWidth;
  94. }

  95. /**
  96. * 设置View的宽度,double型
  97. *
  98. * @param viewWidth
  99. */
  100. public void setViewWidth(double viewWidth)
  101. {
  102. this.viewWidth = viewWidth;
  103. }

  104. /**
  105. * 获取View的高度
  106. *
  107. * @return View的高度,double型
  108. */
  109. public double getViewHeight()
  110. {
  111. return viewHeight;
  112. }

  113. /**
  114. * 设置View的高度,double型
  115. *
  116. * @param viewHeight
  117. */
  118. public void setViewHeight(double viewHeight)
  119. {
  120. this.viewHeight = viewHeight;
  121. }

  122. /**
  123. * 获取View距离左边的距离
  124. *
  125. * @return View距离左边的距离,double型
  126. */
  127. public double getViewMarginLeft()
  128. {
  129. return viewMarginLeft;
  130. }

  131. /**
  132. * 设置View距离左边的距离,double型
  133. *
  134. * @param viewMarginLeft
  135. */
  136. public void setViewMarginLeft(double viewMarginLeft)
  137. {
  138. this.viewMarginLeft = viewMarginLeft;
  139. }

  140. /**
  141. * 获取View距离上部的距离
  142. *
  143. * @return View距离上部的距离,double型
  144. */
  145. public double getViewMarginTop()
  146. {
  147. return viewMarginTop;
  148. }

  149. /**
  150. * 设置View距离上部的距离,double型
  151. *
  152. * @param viewMargdoubleop
  153. */
  154. public void setViewMarginTop(double viewMarginTop)
  155. {
  156. this.viewMarginTop = viewMarginTop;
  157. }

  158. /**
  159. * 获取父类布局的类型
  160. * @return parentLayoutType,int型
  161. */
  162. public int getParentLayoutType()
  163. {
  164. return parentLayoutType;
  165. }

  166. /**
  167. * 设置父类布局的类型
  168. * @param parentLayoutType
  169. */
  170. public void setParentLayoutType(int parentLayoutType)
  171. {
  172. this.parentLayoutType = parentLayoutType;
  173. }

  174. }

第二步:创建一个计算方法:

  1. import java.util.List;

  2. import android.app.Activity;
  3. import android.content.Context;
  4. import android.util.DisplayMetrics;
  5. import android.view.View;
  6. import android.view.ViewGroup.LayoutParams;
  7. import android.widget.LinearLayout;
  8. import android.widget.RelativeLayout;

  9. /**
  10. * 分配率通配类
  11. *
  12. * @author
  13. *
  14. */
  15. public class MyLayoutAdapter
  16. {
  17. /**
  18. * 基准分辨率的宽
  19. */
  20. public double STANDARD_SCREEN_WIDTH;

  21. /**
  22. * 基准分辨率的高
  23. */
  24. public double STANDARD_SCREEN_HEIGHT;

  25. /**
  26. * 系统当前的分辨率的宽
  27. */
  28. public double CURRENT_SCREEN_WIDTH;

  29. /**
  30. * 系统当前的分辨率的高
  31. */
  32. public double CURRENT_SCREEN_HEIGHT;

  33. /**
  34. * 基准屏幕密度
  35. */
  36. public static final double STANDARD_DENSITY = 160;

  37. /**
  38. * 当前屏幕密度
  39. */
  40. private double CURRENT_DENSITY;

  41. /**
  42. * 屏幕密度比例
  43. */
  44. private double DENSITY_RATIO;

  45. /**
  46. * 屏幕宽度比例
  47. */
  48. private double WIDTH_RATIO;

  49. /**
  50. * 屏幕高度比例
  51. */
  52. private double HEIGHT_RATIO;

  53. /**
  54. * 组件基准的宽度
  55. */
  56. private double viewStandardWidth;

  57. /**
  58. * 组件基准的高度
  59. */
  60. private double viewStandardHeight;

  61. /**
  62. * 组件基准的距离左边的距离
  63. */
  64. private double viewStandardMarginLeft;

  65. /**
  66. * 组件基准的距离顶部的距离
  67. */
  68. private double viewStandardMarginTop;

  69. /**
  70. * 组件当前的宽
  71. */
  72. private double viewCurrentWidth;

  73. /**
  74. * 组件当前的高
  75. */
  76. private double viewCurrentHeight;

  77. /**
  78. * 组件当前距离左边的距离
  79. */
  80. private double viewCurrentMarginLeft;

  81. /**
  82. * 组件当前距离顶部的距离
  83. */
  84. private double viewCurrentMarginTop;

  85. /**
  86. * UI组件的对象
  87. */
  88. private View view;

  89. /**
  90. * 此View的父类布局的类型
  91. */
  92. private int parentLayoutType;

  93. /**
  94. * 父类布局的类型为相对布局
  95. */
  96. private final int LAYOUT_TYPE_RELATiVELAYOUT = LayoutInformation.R;

  97. /**
  98. * 父类布局的类型为线性布局
  99. */
  100. private final int LAYOUT_TYPE_LINEARLAYOUT = LayoutInformation.L;

  101. /**
  102. * 布局属性为wrap_content
  103. */
  104. private final int LAYOUTPARAMS_WARP_CONTENT = LayoutParams.WRAP_CONTENT;

  105. /**
  106. * 布局属性为fill_parent
  107. */
  108. private final int LAYOUTPARAMS_FILL_PARENT = LayoutParams.FILL_PARENT;

  109. private Context context;

  110. /**
  111. * 类对象实例化时,设置 基准屏幕宽度,高度
  112. *
  113. * @param context
  114. * Context
  115. * @param standardWidth
  116. * 基准屏幕的宽
  117. * @param standardHeight
  118. * 基准屏幕的高
  119. */
  120. public MyLayoutAdapter(Context context, double standardWidth,
  121. double standardHeight)
  122. {
  123. this.context = context;
  124. getScreenSize();
  125. STANDARD_SCREEN_HEIGHT = standardHeight;
  126. STANDARD_SCREEN_WIDTH = standardWidth;
  127. // 计算宽高比率
  128. WIDTH_RATIO = CURRENT_SCREEN_WIDTH / STANDARD_SCREEN_WIDTH;
  129. HEIGHT_RATIO = CURRENT_SCREEN_HEIGHT / STANDARD_SCREEN_HEIGHT;
  130. }

  131. /**
  132. * 获取当前屏幕大小和密度
  133. */
  134. private void getScreenSize()
  135. {
  136. DisplayMetrics displayMetrics = new DisplayMetrics();
  137. ((Activity) context).getWindowManager().getDefaultDisplay()
  138. .getMetrics(displayMetrics);
  139. CURRENT_SCREEN_WIDTH = displayMetrics.widthPixels;
  140. CURRENT_SCREEN_HEIGHT = displayMetrics.heightPixels;
  141. CURRENT_DENSITY = displayMetrics.densityDpi;
  142. DENSITY_RATIO = STANDARD_DENSITY / CURRENT_DENSITY;
  143. }

  144. /**
  145. * 进行通配
  146. *
  147. * @param listdata
  148. */
  149. public void setViewLayout(List<LayoutInformation> listdata)
  150. {

  151. for (int i = 0; i < listdata.size(); i++)
  152. {

  153. view = listdata.get(i).getView();

  154. viewStandardWidth = listdata.get(i).getViewWidth();

  155. viewStandardHeight = listdata.get(i).getViewHeight();

  156. viewStandardMarginLeft = listdata.get(i).getViewMarginLeft();

  157. viewStandardMarginTop = listdata.get(i).getViewMarginTop();

  158. setLayoutParams();

  159. viewCurrentMarginLeft = viewStandardMarginLeft * WIDTH_RATIO;

  160. viewCurrentMarginTop = viewStandardMarginTop * HEIGHT_RATIO;

  161. parentLayoutType = listdata.get(i).getParentLayoutType();
  162. setLayoutByParentLayoutType();
  163. }
  164. }

  165. /**
  166. * 判断布局属性的值,设置布局的属性
  167. */
  168. private void setLayoutParams()
  169. {
  170. // 如果基准的宽是wrap_content或者fill_parent则使用原值,否则进行计算得到通配后的值
  171. if (viewStandardWidth == LAYOUTPARAMS_WARP_CONTENT
  172. || viewStandardWidth == LAYOUTPARAMS_FILL_PARENT)
  173. {
  174. viewCurrentWidth = viewStandardWidth;
  175. } else
  176. {
  177. viewCurrentWidth = viewStandardWidth * WIDTH_RATIO;
  178. }

  179. // 如果基准的宽是wrap_content或者fill_parent则使用原值,否则进行计算得到通配后的值
  180. if (viewStandardHeight == LAYOUTPARAMS_WARP_CONTENT
  181. || viewStandardHeight == LAYOUTPARAMS_FILL_PARENT)
  182. {
  183. viewCurrentHeight = viewStandardHeight;
  184. } else
  185. {
  186. viewCurrentHeight = viewStandardHeight * HEIGHT_RATIO;
  187. }
  188. }

  189. /**
  190. * 通过判断此View父类的布局类型,给此View设置布局
  191. */
  192. private void setLayoutByParentLayoutType()
  193. {

  194. if (parentLayoutType == LAYOUT_TYPE_RELATiVELAYOUT)
  195. {

  196. RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
  197. (int) viewCurrentWidth, (int) viewCurrentHeight);

  198. params.setMargins((int) viewCurrentMarginLeft,
  199. (int) viewCurrentMarginTop, 0, 0);

  200. view.setLayoutParams(params);

  201. } else if (parentLayoutType == LAYOUT_TYPE_LINEARLAYOUT)
  202. {

  203. LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
  204. (int) viewCurrentWidth, (int) viewCurrentHeight);
  205. params.setMargins((int) viewCurrentMarginLeft,
  206. (int) viewCurrentMarginTop, 0, 0);
  207. view.setLayoutParams(params);
  208. }
  209. }

  210. /**
  211. * 设置字体大小
  212. *
  213. * @param standardSize
  214. * 原始大小
  215. * @return int
  216. */
  217. public int setTextSize(int standardSize)
  218. {
  219. int currentSize;

  220. currentSize = (int) (standardSize * WIDTH_RATIO * DENSITY_RATIO);

  221. return currentSize;
  222. }
  223. }
复制代码第三步,写一个接口:

  1. public interface InitAllView{
  2. /**
  3. * 初始化控件的大小
  4. */
  5. public void initAllView();

  6. }
复制代码第四步:代码控制:

  1. /**
  2. * 通配方法
  3. */
  4. private void initWildcard() {
  5. myLayout = new MyLayoutAdapter(this, 320, 480);
  6. listInfo = new ArrayList<LayoutInformation>();
  7. listInfo.add(new LayoutInformation(mBtn1, LayoutParams.WRAP_CONTENT,
  8. LayoutParams.WRAP_CONTENT, 0, 0, LayoutInformation.R));
  9. listInfo.add(new LayoutInformation(mNowRegisterBtn, 80, 27.3, 14.7, 0,
  10. LayoutInformation.R));
  11. listInfo.add(new LayoutInformation(mNextRegisterBtn, 80, 27.3, 14.7, 0,
  12. LayoutInformation.R));

  13. // listInfo.add(new LayoutInformation(mCheckBtn, 17.3,17.3, 14.7, 0,
  14. // LayoutInformation.L));
  15. mBtn1.setTextSize(myLayout.setTextSize(12));
  16. mNowRegisterBtn.setTextSize(myLayout.setTextSize(12));
  17. mNextRegisterBtn.setTextSize(myLayout.setTextSize(12));
  18. myLayout.setViewLayout(listInfo);
  19. }
复制代码效果对比如下:
小的图片是通过适应过的,大的图片是没有做适配的。可以看得出来他们的差异很明显。如果各位大婶有什么新的方法,希望多多推荐,小弟也可以多学习几种。




效果图.jpg (6.01 KB, 下载次数: 11)

效果对比

分辨率适配的步骤


  1. package com.zte.layout.adapter;

  2. import android.view.View;

  3. /**
  4. * 存储View信息的JavaBean类
  5. *
  6. * @author
  7. *
  8. */
  9. public class LayoutInformation
  10. {
  11. /**
  12. * View的对象
  13. */
  14. private View view;

  15. /**
  16. * View的宽度
  17. */
  18. private double viewWidth;

  19. /**
  20. * View的高度
  21. */
  22. private double viewHeight;

  23. /**
  24. * View距左边的距离,即marginLeft
  25. */
  26. private double viewMarginLeft;

  27. /**
  28. * View距顶部的距离,即MarginTop;
  29. */
  30. private double viewMarginTop;

  31. /**
  32. * 父类布局的类型为相对布局
  33. */
  34. public static int R=-1;

  35. /**
  36. * 父类布局的类型为线性布局
  37. */
  38. public static int L=-2;

  39. /**
  40. * 此View的父类布局的类型
  41. */
  42. private int parentLayoutType;

  43. /**
  44. * 存储View信息的JavaBean类
  45. *
  46. * @param view
  47. * View的对象
  48. * @param viewWidth
  49. * View的宽
  50. * @param viewHeight
  51. * View的高
  52. * @param viewMarginLeft
  53. * View距左边的距离
  54. * @param viewMargdoubleop
  55. * View距上部的距离
  56. * @param parentLayoutType
  57. * 父类布局的类型,LayoutInformation.R
  58. * (表示相对布局)或者LayoutInformation.L(表示线性布局)
  59. */
  60. public LayoutInformation(View view, double viewWidth, double viewHeight,
  61. double viewMarginLeft, double viewMarginTop, int parentLayoutType)
  62. {

  63. this.view = view;
  64. this.viewWidth = viewWidth;
  65. this.viewHeight = viewHeight;
  66. this.viewMarginLeft = viewMarginLeft;
  67. this.viewMarginTop = viewMarginTop;
  68. this.parentLayoutType=parentLayoutType;
  69. }

  70. /**
  71. * 获取View的对象
  72. *
  73. * [url=home.php?mod=space&uid=7300]@return[/url] View对象
  74. */
  75. public View getView()
  76. {
  77. return view;
  78. }

  79. /**
  80. * 设置View的对象
  81. */
  82. public void setView(View view)
  83. {
  84. this.view = view;
  85. }

  86. /**
  87. * 获取View的宽度
  88. *
  89. * @return View的宽度,double型
  90. */
  91. public double getViewWidth()
  92. {
  93. return viewWidth;
  94. }

  95. /**
  96. * 设置View的宽度,double型
  97. *
  98. * @param viewWidth
  99. */
  100. public void setViewWidth(double viewWidth)
  101. {
  102. this.viewWidth = viewWidth;
  103. }

  104. /**
  105. * 获取View的高度
  106. *
  107. * @return View的高度,double型
  108. */
  109. public double getViewHeight()
  110. {
  111. return viewHeight;
  112. }

  113. /**
  114. * 设置View的高度,double型
  115. *
  116. * @param viewHeight
  117. */
  118. public void setViewHeight(double viewHeight)
  119. {
  120. this.viewHeight = viewHeight;
  121. }

  122. /**
  123. * 获取View距离左边的距离
  124. *
  125. * @return View距离左边的距离,double型
  126. */
  127. public double getViewMarginLeft()
  128. {
  129. return viewMarginLeft;
  130. }

  131. /**
  132. * 设置View距离左边的距离,double型
  133. *
  134. * @param viewMarginLeft
  135. */
  136. public void setViewMarginLeft(double viewMarginLeft)
  137. {
  138. this.viewMarginLeft = viewMarginLeft;
  139. }

  140. /**
  141. * 获取View距离上部的距离
  142. *
  143. * @return View距离上部的距离,double型
  144. */
  145. public double getViewMarginTop()
  146. {
  147. return viewMarginTop;
  148. }

  149. /**
  150. * 设置View距离上部的距离,double型
  151. *
  152. * @param viewMargdoubleop
  153. */
  154. public void setViewMarginTop(double viewMarginTop)
  155. {
  156. this.viewMarginTop = viewMarginTop;
  157. }

  158. /**
  159. * 获取父类布局的类型
  160. * @return parentLayoutType,int型
  161. */
  162. public int getParentLayoutType()
  163. {
  164. return parentLayoutType;
  165. }

  166. /**
  167. * 设置父类布局的类型
  168. * @param parentLayoutType
  169. */
  170. public void setParentLayoutType(int parentLayoutType)
  171. {
  172. this.parentLayoutType = parentLayoutType;
  173. }

  174. }

读书人网 >移动开发

热点推荐