Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
licheedev committed Nov 5, 2022
0 parents commit d11c162
Show file tree
Hide file tree
Showing 81 changed files with 44,489 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
*.iml
.gradle
.idea
/local.properties
/.idea/workspace.xml
/.idea/libraries
.DS_Store
/build
/captures
.externalNativeBuild
77 changes: 77 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# CommonSize [![](https://jitpack.io/v/licheedev/SmallestWidthDimens.svg)](https://jitpack.io/#licheedev/SmallestWidthDimens)

Android SmallestWidth屏幕适配方案

## 原理 smallestWidth

https://developer.android.com/guide/practices/screens_support.html#NewQualifiers

关键的地方就是sw后面数值的单位是`dp`

* 跟hongyang的方案不同,不会用绝对分辨率适配,而使用sw(最小宽度)限定符方案
* 只关注最小宽度,无视高度,不会受有/没有虚拟导航键影响,也可以适配18:9的全面屏
* 设备的sw不会有太多,没有合适的配置的话,会使用最接近设备最小宽度的那个较小的配置(如没有小米mix2的392的话,会用384这个配置,误差不会很大)
* 预览应该无压力,毕竟sw列表就是根据官方模拟器配置列表得来的

```java
// 获取最小宽度的代码
Configuration config=getResources().getConfiguration();
int smallestScreenWidthDp=config.smallestScreenWidthDp;
```

## 使用

1. 在根build.gradle中添加

```
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
```

2. 子module添加依赖

```
dependencies {
// 根据设计图的宽度选一个,没有就按照下面配置重新生成自己要的
// 公司美工喜欢做iphone(750*1334)的图,就选sw_750
implementation 'com.github.licheedev.SmallestWidthDimens:sw_750:1.0.0'
implementation 'com.github.licheedev.SmallestWidthDimens:sw_1080:1.0.0'
}
```

没有合适的话,就按照下面配置重新生成一个module,复制到自己项目里面,然后 `compile project(':生成的模块名')`导入即可

## 生成新的尺寸配置

### 运行生成器

![generator](https://raw.githubusercontent.com/licheedev/SmallestWidthDimens/master/pics/generator.png)

### 生成器配置 generator_config.properties

最小宽度列表 [smallest_width_list.txt](https://github.com/licheedev/SmallestWidthDimens/blob/master/smallest_width_list.txt)

```properties
# 设计图参考宽度数值,单位不限,可以填多个,分别以英文逗号,隔开
design_width_list=750,1080
# 最小宽度列表路径
smallest_width_list=smallest_width_list.txt
# dp资源范围
dp_range=-360,1080
# sp资源范围
sp_range=1,100
# 资源文件名
file_name=sw_dimens.xml
# sp资源命名规则,{x}为要替换的数值,负数会在前面加上"_"
dp_res_reg=sw_{x}dp
# sp资源命名规则,{x}为要替换的数值
sp_res_reg=sw_{x}sp
# 只删除目标module
just_delete=false
# 缩进空格长度
intent_length=4
```
1 change: 1 addition & 0 deletions app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
31 changes: 31 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 31
defaultConfig {
applicationId "com.licheedev.commonsize"
minSdkVersion 17
targetSdkVersion 31
versionCode 1
versionName "1.0"
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
androidTestImplementation('androidx.test.espresso:espresso-core:3.1.0', {
exclude group: 'com.android.support', module: 'support-annotations'
})
implementation 'androidx.appcompat:appcompat:1.5.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
testImplementation 'junit:junit:4.13.2'

implementation project(':sw_750')
}
25 changes: 25 additions & 0 deletions app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in E:\DevTools\Android\sdk/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.licheedev.commonsize;

import android.content.Context;
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.ext.junit.runners.AndroidJUnit4;

import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.*;

/**
* Instrumentation test, which will execute on an Android device.
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@Test
public void useAppContext() throws Exception {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getTargetContext();

assertEquals("com.licheedev.commonsize", appContext.getPackageName());
}
}
21 changes: 21 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.licheedev.commonsizedemo"
xmlns:android="http://schemas.android.com/apk/res/android">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name="com.licheedev.commonsize.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
25 changes: 25 additions & 0 deletions app/src/main/java/com/licheedev/commonsize/MainActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.licheedev.commonsize;

import android.content.res.Configuration;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.TextView;
import com.licheedev.commonsizedemo.R;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

TextView tvSw = (TextView) findViewById(R.id.tv_sw);

System.out.println(R.dimen.sw_0dp);

Configuration config = getResources().getConfiguration();
int smallestScreenWidthDp = config.smallestScreenWidthDp;
tvSw.setText(String.format("match_parent, smallest width is %d", smallestScreenWidthDp));
System.out.println(smallestScreenWidthDp);
}
}
9 changes: 9 additions & 0 deletions app/src/main/res/drawable/selectro_test.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<solid android:color="@color/colorAccent" />
<corners android:radius="@dimen/sw_20dp" />
</shape>
</item>
</selector>
39 changes: 39 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.licheedev.commonsize.MainActivity">


<TextView
android:id="@+id/tv_sw"
android:layout_width="match_parent"
android:layout_height="@dimen/sw_100dp"
android:background="#e97d7d"
android:gravity="center"
android:text="match_parent"
android:textSize="@dimen/sw_36sp"
android:textStyle="bold" />

<TextView
android:layout_width="@dimen/sw_745dp"
android:layout_height="@dimen/sw_100dp"
android:background="#37a42d"
android:gravity="center"
android:text="sw_745dp"
android:textSize="@dimen/sw_36sp"
android:textStyle="bold" />

<TextView
android:layout_width="@dimen/sw_750dp"
android:layout_height="@dimen/sw_100dp"
android:background="#2e9ed2"
android:gravity="center"
android:text="sw_750dp"
android:textSize="@dimen/sw_36sp"
android:textStyle="bold" />

</LinearLayout>
Binary file added app/src/main/res/mipmap-hdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/mipmap-mdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/mipmap-xhdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/mipmap-xxhdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions app/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
</resources>
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<resources>
<string name="app_name">CommonSize</string>
</resources>
11 changes: 11 additions & 0 deletions app/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>

</resources>
17 changes: 17 additions & 0 deletions app/src/test/java/com/licheedev/commonsize/ExampleUnitTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.licheedev.commonsize;

import org.junit.Test;

import static org.junit.Assert.*;

/**
* Example local unit test, which will execute on the development machine (host).
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
public class ExampleUnitTest {
@Test
public void addition_isCorrect() throws Exception {
assertEquals(4, 2 + 2);
}
}
25 changes: 25 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.0.2'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}

allprojects {
repositories {
google()
jcenter()
}
}

task clean(type: Delete) {
delete rootProject.buildDir
}
1 change: 1 addition & 0 deletions generator/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
8 changes: 8 additions & 0 deletions generator/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
apply plugin: 'java'

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}

sourceCompatibility = "1.7"
targetCompatibility = "1.7"
Loading

0 comments on commit d11c162

Please sign in to comment.