Android and iOS add handling imageScale

This commit is contained in:
pengfei.zhou
2020-04-23 16:49:19 +08:00
committed by osborn
parent 8b1f5f6a43
commit 82fdd5e46c
8 changed files with 105 additions and 25 deletions

View File

@@ -100,11 +100,6 @@ public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.Time
} catch (Exception e) {
e.printStackTrace();
}
Resources resources = Resources.getSystem();
float scale = 0;
if (resources.getDisplayMetrics() != null) {
scale = resources.getDisplayMetrics().density;
}
JSONBuilder envObject = new JSONBuilder()
.put("platform", "Android")
.put("platformVersion", String.valueOf(android.os.Build.VERSION.SDK_INT))
@@ -112,7 +107,7 @@ public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.Time
.put("appVersion", appVersion)
.put("screenWidth", DoricUtils.px2dp(DoricUtils.getScreenWidth()))
.put("screenHeight", DoricUtils.px2dp(DoricUtils.getScreenHeight()))
.put("screenScale", scale)
.put("screenScale", DoricUtils.getScreenScale())
.put("statusBarHeight", DoricUtils.px2dp(DoricUtils.getStatusBarHeight()))
.put("hasNotch", false)
.put("deviceBrand", Build.BRAND)

View File

@@ -17,8 +17,10 @@ package pub.doric.shader;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
@@ -71,6 +73,7 @@ public class ImageNode extends ViewNode<ImageView> {
private int placeHolderColor = Color.TRANSPARENT;
private int errorColor = Color.TRANSPARENT;
private JSObject stretchInset = null;
private float imageScale = DoricUtils.getScreenScale();
public ImageNode(DoricContext doricContext) {
super(doricContext);
@@ -242,7 +245,13 @@ public class ImageNode extends ViewNode<ImageView> {
protected void setResource(@Nullable Drawable resource) {
if (resource instanceof BitmapDrawable) {
Bitmap bitmap = ((BitmapDrawable) resource).getBitmap();
if (imageScale != DoricUtils.getScreenScale()) {
float scale = DoricUtils.getScreenScale() / imageScale;
Matrix matrix = new Matrix();
matrix.setScale(scale, scale);
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
resource = new BitmapDrawable(getContext().getResources(), bitmap);
}
if (stretchInset != null) {
float left = stretchInset.getProperty("left").asNumber().toFloat();
float top = stretchInset.getProperty("top").asNumber().toFloat();
@@ -250,10 +259,10 @@ public class ImageNode extends ViewNode<ImageView> {
float bottom = stretchInset.getProperty("bottom").asNumber().toFloat();
Rect rect = new Rect(
(int) (bitmap.getWidth() * left),
(int) (bitmap.getHeight() * top),
(int) (bitmap.getWidth() * (1 - right)),
(int) (bitmap.getHeight() * (1 - bottom))
(int) left,
(int) top,
(int) (bitmap.getWidth() - right),
(int) (bitmap.getHeight() - bottom)
);
NinePatchDrawable ninePatchDrawable = new NinePatchDrawable(
@@ -354,6 +363,11 @@ public class ImageNode extends ViewNode<ImageView> {
stretchInset = prop.asObject();
}
break;
case "imageScale":
if (prop.isNumber()) {
imageScale = prop.asNumber().toFloat();
}
break;
default:
super.blend(view, name, prop);
break;

View File

@@ -229,6 +229,14 @@ public class DoricUtils {
}
}
public static float getScreenScale() {
Resources resources = Resources.getSystem();
if (resources.getDisplayMetrics() != null) {
return resources.getDisplayMetrics().density;
} else {
return 1;
}
}
private final static int NO_COLOR = 0x00000001;
private final static int X_SIZE = 2;