我们是否应该为每种尺寸和屏幕密度创建多个可绘制文件夹以获得完美的用户体验?例如:
抽拉小LDPI
抽拉-大LDPI
抽拉小MDPI
抽拉-大MDPI
抽拉小xhdpi
抽拉-大xhdpi
......等等其他密度和尺寸。
但这种做法似乎非常困难。有没有其他简单的方法来支持所有屏幕密度和大小?
答案 0 :(得分:4)
答案 1 :(得分:0)
您可以使用以下课程
import android.app.Activity;
import android.content.Context;
import android.graphics.Typeface;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TextView;
public class SetViewSizeByPixel {
private LayoutParams params = null;
private DisplayMetrics metrics = null;
private Context mContext;
public static final int match_parent = -1;
public SetViewSizeByPixel(Context context) {
metrics = getMetrics(context);
this.mContext = context;
}
/**
*
*/
public int RH(int x) {
return metrics.heightPixels * x / 1920;
}
public int RW(int x) {
return metrics.widthPixels * x / 1080;
}
public int RH720p(int x) {
return metrics.heightPixels * x / 1280;
}
public int RW720p(int x) {
return metrics.widthPixels * x / 720;
}
public int RH(float x) {
return (int) (metrics.heightPixels * x / 1920);
}
public int RW(float x) {
return (int) (metrics.widthPixels * x / 1080);
}
/**
* Metrics
*/
public DisplayMetrics getMetrics(Activity activity) {
metrics = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
return metrics;
}
public DisplayMetrics getMetrics(Context context) {
return getMetrics((Activity) context);
}
public DisplayMetrics getMetrics() {
return metrics;
}
public int heightPixels() {
return metrics.heightPixels;
}
public int widthPixels() {
return metrics.widthPixels;
}
public float density() {
return metrics.density;
}
public int densityDpi() {
return metrics.densityDpi;
}
/**
* Size
*/
public boolean height(View view, int height) {
return size(view, -1, height);
}
public boolean width(View view, int width) {
return size(view, width, -1);
}
public boolean square(View view, int x) {
return size(view, x, x);
}
public boolean size(View view, int width, int height) {
try {
params = view.getLayoutParams();
if (width != -1)
params.width = width;
else
params.width = LayoutParams.FILL_PARENT;
if (height != -1)
params.height = height;
else
params.height = LayoutParams.FILL_PARENT;
view.setLayoutParams(params);
return true;
} catch (Exception e) {
Log.e("SetSizeByPercent", e.toString());
return false;
}
}
/**
* Size with RW, RH.
*/
public boolean heightR(View view, int height) {
return size(view, -1, this.RH(height));
}
public boolean widthR(View view, int width) {
return size(view, this.RW(width), -1);
}
public boolean squareR(View view, int x) {
return size(view, this.RW(x), this.RH(x));
}
public boolean sizeR(View view, int width, int height) {
return size(view, RW(width), RH(height));
}
/**
* Margin R
*/
public void marginTopR(View v, int top) {
marginTop(v, RH(top));
}
public void marginR(View v, int lef, int top, int rig, int bot) {
margin(v, RH(lef), RH(top), RH(rig), RH(bot));
}
public void marginR(View v, int x) {
margin(v, RW(x), RH(x), RW(x), RH(x));
}
/**
* Margin
*/
public void marginLeft(View v, int lef) {
margin(v, lef, -1, -1, -1);
}
public void marginTop(View v, int top) {
margin(v, -1, top, -1, -1);
}
public void marginRight(View v, int rig) {
margin(v, -1, -1, rig, -1);
}
public void marginBottom(View v, int bot) {
margin(v, -1, -1, -1, bot);
}
public void marginLeft(View v, int lef, boolean keepOldMargin) {
int k = keepOldMargin ? -1 : 0;
margin(v, lef, k, k, k);
}
public void marginTop(View v, int top, boolean keepOldMargin) {
int k = keepOldMargin ? -1 : 0;
margin(v, k, top, k, k);
}
public void marginRight(View v, int rig, boolean keepOldMargin) {
int k = keepOldMargin ? -1 : 0;
margin(v, k, k, rig, k);
}
public void marginBottom(View v, int bot, boolean keepOldMargin) {
int k = keepOldMargin ? -1 : 0;
margin(v, k, k, k, bot);
}
public void margin(View v, int margin) {
margin(v, margin, margin, margin, margin);
}
public void margin(View v, int lef, int top, int rig, int bot) {
if (v.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) v
.getLayoutParams();
lef = (lef == -1) ? p.leftMargin : lef;
top = (top == -1) ? p.topMargin : top;
rig = (rig == -1) ? p.rightMargin : rig;
bot = (bot == -1) ? p.leftMargin : bot;
p.setMargins(lef, top, rig, bot);
v.requestLayout();
}
}
public LayoutParams getParams() {
return params;
}
public void setParams(LayoutParams params) {
this.params = params;
}
/**
* Text Size
*/
public void textSize(TextView tv, int size) {
tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
}
public void textSize(View view, int size) {
try {
((TextView) view).setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
} catch (Exception e) {
Log.e("", e.toString());
}
}
public void textSizeR(TextView view, int size) {
view.setTextSize(TypedValue.COMPLEX_UNIT_PX, RH(size));
}
public void textSizeR(View view, int size) {
try {
((TextView) view).setTextSize(TypedValue.COMPLEX_UNIT_PX, RH(size));
} catch (Exception e) {
Log.e("", e.toString());
}
}
/**
* Animation & TypeFace
*/
public Animation getAnimation(int id) {
return AnimationUtils.loadAnimation(mContext, id);
}
public Typeface getTypeface(String typefacePath) {
return Typeface.createFromAsset(mContext.getAssets(), typefacePath);
}
// public void setSize(View v,int w,int h){
// float hrate = MyActivity.Height/1920f;
// size(v, (int) (w*hrate),(int)(h*hrate));
// }
}
OnCreate()
中的用法 SetViewSizeByPixel setSize = new SetViewSizeByPixel(this);
DisplayMetrics metrics = setSize.getMetrics(this);
setSize.height(layout_banner, metrics.heightPixels * 100 / 1024);
layout_banner.setPadding(metrics.widthPixels * 15 / 600, 0,
metrics.widthPixels * 15 / 600, 0);
setSize.marginBottom(layout_banner, metrics.heightPixels * 25 / 1024);
// textview
setSize.width(txt_title, metrics.widthPixels * 500 / 600);
600x1024:这是样本屏幕密度
15,100,500:这是一个以像素为单位的单位
希望能帮到你!!!!