编程语言
首页 > 编程语言> > 最近心血来潮,把Androidautosize的源码搂了一眼

最近心血来潮,把Androidautosize的源码搂了一眼

作者:互联网

https://codechina.csdn.net/mirrors/jessyancoding/androidautosize?utm_source=csdn_github_accelerator

  以上是Androidautosize的源码,有兴趣的就去下下来搂一眼

  我这边还没看完,就把最基础的看了一下,然后顺了下简单的流程,顺便自己写了写试试,来给大家看一眼。

  Androidautosize用起来很方便,基础用法一共两步。

  第一步在你的项目里面引入Androidautosize,像这样

implementation 'me.jessyan:autosize:1.2.1'

  第二步,在你的AndroidManifest.xml  文件里面配置一下,像这样,

 <application>            
        <meta-data
            android:name="design_width_in_dp"
            android:value="360"/>
        <meta-data
            android:name="design_height_in_dp"
            android:value="640"/>           
     </application>     

  然后就完事了。具体细节的使用方法大家可以百度其他文章,这里就不在说了。之前也看过介绍这个框架的文章,说是用的contentprovider做的,但没细看。最近看了看,顺便写了写,就给大家搂一搂;

我们知道 ContentProvider 的执行时机非常的靠前,靠前到什么程度呢,在执行application之前就会执行ContentProvider ,所以可以在ContentProvider里面拿AndroidManifest.xml的参数。先看下ContentProvider 的定义,像这样

 

public class AutoSizeProvider extends ContentProvider {
    @Override
    public boolean onCreate() {
        Context applicationContext = getContext().getApplicationContext();
        AutoSizeConfig.getInstance().init((Application) applicationContext);
        return true;
    }

    @Nullable
    @Override
    public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
        return null;
    }

    @Nullable
    @Override
    public String getType(@NonNull Uri uri) {
        return null;
    }

    @Nullable
    @Override
    public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
        return null;
    }

    @Override
    public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
        return 0;
    }

    @Override
    public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) {
        return 0;
    }

  

  我们可以看到里面有个oncreate()方法,啥意思就不说了大家都懂,然后我在这里面执行了两行代码,我把其中相关的类给大家贴一下:

public class AutoSizeConfig {

    private static AutoSizeConfig sInstance;

    private String AUTO_SIZE_WIDTH_IN_DP_KEY = "sky_width_in_dp";//在文件中设置的默认宽度_KEY

    private String AUTO_SIZE_HEIGHT_IN_DP_KEY = "sky_height_in_dp";//在文件中设置的默认高度_KEY

    private int AUTO_SIZE_WIDTH_IN_DP_VALUE;//在文件中设置的默认宽度
    private int AUTO_SIZE_HEIGHT_IN_DP_VALUE;//在文件中设置的默认宽度


    private AutoSizeConfig() {
    }

    //单例对象
    public static AutoSizeConfig getInstance() {
        if (sInstance == null) {
            synchronized (AutoSizeConfig.class) {
                if (sInstance == null) {
                    sInstance = new AutoSizeConfig();
                }
            }
        }
        return sInstance;
    }

    //初始化参数
    public void init(Application application) {
        PackageManager packageManager = application.getPackageManager();
        ApplicationInfo applicationInfo = null;
        try {
      //拿到applicationinfo对象 applicationInfo = packageManager.getApplicationInfo(application.getPackageName(), PackageManager.GET_META_DATA); Bundle metaData = applicationInfo.metaData;
        //在这里拿AndroidManifest里面的参数 AUTO_SIZE_WIDTH_IN_DP_VALUE = (int) metaData.get(AUTO_SIZE_WIDTH_IN_DP_KEY); AUTO_SIZE_HEIGHT_IN_DP_VALUE = (int) metaData.get(AUTO_SIZE_HEIGHT_IN_DP_KEY); //这个是具体去执行适配方法的类,贴在下面了 LayoutSizeStrategy layoutSizeStrategy = new DefaultLayoutSizeStrategy(); //activity生命周期监听,当监听到activity执行oncreate或者onstart时,去修改系统的density和其他参数 Application.ActivityLifecycleCallbacks callbacks = new ActivityLifecycleCallbacksImpl(layoutSizeStrategy, application, AUTO_SIZE_WIDTH_IN_DP_VALUE, false); application.registerActivityLifecycleCallbacks(callbacks); } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } } }

  生命周期监听在这:

public class ActivityLifecycleCallbacksImpl implements Application.ActivityLifecycleCallbacks {

    private LayoutSizeStrategy mLayoutSizeStrategy;
    private Application application;
    private int baseSize;
    private boolean isBaseOnWidth;

    public ActivityLifecycleCallbacksImpl(LayoutSizeStrategy mLayoutSizeStrategy, Application application, int baseSize, boolean isBaseOnWidth) {
        this.mLayoutSizeStrategy = mLayoutSizeStrategy;
        this.application = application;
        this.baseSize = baseSize;
        this.isBaseOnWidth = isBaseOnWidth;
    }

    @Override
    public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle savedInstanceState) {

        Log.d("zhd", "onActivityCreated: ");
        if (mLayoutSizeStrategy != null) {
            mLayoutSizeStrategy.applyStrategy(activity, application, baseSize, isBaseOnWidth);
        }
    }

    @Override
    public void onActivityStarted(@NonNull Activity activity) {
        Log.d("zhd", "onActivityStarted: ");
        if (mLayoutSizeStrategy != null) {
            mLayoutSizeStrategy.applyStrategy(activity, application, baseSize, isBaseOnWidth);
        }
    }

    @Override
    public void onActivityResumed(@NonNull Activity activity) {

    }

    @Override
    public void onActivityPaused(@NonNull Activity activity) {

    }

    @Override
    public void onActivityStopped(@NonNull Activity activity) {

    }

    @Override
    public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle outState) {

    }

    @Override
    public void onActivityDestroyed(@NonNull Activity activity) {

    }

  

具体的执行适配代码的地方在这:

 

public class DefaultLayoutSizeStrategy implements LayoutSizeStrategy {

    private static float sNoncompatDesity;
    private static float sNoncompatScaledDensity;

    //在此执行改变的逻辑
    @Override
    public void applyStrategy(Activity activity, Application application, int baseSize, boolean isBaseOnWidth) {
        final DisplayMetrics appDisplayMetrics = application.getResources().getDisplayMetrics();
        if (sNoncompatDesity == 0) {
            sNoncompatDesity = appDisplayMetrics.density;
            sNoncompatScaledDensity = appDisplayMetrics.scaledDensity;
            application.registerComponentCallbacks(new ComponentCallbacks() {
                @Override
                public void onConfigurationChanged(@NonNull Configuration newConfig) {
                    if (newConfig != null && newConfig.fontScale > 0) {
                        sNoncompatScaledDensity = application.getResources().getDisplayMetrics().scaledDensity;
                    }
                }

                @Override
                public void onLowMemory() {

                }
            });
            final float targetDensity;
            if (isBaseOnWidth) {
                //以宽为基准
                targetDensity = appDisplayMetrics.widthPixels / baseSize;
            } else {
                //以高为基准
                targetDensity = appDisplayMetrics.heightPixels / baseSize;
            }

            final float targetScaledDensity = targetDensity * (sNoncompatScaledDensity / sNoncompatDesity);
            final int targetDensityDpi = (int) (160 * targetDensity);
            appDisplayMetrics.density = targetDensity;
            appDisplayMetrics.scaledDensity = targetScaledDensity;
            appDisplayMetrics.densityDpi = targetDensityDpi;
            final DisplayMetrics activityDisplayMetrics = activity.getResources().getDisplayMetrics();
            activityDisplayMetrics.density = targetDensity;
            activityDisplayMetrics.scaledDensity = targetScaledDensity;
            activityDisplayMetrics.densityDpi = targetDensityDpi;

        }

    }

  整个框架大体的思路就这些,其他的还没看,等有空了再看看,然后贴出来我们聊一聊。说实话,还是看看源码,然后自己动手写写有意思。

 

标签:Androidautosize,NonNull,Nullable,心血来潮,application,源码,activity,Override,public
来源: https://www.cnblogs.com/zhdsky/p/14279322.html