使用Andbase框架实现屏幕适配

时间:2025-01-27 21:35:04

Andbase框架中有个AbViewUtil,经测试,确实可以实现屏幕适配。

下面是该文件源码:

这里写代码片/*
 * Copyright (C) 2012 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     /licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package ;

import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;

import ;
import ;


// TODO: Auto-generated Javadoc
/**
 * © 2012 
 * 名称: 
 * 描述:View工具类.
 *
 * @author 还如一梦中
 * @version v1.0
 * @date:2013-01-17 下午11:52:13
 */

public class AbViewUtil {

    /**
     * 无效值
     */
    public static final int INVALID = Integer.MIN_VALUE;

    /**
     * 描述:重置AbsListView的高度. item 的最外层布局要用
     * RelativeLayout,如果计算的不准,就为RelativeLayout指定一个高度
     * 
     * @param absListView
     *            the abs list view
     * @param lineNumber
     *            每行几个 ListView一行一个item
     * @param verticalSpace
     *            the vertical space
     */
    public static void setAbsListViewHeight(AbsListView absListView,
            int lineNumber, int verticalSpace) {

        int totalHeight = getAbsListViewHeight(absListView, lineNumber,
                verticalSpace);
         params = ();
         = totalHeight;
        ((MarginLayoutParams) params).setMargins(0, 0, 0, 0);
        (params);
    }

    /**
     * 描述:获取AbsListView的高度.
     *
     * @param absListView            the abs list view
     * @param lineNumber            每行几个 ListView一行一个item
     * @param verticalSpace            the vertical space
     * @return the abs list view height
     */
    public static int getAbsListViewHeight(AbsListView absListView,
            int lineNumber, int verticalSpace) {
        int totalHeight = 0;
        int w = (0,
                );
        int h = (0,
                );
        (w, h);
        ListAdapter mListAdapter = ();
        if (mListAdapter == null) {
            return totalHeight;
        }

        int count = ();
        if (absListView instanceof ListView) {
            for (int i = 0; i < count; i++) {
                View listItem = (i, null, absListView);
                (w, h);
                totalHeight += ();
            }
            if (count == 0) {
                totalHeight = verticalSpace;
            } else {
                totalHeight = totalHeight
                        + (((ListView) absListView).getDividerHeight() * (count - 1));
            }

        } else if (absListView instanceof GridView) {
            int remain = count % lineNumber;
            if (remain > 0) {
                remain = 1;
            }
            if (() == 0) {
                totalHeight = verticalSpace;
            } else {
                View listItem = (0, null, absListView);
                (w, h);
                int line = count / lineNumber + remain;
                totalHeight = line * () + (line - 1)
                        * verticalSpace;
            }

        }
        return totalHeight;

    }

    /**
     * 测量这个view
     * 最后通过getMeasuredWidth()获取宽度和高度.
     * @param view 要测量的view
     * @return 测量过的view
     */
    public static void measureView(View view) {
         p = ();
        if (p == null) {
            p = new (.MATCH_PARENT,
                    .WRAP_CONTENT);
        }

        int childWidthSpec = (0, 0 + 0, );
        int lpHeight = ;
        int childHeightSpec;
        if (lpHeight > 0) {
            childHeightSpec = (lpHeight,
                    );
        } else {
            childHeightSpec = (0,
                    );
        }
        (childWidthSpec, childHeightSpec);
    }

    /**
     * 获得这个View的宽度
     * 测量这个view,最后通过getMeasuredWidth()获取宽度.
     * @param view 要测量的view
     * @return 测量过的view的宽度
     */
    public static int getViewWidth(View view) {
        measureView(view);
        return ();
    }

    /**
     * 获得这个View的高度
     * 测量这个view,最后通过getMeasuredHeight()获取高度.
     * @param view 要测量的view
     * @return 测量过的view的高度
     */
    public static int getViewHeight(View view) {
        measureView(view);
        return ();
    }

    /**
     * 从父亲布局中移除自己
     * @param v
     */
    public static void removeSelfFromParent(View v) {
        ViewParent parent = ();
        if(parent != null){
            if(parent instanceof ViewGroup){
                ((ViewGroup)parent).removeView(v);
            }
        }
    }


    /**
     * 描述:dip转换为px.
     *
     * @param context the context
     * @param dipValue the dip value
     * @return px值
     */
    public static float dip2px(Context context, float dipValue) {
        DisplayMetrics mDisplayMetrics = (context);
        return applyDimension(TypedValue.COMPLEX_UNIT_DIP,dipValue,mDisplayMetrics);
    }

    /**
     * 描述:px转换为dip.
     *
     * @param context the context
     * @param pxValue the px value
     * @return dip值
     */
    public static float px2dip(Context context, float pxValue) {
        DisplayMetrics mDisplayMetrics = (context);
        return pxValue / ;
    }

    /**
     * 描述:sp转换为px.
     *
     * @param context the context
     * @param spValue the sp value
     * @return sp值
     */
    public static float sp2px(Context context, float spValue) {
        DisplayMetrics mDisplayMetrics = (context);
        return applyDimension(TypedValue.COMPLEX_UNIT_SP,spValue,mDisplayMetrics);
    }

    /**
     * 描述:px转换为sp.
     *
     * @param context the context
     * @param pxValue the sp value
     * @return sp值
     */
    public static float px2sp(Context context, float pxValue) {
        DisplayMetrics mDisplayMetrics = (context);
        return pxValue / ;
    }

    /**
     * 描述:根据屏幕大小缩放.
     *
     * @param context the context
     * @param value the px value
     * @return the int
     */
    public static int scaleValue(Context context, float value) {
        DisplayMetrics mDisplayMetrics = (context);
        //为了兼容尺寸小密度大的情况
        int width = ;
        int height = ;
        //解决横屏比例问题
        if(width > height){
            width = ;
            height = ;
        }
        if( >= AbAppConfig.UI_DENSITY){
            //密度
            if(width > AbAppConfig.UI_WIDTH){
                value = value*(1.3f - 1.0f/);
            }else if(width < AbAppConfig.UI_WIDTH){
                value = value*(1.0f - 1.0f/);
            }
        }else{
            //密度小屏幕大:缩小比例
            float offset = AbAppConfig.UI_DENSITY;
            if(offset > 0.5f){
                value = value * 0.9f;
            }else{
                value = value * 0.95f;
            }

        }
        return scale(,, value);
    }

    /**
     * 描述:根据屏幕大小缩放文本.
     *
     * @param context the context
     * @param value the px value
     * @return the int
     */
    public static int scaleTextValue(Context context, float value) {
        return scaleValue(context, value);
    }

    /**
     * 描述:根据屏幕大小缩放.
     *
     * @param displayWidth the display width
     * @param displayHeight the display height
     * @param pxValue the px value
     * @return the int
     */
    public static int scale(int displayWidth, int displayHeight, float pxValue) {
        if(pxValue == 0 ){
            return 0;
        }
        float scale = 1;
        try {
            int width = displayWidth;
            int height = displayHeight;
            //解决横屏比例问题
            if(width > height){
                width = displayHeight;
                height = displayWidth;
            }
            float scaleWidth = (float) width / AbAppConfig.UI_WIDTH;
            float scaleHeight = (float) height / AbAppConfig.UI_HEIGHT;
            scale = (scaleWidth, scaleHeight);
        } catch (Exception e) {
        }
        return (pxValue * scale + 0.5f);
    }


    /**
     * TypedValue官方源码中的算法,任意单位转换为PX单位
     * @param unit  TypedValue.COMPLEX_UNIT_DIP
     * @param value 对应单位的值
     * @param metrics 密度
     * @return px值
     */
    public static float applyDimension(int unit, float value,
                                       DisplayMetrics metrics){
        switch (unit) {
        case TypedValue.COMPLEX_UNIT_PX:
            return value;
        case TypedValue.COMPLEX_UNIT_DIP:
            return value * ;
        case TypedValue.COMPLEX_UNIT_SP:
            return value * ;
        case TypedValue.COMPLEX_UNIT_PT:
            return value *  * (1.0f/72);
        case TypedValue.COMPLEX_UNIT_IN:
            return value * ;
        case TypedValue.COMPLEX_UNIT_MM:
            return value *  * (1.0f/25.4f);
        }
        return 0;
    }


    /**
     * 
     * 描述:View树递归调用做适配.
     *  = 1080;
     *  = 700;
     * scaleContentView((RelativeLayout)findViewById());
     * 要求布局中的单位都用px并且和美工的设计图尺寸一致,包括所有宽高,Padding,Margin,文字大小
     * @param contentView
     */
    public static void scaleContentView(ViewGroup contentView){
        (contentView);
        if(()>0){
            for(int i=0;i<();i++){
                View view = (i);
                if(view instanceof ViewGroup){
                    if(isNeedScale(view)){
                        scaleContentView((ViewGroup)(view));
                    }
                }else{
                    scaleView((i));
                }
            }
        }
    }

    /**
     * 
     * 描述:View树递归调用做适配.
     *  = 1080;
     *  = 700;
     * scaleContentView(context,);
     * 要求布局中的单位都用px并且和美工的设计图尺寸一致,包括所有宽高,Padding,Margin,文字大小
     * @param parent
     * @param id
     */
    public static void scaleContentView(View parent,int id){
        ViewGroup contentView = null;
        View view = (id);
        if(view instanceof ViewGroup){
            contentView = (ViewGroup)view;
            scaleContentView(contentView);
        }
    }

    /**
     * 
     * 描述:View树递归调用做适配.
     *  = 1080;
     *  = 700;
     * scaleContentView(context,);
     * 要求布局中的单位都用px并且和美工的设计图尺寸一致,包括所有宽高,Padding,Margin,文字大小
     * @param context
     * @param id
     */
    public static void scaleContentView(Context context,int id){
        ViewGroup contentView = null;
        View view = ((Activity)context).findViewById(id);
        if(view instanceof ViewGroup){
            contentView = (ViewGroup)view;
            scaleContentView(contentView);
        }
    }

    /**
     * 按比例缩放View,以布局中的尺寸为基准
     * @param view
     */
    @SuppressLint("NewApi")
    public static void scaleView(View view){
        if(!isNeedScale(view)){
            return;
        }
        if (view instanceof TextView){
            TextView textView = (TextView) view;
            setTextSize(textView,());
        }

         params = () ();
        if (null != params){
            int width = INVALID;
            int height = INVALID;
            if ( != .WRAP_CONTENT
                &&  != .MATCH_PARENT){
                width = ;
            }

            if ( != .WRAP_CONTENT
                &&  != .MATCH_PARENT){
                height = ;
            }

            //size
            setViewSize(view,width,height);

            // Padding
            setPadding(view,(),(),(),());
        }

        // Margin
        if(() instanceof MarginLayoutParams){
            MarginLayoutParams mMarginLayoutParams = (MarginLayoutParams) view
                    .getLayoutParams();
            if (mMarginLayoutParams != null){
                setMargin(view,,,,);
            }
        }

        if(VERSION.SDK_INT>=16){
            //最大最小宽高
            int minWidth = scaleValue((),());
            int minHeight = scaleValue((),());
            (minWidth);
            (minHeight);
        }
    }

    /**
     * 
     * 描述:是否需要Scale.
     * @param view
     * @return
     */
    public static boolean isNeedScale(View view){
        if (view instanceof AbListViewHeader){
            return false;
        }

        if (view instanceof AbListViewFooter){
            return false;
        }
        return true;
    }

    /**
     * 缩放文字大小
     * @param textView button
     * @param size sp值
     * @return
     */
    public static void setSPTextSize(TextView textView,float size) {
        float scaledSize = scaleTextValue((),size);
        (scaledSize);
    }

    /**
     * 缩放文字大小,这样设置的好处是文字的大小不和密度有关,
     * 能够使文字大小在不同的屏幕上显示比例正确
     * @param textView button
     * @param sizePixels px值
     * @return
     */
    public static void setTextSize(TextView textView,float sizePixels) {
        float scaledSize = scaleTextValue((),sizePixels);
        (TypedValue.COMPLEX_UNIT_PX,scaledSize);
    }

    /**
     * 缩放文字大小
     * @param context
     * @param textPaint
     * @param sizePixels px值
     * @return
     */
    public static void setTextSize(Context context,TextPaint textPaint,float sizePixels) {
        float scaledSize = scaleTextValue(context,sizePixels);
        (scaledSize);
    }

    /**
     * 缩放文字大小
     * @param context
     * @param paint
     * @param sizePixels px值
     * @return
     */
    public static void setTextSize(Context context,Paint paint,float sizePixels) {
        float scaledSize = scaleTextValue(context,sizePixels);
        (scaledSize);
    }

   /**
    * 设置View的PX尺寸
    * @param view  如果是代码new出来的View,需要设置一个适合的LayoutParams
    * @param widthPixels
    * @param heightPixels
    */
    public static void setViewSize(View view,int widthPixels, int heightPixels){
        int scaledWidth = scaleValue((), widthPixels);
        int scaledHeight = scaleValue((), heightPixels);
         params = ();
        if(params == null){
            (, "setViewSize出错,如果是代码new出来的View,需要设置一个适合的LayoutParams");
            return;
        }
        if (widthPixels != INVALID){
             = scaledWidth;
        }
        if (heightPixels != INVALID && heightPixels!=1){
             = scaledHeight;
        }
        (params);
    }

    /**
     * 设置PX padding.
     *
     * @param view the view
     * @param left the left padding in pixels
     * @param top the top padding in pixels
     * @param right the right padding in pixels
     * @param bottom the bottom padding in pixels
     */
    public static void setPadding(View view, int left,
            int top, int right, int bottom) {
        int scaledLeft = scaleValue((), left);
        int scaledTop = scaleValue((), top);
        int scaledRight = scaleValue((), right);
        int scaledBottom = scaleValue((), bottom);
        (scaledLeft, scaledTop, scaledRight, scaledBottom);
    }

    /**
     * setting PX margin.
     * 
     * @param view the view
     * @param left the left margin in pixels
     * @param top the top margin in pixels
     * @param right the right margin in pixels
     * @param bottom the bottom margin in pixels
     */
    public static void setMargin(View view, int left, int top,
            int right, int bottom) {
        int scaledLeft = scaleValue((), left);
        int scaledTop = scaleValue((), top);
        int scaledRight = scaleValue((), right);
        int scaledBottom = scaleValue((), bottom);

        if(() instanceof MarginLayoutParams){
            MarginLayoutParams mMarginLayoutParams = (MarginLayoutParams) view
                    .getLayoutParams();
            if (mMarginLayoutParams != null){
                if (left != INVALID) {
                     = scaledLeft;
                }
                if (right != INVALID) {
                     = scaledRight;
                }
                if (top != INVALID) {
                     = scaledTop;
                }
                if (bottom != INVALID) {
                     = scaledBottom;
                }
                (mMarginLayoutParams);
            }
        }

    }

}

下面简单介绍其用法。

1)首先,自定义一个Application子类。

2)将自定义Application子类添加到Manifest文件中。

    <application
        android:name="."

3)在Application子类的onCreate()方法中添加美工尺寸。

        //美工效果图分辨率,用于适配
        AbAppConfig.UI_WIDTH = 640;
        AbAppConfig.UI_HEIGHT = 1136;

4)然后,在布局文件的最外层容器中添加一个id。

android:id="@+id/act_main_root_layout"

5)如果是Activity,那么在Activity的onCreate()方法中添加如下内容。

        setContentView(R.layout.activity_forget_pwd);
        //根据屏幕分辨率和屏幕密度自定缩放
        AbViewUtil.scaleContentView((LinearLayout) findViewById(R.id.act_main_root_layout));

如果是Fragment,那么在Fragment的onCreateView()方法中添加如下内容。

        inflater = LayoutInflater.from(getActivity());
        rootView = inflater.inflate(R.layout.fragment_function, container, false);
        AbViewUtil.scaleContentView((LinearLayout) rootView.findViewById(R.id.fgm_main_root_layout));

6)注意,布局文件中的宽高和文字大小都统一使用美工提供的分辨率的值,单位统一使用px,至此。一个Activity的屏幕适配就完成了。

相关文章