Step 1. Add the JitPack repository to your build file
Add it in your root settings.gradle at the end of repositories:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url 'https://jitpack.io' }
}
}
Add it in your settings.gradle.kts at the end of repositories:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url = uri("https://jitpack.io") }
}
}
Add to pom.xml
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Add it in your build.sbt at the end of resolvers:
resolvers += "jitpack" at "https://jitpack.io"
Add it in your project.clj at the end of repositories:
:repositories [["jitpack" "https://jitpack.io"]]
Step 2. Add the dependency
dependencies {
implementation 'com.github.gavinliu:Android-ScaleLayout:'
}
dependencies {
implementation("com.github.gavinliu:Android-ScaleLayout:")
}
<dependency>
<groupId>com.github.gavinliu</groupId>
<artifactId>Android-ScaleLayout</artifactId>
<version></version>
</dependency>
libraryDependencies += "com.github.gavinliu" % "Android-ScaleLayout" % ""
:dependencies [[com.github.gavinliu/Android-ScaleLayout ""]]
A Simple & Convenience MultiScreen-Support-Library for Android
android-percent-support-lib
More reliable
android-percent-support-lib
The percentage of the parent and child views.
Android-ScaleLayout
The percentage of the design and devices screens.
More convenience
android-percent-support-lib
need to calculate percent.
Android-ScaleLayout
directly write the design size on layout.xml
.
float realPixel = percent * designPixel
float realPixel = percent * designPixel
float percent = mScreenWidth / designScreenWidth
float designPixel = res.getDimensionPixelSize()
float realPixel = mScreenWidth * res.getDimensionPixelSize() / designScreenWidth
float realPixel = percent * designPixel
float percent = mScreenWidth / designScreenWidth
float designPixel = designDP * designDensity // dp to pixel
float designDP = res.getDimensionPixelSize() / mDensity
float realPixel = (mScreenWidth * designDensity * getPixelSize()) / (designScreenWidth * mDensity)
dependencies {
compile 'cn.gavinliu.android.lib:ScaleLayout:1.0.4'
}
public class MyApplication extends Application {
@Override
public void onCreate() {
ScaleConfig.create(this,
1080, // Design Width
1920, // Design Height
3, // Design Density
3, // Design FontScale
ScaleConfig.DIMENS_UNIT_DP);
}
}
TypedValue.COMPLEX_UNIT_SP
is Android FontSize unit, thefontscale
: float fontScale = ctx.getResources().getDisplayMetrics().scaledDensity;
Only need to replace FrameLayout
LinearLayout
RelativeLayout
to ScaleFrameLayout
ScaleLinearLayout
ScaleRelativeLayout
.
Width is default, you can also changed using attr.
<attr name="layout_scale_by" format="enum">
<enum name="width" value="0"/>
<enum name="height" value="1"/>
</attr>
app:layout_scale_by="width"
<attr name="android:layout_width"/>
<attr name="android:layout_height"/>
<attr name="android:layout_margin"/>
<attr name="android:layout_marginLeft"/>
<attr name="android:layout_marginTop"/>
<attr name="android:layout_marginRight"/>
<attr name="android:layout_marginBottom"/>
<attr name="android:layout_marginStart"/>
<attr name="android:layout_marginEnd"/>
<attr name="android:padding"/>
<attr name="android:paddingLeft"/>
<attr name="android:paddingTop"/>
<attr name="android:paddingRight"/>
<attr name="android:paddingBottom"/>
<attr name="android:paddingStart"/>
<attr name="android:paddingEnd"/>
<!-- TextView -->
<attr name="android:textSize"/>
MIT