From d56dcb7acc977f1f8143363d86e57c2a2aca83ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=95=C3=85=D0=A1=C3=87=C3=AF?= <1273378004@qq.com> Date: Thu, 24 Mar 2022 16:01:02 +0800 Subject: [PATCH] =?UTF-8?q?link=20#I4U9II=20#I4614W=20=E6=96=B0=E5=A2=9Epl?= =?UTF-8?q?aceholderScaleMode()=E6=8E=A5=E5=8F=A3=EF=BC=8C=E7=94=A8?= =?UTF-8?q?=E4=BA=8E=E8=AE=BE=E7=BD=AEload=E5=8D=A0=E4=BD=8D=E5=9B=BE?= =?UTF-8?q?=E3=80=81fallback=E5=8D=A0=E4=BD=8D=E5=9B=BE=E3=80=81error?= =?UTF-8?q?=E5=8D=A0=E4=BD=8D=E5=9B=BE=E7=9A=84=E7=BC=A9=E6=94=BE=E6=A8=A1?= =?UTF-8?q?=E5=BC=8F=20=E8=A7=A3=E5=86=B3=E8=A7=A3=E5=86=B3=E5=8D=A0?= =?UTF-8?q?=E4=BD=8D=E5=9B=BE=E6=98=BE=E7=A4=BA=E6=AF=94=E4=BE=8B=E5=BC=82?= =?UTF-8?q?=E5=B8=B8=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 4 +- .../drawable/DrawableDecoderCompat.java | 16 +++ .../glide/request/BaseRequestOptions.java | 17 ++- .../bumptech/glide/request/SingleRequest.java | 60 ++++---- .../glide/util/PixelMapScaleUtil.java | 135 ++++++++++++++++++ 5 files changed, 202 insertions(+), 30 deletions(-) create mode 100644 library/src/main/java/com/bumptech/glide/util/PixelMapScaleUtil.java diff --git a/build.gradle b/build.gradle index 3356f4a15..ee3d4eb5d 100644 --- a/build.gradle +++ b/build.gradle @@ -19,8 +19,8 @@ buildscript { mavenCentral() } dependencies { - classpath 'com.huawei.ohos:hap:2.4.5.0' - classpath 'com.huawei.ohos:decctest:1.2.4.1' + classpath 'com.huawei.ohos:hap:3.0.5.2' + classpath 'com.huawei.ohos:decctest:1.2.7.2' } } diff --git a/library/src/main/java/com/bumptech/glide/load/resource/drawable/DrawableDecoderCompat.java b/library/src/main/java/com/bumptech/glide/load/resource/drawable/DrawableDecoderCompat.java index 37b778f93..82dbbe5c1 100644 --- a/library/src/main/java/com/bumptech/glide/load/resource/drawable/DrawableDecoderCompat.java +++ b/library/src/main/java/com/bumptech/glide/load/resource/drawable/DrawableDecoderCompat.java @@ -1,6 +1,8 @@ package com.bumptech.glide.load.resource.drawable; import com.bumptech.glide.util.Util; +import com.bumptech.glide.util.PixelMapScaleUtil; +import com.bumptech.glide.util.PixelMapScaleUtil.PlaceholderScaleMode; import ohos.agp.components.element.Element; import ohos.agp.components.element.PixelMapElement; import ohos.app.Context; @@ -54,4 +56,18 @@ public final class DrawableDecoderCompat { } } + public static Element getDrawable(Context ourContext, int id, float viewWidth, float viewHeigth, + PlaceholderScaleMode placeholderScaleMode) { + + ResourceManager rm = ourContext.getResourceManager(); + try { + InputStream is = rm.getResource(id); + PixelMap pixelmap = Util.decodePixelMap(is, getDefaultOptions(), "image/png"); + return (Element) (new PixelMapElement(PixelMapScaleUtil.scalePixelMap(pixelmap, viewWidth, viewHeigth, placeholderScaleMode))); + + } catch (NotExistException | IOException exception) { + return null; + } + } + } diff --git a/library/src/main/java/com/bumptech/glide/request/BaseRequestOptions.java b/library/src/main/java/com/bumptech/glide/request/BaseRequestOptions.java index 2ce6b9e13..018bed534 100644 --- a/library/src/main/java/com/bumptech/glide/request/BaseRequestOptions.java +++ b/library/src/main/java/com/bumptech/glide/request/BaseRequestOptions.java @@ -7,7 +7,7 @@ import com.bumptech.glide.load.model.stream.HttpGlideUrlLoader; import com.bumptech.glide.load.resource.bitmap.*; import com.bumptech.glide.signature.EmptySignature; import com.bumptech.glide.util.CachedHashCodeArrayMap; -import com.bumptech.glide.util.LogUtil; +import com.bumptech.glide.util.PixelMapScaleUtil.PlaceholderScaleMode; import com.bumptech.glide.util.Preconditions; import com.bumptech.glide.util.Util; import ohos.agp.components.element.Element; @@ -59,6 +59,7 @@ public abstract class BaseRequestOptions> implem private int errorId; @Nullable private Element placeholderDrawable; private int placeholderId; + private PlaceholderScaleMode placeholderScaleMode = PlaceholderScaleMode.ZOOM_CENTER; private boolean isCacheable = true; private int overrideHeight = UNSET; private int overrideWidth = UNSET; @@ -375,6 +376,16 @@ public abstract class BaseRequestOptions> implem return selfOrThrowIfLocked(); } + /** + * set placeholder、error、fallback's scale mode + * @param placeholderScaleMode scale mode + * @return This request builder + */ + @NotNull + public T placeholderScaleMode(PlaceholderScaleMode placeholderScaleMode){ + this.placeholderScaleMode = placeholderScaleMode; + return selfOrThrowIfLocked(); + } /** * Allows the loaded resource to skip the memory cache. @@ -1296,11 +1307,13 @@ public abstract class BaseRequestOptions> implem return fallbackId; } - public final Element getFallbackDrawable() { return fallbackDrawable; } + public final PlaceholderScaleMode getPlaceholderScaleMode(){ + return placeholderScaleMode; + } @SuppressWarnings("WeakerAccess") public final boolean isMemoryCacheable() { diff --git a/library/src/main/java/com/bumptech/glide/request/SingleRequest.java b/library/src/main/java/com/bumptech/glide/request/SingleRequest.java index fe9813446..bdaf61b2f 100644 --- a/library/src/main/java/com/bumptech/glide/request/SingleRequest.java +++ b/library/src/main/java/com/bumptech/glide/request/SingleRequest.java @@ -7,7 +7,6 @@ import com.bumptech.glide.load.engine.Engine; import com.bumptech.glide.load.engine.GlideException; import com.bumptech.glide.load.engine.Resource; import com.bumptech.glide.load.resource.drawable.DrawableDecoderCompat; -import com.bumptech.glide.request.target.ImageViewTarget; import com.bumptech.glide.request.target.SizeReadyCallback; import com.bumptech.glide.request.target.Target; import com.bumptech.glide.request.target.ViewTarget; @@ -17,7 +16,7 @@ import com.bumptech.glide.util.LogTime; import com.bumptech.glide.util.LogUtil; import com.bumptech.glide.util.Util; import com.bumptech.glide.util.pool.StateVerifier; -import ohos.agp.components.Component; +import com.bumptech.glide.util.PixelMapScaleUtil.PlaceholderScaleMode; import ohos.agp.components.element.Element; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -72,7 +71,7 @@ public final class SingleRequest implements Request, SizeReadyCallback, Resou CLEARED, } - @Nullable + @Nullable private final String tag = IS_VERBOSE_LOGGABLE ? String.valueOf(super.hashCode()) : null; private final StateVerifier stateVerifier = StateVerifier.newInstance(); @@ -80,13 +79,15 @@ public final class SingleRequest implements Request, SizeReadyCallback, Resou /* Variables mutated only when a request is initialized or returned to the object pool. */ private final Object requestLock; - @Nullable private final RequestListener targetListener; + @Nullable + private final RequestListener targetListener; private final RequestCoordinator requestCoordinator; private final GlideContext glideContext; - @Nullable private final Object model; + @Nullable + private final Object model; private final Class transcodeClass; @@ -100,7 +101,8 @@ public final class SingleRequest implements Request, SizeReadyCallback, Resou private final Target target; - @Nullable private final List> requestListeners; + @Nullable + private final List> requestListeners; private final TransitionFactory animationFactory; @@ -119,13 +121,13 @@ public final class SingleRequest implements Request, SizeReadyCallback, Resou /* Variables mutated during a request. */ private Status status; - @Nullable + @Nullable private Element errorDrawable; - @Nullable + @Nullable private Element placeholderDrawable; - @Nullable + @Nullable private Element fallbackDrawable; private int width; @@ -134,7 +136,8 @@ public final class SingleRequest implements Request, SizeReadyCallback, Resou private boolean isCallingCallbacks; - @Nullable private RuntimeException requestOrigin; + @Nullable + private RuntimeException requestOrigin; public static SingleRequest obtain( GlideContext glideContext, @@ -255,7 +258,7 @@ public final class SingleRequest implements Request, SizeReadyCallback, Resou if ((status == Status.RUNNING || status == Status.WAITING_FOR_SIZE) && canNotifyStatusChanged()) { - if(!checkLastTarget(target)) target.onLoadStarted(getPlaceholderDrawable()); + if (!checkLastTarget(target)) target.onLoadStarted(getPlaceholderDrawable()); } if (IS_VERBOSE_LOGGABLE) { logV("finished run method in " + LogTime.getElapsedMillis(startTime)); @@ -316,7 +319,7 @@ public final class SingleRequest implements Request, SizeReadyCallback, Resou resource = null; } if (canNotifyCleared()) { - if(!checkLastTarget(target)) target.onLoadCleared(getPlaceholderDrawable()); + if (!checkLastTarget(target)) target.onLoadCleared(getPlaceholderDrawable()); } status = Status.CLEARED; @@ -369,7 +372,7 @@ public final class SingleRequest implements Request, SizeReadyCallback, Resou if (errorDrawable == null) { errorDrawable = requestOptions.getErrorPlaceholder(); if (errorDrawable == null && requestOptions.getErrorId() > 0) { - errorDrawable = loadDrawable(requestOptions.getErrorId()); + errorDrawable = loadDrawable(requestOptions.getErrorId(), requestOptions.getPlaceholderScaleMode()); } } return errorDrawable; @@ -379,7 +382,7 @@ public final class SingleRequest implements Request, SizeReadyCallback, Resou if (placeholderDrawable == null) { placeholderDrawable = requestOptions.getPlaceholderDrawable(); if (placeholderDrawable == null && requestOptions.getPlaceholderId() > 0) { - placeholderDrawable = loadDrawable(requestOptions.getPlaceholderId()); + placeholderDrawable = loadDrawable(requestOptions.getPlaceholderId(), requestOptions.getPlaceholderScaleMode()); } } return placeholderDrawable; @@ -389,17 +392,19 @@ public final class SingleRequest implements Request, SizeReadyCallback, Resou if (fallbackDrawable == null) { fallbackDrawable = requestOptions.getFallbackDrawable(); if (fallbackDrawable == null && requestOptions.getFallbackId() > 0) { - fallbackDrawable = loadDrawable(requestOptions.getFallbackId()); + fallbackDrawable = loadDrawable(requestOptions.getFallbackId(), requestOptions.getPlaceholderScaleMode()); } } return fallbackDrawable; } - private Element loadDrawable(int resourceId) { return DrawableDecoderCompat.getDrawable(glideContext.getContext(), resourceId); } + private Element loadDrawable(int resourceId, PlaceholderScaleMode placeholderScaleMode) { + return DrawableDecoderCompat.getDrawable(glideContext.getContext(), resourceId, this.width, this.height, placeholderScaleMode); + } private void setErrorPlaceholder() { if (!canNotifyStatusChanged()) { @@ -423,6 +428,7 @@ public final class SingleRequest implements Request, SizeReadyCallback, Resou /** * A callback method that should never be invoked directly. + * * @param width * @param height */ @@ -518,6 +524,7 @@ public final class SingleRequest implements Request, SizeReadyCallback, Resou /** * A callback method that should never be invoked directly. + * * @param resource * @param dataSource */ @@ -587,9 +594,9 @@ public final class SingleRequest implements Request, SizeReadyCallback, Resou /** * Internal {@link #onResourceReady(Resource, DataSource)} where arguments are known to be safe. * - * @param resource original {@link Resource}, never null - * @param result object returned by {@link Resource#get()}, checked for type and never null - * + * @param resource original {@link Resource}, never null + * @param result object returned by {@link Resource#get()}, checked for type and never null + * * @param dataSource */ private void onResourceReady(Resource resource, R result, DataSource dataSource) { @@ -631,8 +638,8 @@ public final class SingleRequest implements Request, SizeReadyCallback, Resou if (!anyListenerHandledUpdatingTarget) { Transition animation = animationFactory.build(dataSource, isFirstResource); - if(target instanceof ViewTarget) { - ((ViewTarget)target).setLastModel(model); + if (target instanceof ViewTarget) { + ((ViewTarget) target).setLastModel(model); } target.onResourceReady(result, animation); } @@ -645,6 +652,7 @@ public final class SingleRequest implements Request, SizeReadyCallback, Resou /** * A callback method that should never be invoked directly. + * * @param e */ @Override @@ -759,12 +767,12 @@ public final class SingleRequest implements Request, SizeReadyCallback, Resou LogUtil.info(TAG, message + " this: " + tag); } - private boolean checkLastTarget(Target target){ - if(target instanceof ViewTarget){ - ViewTarget viewTarget =(ViewTarget) target; + private boolean checkLastTarget(Target target) { + if (target instanceof ViewTarget) { + ViewTarget viewTarget = (ViewTarget) target; Object model = viewTarget.getLastModel(); - if(model != null) { - if(model.equals(this.model)){ + if (model != null) { + if (model.equals(this.model)) { return true; } } diff --git a/library/src/main/java/com/bumptech/glide/util/PixelMapScaleUtil.java b/library/src/main/java/com/bumptech/glide/util/PixelMapScaleUtil.java new file mode 100644 index 000000000..cd56c8d72 --- /dev/null +++ b/library/src/main/java/com/bumptech/glide/util/PixelMapScaleUtil.java @@ -0,0 +1,135 @@ +package com.bumptech.glide.util; + +import ohos.agp.render.Canvas; +import ohos.agp.render.Paint; +import ohos.agp.render.PixelMapHolder; +import ohos.agp.render.Texture; +import ohos.agp.utils.Matrix; +import ohos.media.image.PixelMap; +import ohos.media.image.common.PixelFormat; +import ohos.media.image.common.Size; + +/** + * pixelMap scaling tool class + */ +public class PixelMapScaleUtil { + + /** + * ScaleMode + */ + public enum PlaceholderScaleMode { + CENTER, CLIP_CENTER, STRETCH, ZOOM_START, ZOOM_END, ZOOM_CENTER + } + + /** + * Get a new pixelMap according to the given size + * + * @param oldPixelMap raw pixelMap + * @param newWidth new pixelMap width + * @param newHeight new pixelMap height + * @param placeholderScaleMode scale mode + * @return new pixelMap + */ + public static PixelMap scalePixelMap(PixelMap oldPixelMap, float newWidth, float newHeight, + PlaceholderScaleMode placeholderScaleMode) { + float oldWidth = oldPixelMap.getImageInfo().size.width; + float oldHeight = oldPixelMap.getImageInfo().size.height; + float[] info = getNewPixelMapInfo(oldWidth, oldHeight, newWidth, newHeight, placeholderScaleMode); + float scalex = info[0]; + float scaley = info[1]; + float left = info[2]; + float top = info[3]; + + PixelMap newPixelMap; + Matrix matrix = new Matrix(); + matrix.scale(scalex, scaley); + + newPixelMap = createPixelMap((int) newWidth, (int) newHeight, oldPixelMap.getImageInfo().pixelFormat); + + Paint paint = new Paint(); + paint.setAntiAlias(true); + paint.setFilterBitmap(true); + paint.setDither(true); + + PixelMapHolder holder = new PixelMapHolder(oldPixelMap); + Canvas canvas = new Canvas(new Texture(newPixelMap)); + canvas.setMatrix(matrix); + canvas.drawPixelMapHolder(holder, left / scalex, top / scaley, paint); + canvas.setTexture(null); + + return newPixelMap; + } + + private static float[] getNewPixelMapInfo(float oldWidth, float oldHeight, float newWidth, float newHeight, + PlaceholderScaleMode placeholderScaleMode) { + float[] info = new float[4]; + switch (placeholderScaleMode) { + case CENTER: + info[0] = 1.0f; + info[1] = 1.0f; + info[2] = (newWidth - oldWidth * info[0]) / 2; + info[3] = (newHeight - oldHeight * info[1]) / 2; + break; + case CLIP_CENTER: + if (newWidth / oldWidth >= newHeight / oldHeight) { + info[0] = newWidth / oldWidth; + info[1] = newWidth / oldWidth; + } else { + info[0] = newHeight / oldHeight; + info[1] = newHeight / oldHeight; + } + info[2] = (newWidth - oldWidth * info[0]) / 2; + info[3] = (newHeight - oldHeight * info[1]) / 2; + break; + case STRETCH: + info[0] = newWidth / oldWidth; + info[1] = newHeight / oldHeight; + info[2] = 0; + info[3] = 0; + break; + case ZOOM_START: + if (newWidth / oldWidth >= newHeight / oldHeight) { + info[0] = newHeight / oldHeight; + info[1] = newHeight / oldHeight; + } else { + info[0] = newWidth / oldWidth; + info[1] = newWidth / oldWidth; + } + info[2] = 0; + info[3] = 0; + break; + case ZOOM_END: + if (newWidth / oldWidth >= newHeight / oldHeight) { + info[0] = newHeight / oldHeight; + info[1] = newHeight / oldHeight; + } else { + info[0] = newWidth / oldWidth; + info[1] = newWidth / oldWidth; + } + info[2] = newWidth - oldWidth * info[0]; + info[3] = newHeight - oldHeight * info[1]; + break; + default: + if (newWidth / oldWidth >= newHeight / oldHeight) { + info[0] = newHeight / oldHeight; + info[1] = newHeight / oldHeight; + } else { + info[0] = newWidth / oldWidth; + info[1] = newWidth / oldWidth; + } + info[2] = (newWidth - oldWidth * info[0]) / 2; + info[3] = (newHeight - oldHeight * info[1]) / 2; + break; + } + return info; + } + + private static PixelMap createPixelMap(int width, int height, PixelFormat config) { + PixelMap.InitializationOptions opts = new PixelMap.InitializationOptions(); + opts.size = new Size(width, height); + opts.pixelFormat = config; + opts.editable = true; + return PixelMap.create(opts); + } + +} -- Gitee