文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

android 分辨率适配的方法

2022-06-06 11:06

关注

以前在公司做项目的时候,遇到了分辨率的适配问题,说起来当时挺纠结的,因为没有外网,所以这个问题,都是黑暗中摸索的,尝试了许多方法,最后和徒弟们一起搞出了这个方法,具体行不行,先和大家分享下,这个方法不能说万能的,但是最起码它解决了分辨率跟密集度的关系,但是也引来一个问题,就是布局会因为图片资源小而失真,所以这也需要美工的同志多多配合的,废话不说,贴代码:
第一步,先创建一个view信息的javabean类
代码如下:
package com.zte.layout.adapter;
import android.view.View;

public class LayoutInformation
{

private View view;

private double viewWidth;

private double viewHeight;

private double viewMarginLeft;

private double viewMarginTop;

public static int R=-1;

public static int L=-2;

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;
}
}

第二步:创建一个计算方法
代码如下:
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 setVi ewLayout(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;
}
}

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();
}

第四步:代码控制
代码如下:

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);
}
您可能感兴趣的文章:Android 8.0系统中通知栏的适配详解Android 8.0系统中通知栏的适配微技巧Android 8.0系统中应用图标的适配微技巧Android-屏幕适配需要注意的地方总结Android的分辨率和屏幕适配详解Android 悬浮窗权限各机型各系统适配大全(总结)Android适配安卓6.0蓝牙通讯实现过程适配Android 8.0版本更新安装与通知栏的一些坑


阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-移动开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯