Android万能分辨率适应法
来源:互联网 发布:视频加背景音乐软件 编辑:程序博客网 时间:2024/06/02 21:43
(1)获取屏幕的尺寸
WindowManager windowManager = (WindowManager)
getSystemService(Context.WINDOW_SERVICE);
Display d = windowManager.getDefaultDisplay();
mWidth = d.getWidth();
mHeight = d.getHeight();
DisplayMetrics dm = getResources().getDisplayMetrics();
mScreenDensity = dm.density;
(2)美工设计图的尺寸
uiWidth,uiHeight
(3)获取缩放比例
float
scaleWidth = mWidth / uiWidth;
float
scaleHeight = mHeight/ uiHeight;
(4)所有布局的尺寸用代码实现:
public
static
int
getWidthSize(
int
size){
return
(
int
) (size * scaleWidth);
}
public
static
int
getHightSize(
int
size){
return
(
int
) (size * scaleHeight);
}
public
static
float
getTextSize(
int
pxSize){
return
(pxSize*scaleHeight) / mScreenDensity;
}
public
static
void
setViewSize(
int
width,
int
height, View v){
int
paramWidth = getWidthSize(width);
int
paramHeight = getHightSize(height);
ViewGroup.MarginLayoutParams params
= (ViewGroup.MarginLayoutParams) v.getLayoutParams();
if
(width != INVALID){
params.width = paramWidth;
}
if
(height != INVALID){
params.height = paramHeight;
}
v.setLayoutParams(params);
}
public
static
void
setViewPadding(
int
left,
int
top,
int
right,
int
bottom,
View v){
left = getWidthSize(left);
top = getHightSize(top);
right = getWidthSize(right);
bottom = getWidthSize(bottom);
v.setPadding(left, top, right, bottom);
}
public
static
void
setViewMargin(
int
left,
int
top,
int
right,
int
bottom,
View v){
int
paramLeft = getWidthSize(left);
int
paramTop = getHightSize(top);
int
paramRight = getWidthSize(right);
int
paramBottom = getHightSize(bottom);
ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams)
v.getLayoutParams();
if
(left != INVALID){
params.leftMargin = paramLeft;
}
if
(right != INVALID){
params.rightMargin = paramRight;
}
if
(top != INVALID){
params.topMargin = paramTop;
}
if
(bottom != INVALID){
params.bottomMargin = paramBottom;
}
v.setLayoutParams(params);
}
(5)这里是设置尺寸的代码:
setViewSize(
100
,
100
, mView);
setViewMargin(
20
,
0
,
0
,
20
, mView);
setViewPadding(
10
,
10
,
10
,
10
, mView);
mTextView.setTextSize(getTextSize(
30
));
由上在设计效果图时,可对图内元素进行尺寸标注,程序即可实现按比例缩放。
0 0
- Android万能分辨率适应法
- Android万能分辨率适应法
- Android万能分辨率适应法
- Android大分辨率适应
- android 多分辨率适应
- android 布局适应不同分辨率
- android程序适应各种分辨率
- android 不同分辨率,屏幕适应
- android程序适应各种分辨率
- Android程序适应多种分辨率
- (转)android 按比例布局 适应不同分辨率
- Android--让app适应不同分辨率
- 如何让android app适应不同分辨率
- Android 之百分比布局 适应不同分辨率
- Android zxing二维码扫描适应各种分辨率
- 怎样开发适应不同分辨率的Android应用
- android .9.png 点九PNG技术 适应不同分辨率
- 适应不同分辨率屏幕的问题 android layout
- win7中用MinGW编译x264出现"No working C compiler found."错误
- 头文件有.h与没.h的区别
- Hadoop必看:hadoop使用中的几个小细节(一)
- Bluetooth LE(低功耗蓝牙) - 第一部分
- ASIHTTPRequest判断网络异常的使用
- Android万能分辨率适应法
- 树的直径(最长路)
- jdbc查询sybase参数过多问题
- cocos2dx 调用 java 参数、返回值样式对照表
- 配置cmake & 使用make命令
- linux制定任务计划 crontab命令
- NFS服务器安装配置实现Ubuntu 12.04与ARM文件共享
- 简单而又深刻的认识:原码、补码、反码
- 转:每天问四句话改变孩子一生