适配完结篇2-グーグルの弟のAndroidマルチ解像度适合フレームワーク
7097 ワード
import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewGroup.MarginLayoutParams;
import android.widget.TextView;
// : https://blog.csdn.net/lfdfhl/article/details/52735103
public class SupportMultipleScreensUtil {
public static final int BASE_SCREEN_WIDTH = 1080;
public static float scale = 1.0F;
private SupportMultipleScreensUtil() {
}
public static void init(Context context) {
Resources resources = context.getResources();
DisplayMetrics displayMetrics = resources.getDisplayMetrics();
int widthPixels = displayMetrics.widthPixels;
scale = (float) widthPixels / BASE_SCREEN_WIDTH;
}
public static void scale(View view) {
if (null != view) {
if (view instanceof ViewGroup) {
scaleViewGroup((ViewGroup) view);
} else {
scaleView(view);
}
}
}
public static void scaleViewGroup(ViewGroup viewGroup) {
for (int i = 0; i < viewGroup.getChildCount(); ++i) {
View view = viewGroup.getChildAt(i);
if (view instanceof ViewGroup) {
scaleViewGroup((ViewGroup) view);
}
scaleView(view);
}
}
public static void scaleView(View view) {
Object isScale = view.getTag(R.id.is_scale_size_tag);
if (!(isScale instanceof Boolean) || !((Boolean) isScale).booleanValue()) {
if (view instanceof TextView) {
scaleTextView((TextView) view);
} else {
scaleViewSize(view);
}
view.setTag(R.id.is_scale_size_tag, Boolean.valueOf(true));
}
}
// TextView, , :
private static void scaleTextView(TextView textView) {
if (null != textView) {
scaleViewSize(textView);
Object isScale = textView.getTag(R.id.is_scale_font_tag);
if (!(isScale instanceof Boolean) || !((Boolean) isScale).booleanValue()) {
float size = textView.getTextSize();
size *= scale;
textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
}
Drawable[] drawables = textView.getCompoundDrawables();
Drawable leftDrawable = drawables[0];
Drawable topDrawable = drawables[1];
Drawable rightDrawable = drawables[2];
Drawable bottomDrawable = drawables[3];
setTextViewCompoundDrawables(textView, leftDrawable, topDrawable, rightDrawable, bottomDrawable);
int compoundDrawablePadding = getScaleValue(textView.getCompoundDrawablePadding());
textView.setCompoundDrawablePadding(compoundDrawablePadding);
}
}
/**
* : View ,padding,margin , 。 :
*/
private static void scaleViewSize(View view) {
if (null != view) {
int paddingLeft = getScaleValue(view.getPaddingLeft());
int paddingTop = getScaleValue(view.getPaddingTop());
int paddingRight = getScaleValue(view.getPaddingRight());
int paddingBottom = getScaleValue(view.getPaddingBottom());
view.setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom);
LayoutParams layoutParams = view.getLayoutParams();
if (null != layoutParams) {
if (layoutParams.width > 0) {
layoutParams.width = getScaleValue(layoutParams.width);
}
if (layoutParams.height > 0) {
layoutParams.height = getScaleValue(layoutParams.height);
}
if (layoutParams instanceof MarginLayoutParams) {
MarginLayoutParams marginLayoutParams = (MarginLayoutParams) layoutParams;
int topMargin = getScaleValue(marginLayoutParams.topMargin);
int leftMargin = getScaleValue(marginLayoutParams.leftMargin);
int bottomMargin = getScaleValue(marginLayoutParams.bottomMargin);
int rightMargin = getScaleValue(marginLayoutParams.rightMargin);
marginLayoutParams.topMargin = topMargin;
marginLayoutParams.leftMargin = leftMargin;
marginLayoutParams.bottomMargin = bottomMargin;
marginLayoutParams.rightMargin = rightMargin;
}
}
view.setLayoutParams(layoutParams);
}
}
private static void setTextViewCompoundDrawables(TextView textView, Drawable leftDrawable, Drawable topDrawable,
Drawable rightDrawable, Drawable bottomDrawable) {
if (null != leftDrawable) {
scaleDrawableBounds(leftDrawable);
}
if (null != rightDrawable) {
scaleDrawableBounds(rightDrawable);
}
if (null != topDrawable) {
scaleDrawableBounds(topDrawable);
}
if (null != bottomDrawable) {
scaleDrawableBounds(bottomDrawable);
}
textView.setCompoundDrawables(leftDrawable, topDrawable, rightDrawable, bottomDrawable);
}
// TextView CompoundDrawable
private static Drawable scaleDrawableBounds(Drawable drawable) {
int right = getScaleValue(drawable.getIntrinsicWidth());
int bottom = getScaleValue(drawable.getIntrinsicHeight());
drawable.setBounds(0, 0, right, bottom);
return drawable;
}
private static int getScaleValue(int value) {
return value <= 4 ? value : (int) Math.ceil((double) (scale * (float) value));
}
}
図は簡単で、直接コードを貼って、
R.id.is_scale_size_tag
とR.id.is_scale_size_tag
が間違っていることを報告して、resvaluesの下でids.xml
ファイルの下で定義するだけでいいです
-
使用法
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View rootView=findViewById(android.R.id.content);
SupportMultipleScreensUtil.init(getApplication());
SupportMultipleScreensUtil.scale(rootView);
}
まとめ
現在、xxhdpi解像度の携帯電話が主流を占めているため、このフレームワークにはdrawable-xxhdpiのカットマップが採用されている.今後xxshdpi解像度の携帯電話が主導的な地位を占めている場合は、UIデザイナーにこの解像度カットに従ってdrawable-nohdpiに置いてBASE_を変更してください.SCREEN_WIDTHでいいです.
記事ソース(References)
Androidマルチ解像度対応フレームワーク(1)—コアベース-CSDNブログ