Skip to content

Commit 7b70d4b

Browse files
committed
新增: Androidx Demo
1 parent fc595c8 commit 7b70d4b

37 files changed

+1087
-3
lines changed

demo-androidx/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

demo-androidx/build.gradle

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
apply plugin: 'com.android.application'
2+
3+
android {
4+
compileSdkVersion rootProject.compileSdkVersion
5+
buildToolsVersion rootProject.buildToolsVersion
6+
7+
defaultConfig {
8+
applicationId "me.jessyan.autosize.demo.androidx"
9+
minSdkVersion rootProject.minSdkVersion
10+
targetSdkVersion rootProject.targetSdkVersion
11+
versionCode rootProject.versionCode
12+
versionName rootProject.versionName
13+
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
14+
}
15+
buildTypes {
16+
release {
17+
minifyEnabled false
18+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
19+
}
20+
}
21+
}
22+
23+
dependencies {
24+
// implementation 'me.jessyan:autosize:1.1.2'
25+
implementation project(':autosize')
26+
implementation rootProject.androidx_appcompat
27+
testImplementation 'junit:junit:4.12'
28+
}

demo-androidx/proguard-rules.pro

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package me.jessyan.autosize.demo;
2+
3+
import android.content.Context;
4+
import android.support.test.InstrumentationRegistry;
5+
import android.support.test.runner.AndroidJUnit4;
6+
7+
import org.junit.Test;
8+
import org.junit.runner.RunWith;
9+
10+
import static org.junit.Assert.*;
11+
12+
/**
13+
* Instrumented test, which will execute on an Android device.
14+
*
15+
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
16+
*/
17+
@RunWith(AndroidJUnit4.class)
18+
public class ExampleInstrumentedTest {
19+
@Test
20+
public void useAppContext() {
21+
// Context of the app under test.
22+
Context appContext = InstrumentationRegistry.getTargetContext();
23+
24+
assertEquals("me.jessyan.autosize.demo", appContext.getPackageName());
25+
}
26+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="me.jessyan.autosize.demo.androidx">
4+
5+
<application
6+
android:name=".BaseApplication"
7+
android:allowBackup="true"
8+
android:icon="@mipmap/autosize_logo"
9+
android:label="@string/app_name"
10+
android:supportsRtl="true"
11+
android:theme="@style/AppTheme">
12+
<activity android:name=".MainActivity">
13+
<intent-filter>
14+
<action android:name="android.intent.action.MAIN"/>
15+
16+
<category android:name="android.intent.category.LAUNCHER"/>
17+
</intent-filter>
18+
</activity>
19+
<activity android:name=".CustomAdaptActivity"/>
20+
<activity android:name=".FragmentHost"/>
21+
22+
<!-- 如果您项目中的所有页面都只需要以高或宽中的一个作为基准进行适配的话, 那就只需要填写高或宽中的一个设计图尺寸即可 -->
23+
<meta-data
24+
android:name="design_width_in_dp"
25+
android:value="360"/>
26+
<meta-data
27+
android:name="design_height_in_dp"
28+
android:value="640"/>
29+
30+
</application>
31+
32+
</manifest>
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
* Copyright 2018 JessYan
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package me.jessyan.autosize.demo.androidx;
17+
18+
import android.app.Activity;
19+
import android.app.Application;
20+
import android.util.DisplayMetrics;
21+
22+
import java.util.Locale;
23+
24+
import me.jessyan.autosize.AutoSize;
25+
import me.jessyan.autosize.AutoSizeConfig;
26+
import me.jessyan.autosize.onAdaptListener;
27+
import me.jessyan.autosize.utils.AutoSizeLog;
28+
29+
/**
30+
* ================================================
31+
* v0.9.1 发布后新增了副单位,可以在 pt、in、mm 三个冷门单位中选择一个作为副单位,然后在 layout 文件中使用副单位进行布局
32+
* 副单位可以规避修改 {@link DisplayMetrics#density} 所造成的对于其他使用 dp 布局的系统控件或三方库控件的不良影响
33+
* 使用副单位后可直接在 AndroidManifest 中填写设计图上的像素尺寸,不需要再将像素转化为 dp
34+
* <a href="https://github.com/JessYanCoding/AndroidAutoSize/blob/master/README-zh.md#preview">点击查看在布局中的实时预览方式</a>
35+
* <p>
36+
* 本框架核心原理来自于 <a href="https://mp.weixin.qq.com/s/d9QCoBP6kV9VSWvVldVVwA">今日头条官方适配方案</a>
37+
* <p>
38+
* 本框架源码的注释都很详细, 欢迎阅读学习
39+
* <p>
40+
* AndroidAutoSize 会在 APP 启动时自动完成初始化, 如果您想设置自定义参数可以在 {@link Application#onCreate()} 中设置
41+
* <p>
42+
* Created by JessYan on 2018/8/9 17:05
43+
* <a href="mailto:[email protected]">Contact me</a>
44+
* <a href="https://github.com/JessYanCoding">Follow me</a>
45+
* ================================================
46+
*/
47+
public class BaseApplication extends Application {
48+
@Override
49+
public void onCreate() {
50+
super.onCreate();
51+
//当 App 中出现多进程, 并且您需要适配所有的进程, 就需要在 App 初始化时调用 initCompatMultiProcess()
52+
//在 Demo 中跳转的三方库中的 DefaultErrorActivity 就是在另外一个进程中, 所以要想适配这个 Activity 就需要调用 initCompatMultiProcess()
53+
AutoSize.initCompatMultiProcess(this);
54+
/**
55+
* 以下是 AndroidAutoSize 可以自定义的参数, {@link AutoSizeConfig} 的每个方法的注释都写的很详细
56+
* 使用前请一定记得跳进源码,查看方法的注释, 下面的注释只是简单描述!!!
57+
*/
58+
AutoSizeConfig.getInstance()
59+
60+
//是否让框架支持自定义 Fragment 的适配参数, 由于这个需求是比较少见的, 所以须要使用者手动开启
61+
//如果没有这个需求建议不开启
62+
.setCustomFragment(true)
63+
64+
//是否屏蔽系统字体大小对 AndroidAutoSize 的影响, 如果为 true, App 内的字体的大小将不会跟随系统设置中字体大小的改变
65+
//如果为 false, 则会跟随系统设置中字体大小的改变, 默认为 false
66+
// .setExcludeFontScale(true)
67+
68+
//区别于系统字体大小的放大比例, AndroidAutoSize 允许 APP 内部可以独立于系统字体大小之外,独自拥有全局调节 APP 字体大小的能力
69+
//当然, 在 APP 内您必须使用 sp 来作为字体的单位, 否则此功能无效, 不设置或将此值设为 0 则取消此功能
70+
// .setPrivateFontScale(0.8f)
71+
72+
//屏幕适配监听器
73+
.setOnAdaptListener(new onAdaptListener() {
74+
@Override
75+
public void onAdaptBefore(Object target, Activity activity) {
76+
//使用以下代码, 可以解决横竖屏切换时的屏幕适配问题
77+
//使用以下代码, 可支持 Android 的分屏或缩放模式, 但前提是在分屏或缩放模式下当用户改变您 App 的窗口大小时
78+
//系统会重绘当前的页面, 经测试在某些机型, 某些情况下系统不会重绘当前页面, ScreenUtils.getScreenSize(activity) 的参数一定要不要传 Application!!!
79+
// AutoSizeConfig.getInstance().setScreenWidth(ScreenUtils.getScreenSize(activity)[0]);
80+
// AutoSizeConfig.getInstance().setScreenHeight(ScreenUtils.getScreenSize(activity)[1]);
81+
AutoSizeLog.d(String.format(Locale.ENGLISH, "%s onAdaptBefore!", target.getClass().getName()));
82+
}
83+
84+
@Override
85+
public void onAdaptAfter(Object target, Activity activity) {
86+
AutoSizeLog.d(String.format(Locale.ENGLISH, "%s onAdaptAfter!", target.getClass().getName()));
87+
}
88+
})
89+
90+
//是否打印 AutoSize 的内部日志, 默认为 true, 如果您不想 AutoSize 打印日志, 则请设置为 false
91+
// .setLog(false)
92+
93+
//是否使用设备的实际尺寸做适配, 默认为 false, 如果设置为 false, 在以屏幕高度为基准进行适配时
94+
//AutoSize 会将屏幕总高度减去状态栏高度来做适配
95+
//设置为 true 则使用设备的实际屏幕高度, 不会减去状态栏高度
96+
// .setUseDeviceSize(true)
97+
98+
//是否全局按照宽度进行等比例适配, 默认为 true, 如果设置为 false, AutoSize 会全局按照高度进行适配
99+
// .setBaseOnWidth(false)
100+
101+
//设置屏幕适配逻辑策略类, 一般不用设置, 使用框架默认的就好
102+
// .setAutoAdaptStrategy(new AutoAdaptStrategy())
103+
;
104+
}
105+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright 2018 JessYan
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package me.jessyan.autosize.demo.androidx;
17+
18+
import android.app.Activity;
19+
import android.content.Intent;
20+
import android.os.Bundle;
21+
import android.view.View;
22+
23+
import androidx.appcompat.app.AppCompatActivity;
24+
import androidx.fragment.app.Fragment;
25+
import me.jessyan.autosize.internal.CustomAdapt;
26+
27+
/**
28+
* ================================================
29+
* 本框架核心原理来自于 <a href="https://mp.weixin.qq.com/s/d9QCoBP6kV9VSWvVldVVwA">今日头条官方适配方案</a>
30+
* <p>
31+
* {@link CustomAdaptActivity} 展示项目内部的 {@link Activity} 自定义适配参数的用法, 需要实现 {@link CustomAdapt}
32+
* 现在 AndroidAutoSize 是全局以屏幕宽度为基准进行适配的, 并且全局的设计图尺寸为 360 * 640
33+
* 这里就展示怎么让 {@link CustomAdaptActivity} 单个页面, 有别于全局设置, 以屏幕高度为基准进行适配, 并且更改设计图尺寸为 iPhone 的设计图尺寸
34+
* 如果这个页面的设计图尺寸有别于其他页面, AndroidAutoSize 允许您改变单个页面的设计图尺寸, {@link #getSizeInDp()}
35+
* <p>
36+
* Created by JessYan on 2018/8/11 11:31
37+
* <a href="mailto:[email protected]">Contact me</a>
38+
* <a href="https://github.com/JessYanCoding">Follow me</a>
39+
* ================================================
40+
*/
41+
public class CustomAdaptActivity extends AppCompatActivity implements CustomAdapt {
42+
43+
@Override
44+
protected void onCreate(Bundle savedInstanceState) {
45+
super.onCreate(savedInstanceState);
46+
setContentView(R.layout.activity_custom_adapt);
47+
}
48+
49+
/**
50+
* 跳转到 {@link FragmentHost}, 展示项目内部的 {@link Fragment} 自定义适配参数的用法
51+
*
52+
* @param view {@link View}
53+
*/
54+
public void goCustomAdaptFragment(View view) {
55+
startActivity(new Intent(getApplicationContext(), FragmentHost.class));
56+
}
57+
58+
/**
59+
* 是否按照宽度进行等比例适配 (为了保证在高宽比不同的屏幕上也能正常适配, 所以只能在宽度和高度之中选择一个作为基准进行适配)
60+
*
61+
* @return {@code true} 为按照宽度进行适配, {@code false} 为按照高度进行适配
62+
*/
63+
@Override
64+
public boolean isBaseOnWidth() {
65+
return false;
66+
}
67+
68+
/**
69+
* 这里使用 iPhone 的设计图, iPhone 的设计图尺寸为 750px * 1334px, 高换算成 dp 为 667 (1334px / 2 = 667dp)
70+
* <p>
71+
* 返回设计图上的设计尺寸, 单位 dp
72+
* {@link #getSizeInDp} 须配合 {@link #isBaseOnWidth()} 使用, 规则如下:
73+
* 如果 {@link #isBaseOnWidth()} 返回 {@code true}, {@link #getSizeInDp} 则应该返回设计图的总宽度
74+
* 如果 {@link #isBaseOnWidth()} 返回 {@code false}, {@link #getSizeInDp} 则应该返回设计图的总高度
75+
* 如果您不需要自定义设计图上的设计尺寸, 想继续使用在 AndroidManifest 中填写的设计图尺寸, {@link #getSizeInDp} 则返回 {@code 0}
76+
*
77+
* @return 设计图上的设计尺寸, 单位 dp
78+
*/
79+
@Override
80+
public float getSizeInDp() {
81+
return 667;
82+
}
83+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright 2018 JessYan
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package me.jessyan.autosize.demo.androidx;
17+
18+
import android.os.Bundle;
19+
import android.view.Gravity;
20+
import android.view.LayoutInflater;
21+
import android.view.View;
22+
import android.view.ViewGroup;
23+
import android.widget.TextView;
24+
25+
import androidx.annotation.NonNull;
26+
import androidx.annotation.Nullable;
27+
import androidx.fragment.app.Fragment;
28+
import me.jessyan.autosize.AutoSize;
29+
import me.jessyan.autosize.internal.CustomAdapt;
30+
import me.jessyan.autosize.utils.AutoSizeUtils;
31+
32+
/**
33+
* ================================================
34+
* Created by JessYan on 2018/8/25 14:06
35+
* <a href="mailto:[email protected]">Contact me</a>
36+
* <a href="https://github.com/JessYanCoding">Follow me</a>
37+
* ================================================
38+
*/
39+
public class CustomFragment1 extends Fragment implements CustomAdapt {
40+
41+
@Nullable
42+
@Override
43+
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
44+
//由于某些原因, 屏幕旋转后 Fragment 的重建, 会导致框架对 Fragment 的自定义适配参数失去效果
45+
//所以如果您的 Fragment 允许屏幕旋转, 则请在 onCreateView 手动调用一次 AutoSize.autoConvertDensity()
46+
//如果您的 Fragment 不允许屏幕旋转, 则可以将下面调用 AutoSize.autoConvertDensity() 的代码删除掉
47+
AutoSize.autoConvertDensity(getActivity(), 1080, true);
48+
return createTextView(inflater, "Fragment-1\nView width = 360dp\nTotal width = 1080dp", 0xffff0000);
49+
}
50+
51+
@Override
52+
public boolean isBaseOnWidth() {
53+
return true;
54+
}
55+
56+
@Override
57+
public float getSizeInDp() {
58+
return 1080;
59+
}
60+
61+
public static View createTextView(LayoutInflater inflater, String content, int backgroundColor) {
62+
TextView view = new TextView(inflater.getContext());
63+
ViewGroup.LayoutParams layoutParams =
64+
new ViewGroup.LayoutParams((AutoSizeUtils.dp2px(inflater.getContext(), 360)),
65+
ViewGroup.LayoutParams.MATCH_PARENT);
66+
view.setLayoutParams(layoutParams);
67+
view.setText(content);
68+
view.setTextColor(0xffffffff);
69+
view.setGravity(Gravity.CENTER);
70+
view.setTextSize(30);
71+
view.setBackgroundColor(backgroundColor);
72+
return view;
73+
}
74+
}

0 commit comments

Comments
 (0)