-
Notifications
You must be signed in to change notification settings - Fork 24.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support Image resizeMode=repeat on Android #17404
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4383f89
Support multiple Postprocessors in Android ReactImageView
motiz88 90c2719
Implement Image resizeMode=repeat on Android
motiz88 b289229
Change close() to closeSafely() in a number of places
motiz88 17a8e17
Simplify ImageResizeMode.toTileMode
motiz88 431a560
Simplify MultiPostprocessor.getName
motiz88 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
ReactAndroid/src/main/java/com/facebook/react/views/image/MultiPostprocessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/** | ||
* Copyright (c) 2017-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
|
||
package com.facebook.react.views.image; | ||
|
||
import android.graphics.Bitmap; | ||
|
||
import com.facebook.cache.common.CacheKey; | ||
import com.facebook.cache.common.MultiCacheKey; | ||
import com.facebook.common.references.CloseableReference; | ||
import com.facebook.imagepipeline.bitmaps.PlatformBitmapFactory; | ||
import com.facebook.imagepipeline.request.Postprocessor; | ||
|
||
import java.util.LinkedList; | ||
import java.util.List; | ||
|
||
public class MultiPostprocessor implements Postprocessor { | ||
private final List<Postprocessor> mPostprocessors; | ||
|
||
public static Postprocessor from(List<Postprocessor> postprocessors) { | ||
switch (postprocessors.size()) { | ||
case 0: | ||
return null; | ||
case 1: | ||
return postprocessors.get(0); | ||
default: | ||
return new MultiPostprocessor(postprocessors); | ||
} | ||
} | ||
|
||
private MultiPostprocessor(List<Postprocessor> postprocessors) { | ||
mPostprocessors = new LinkedList<>(postprocessors); | ||
} | ||
|
||
@Override | ||
public String getName () { | ||
StringBuilder name = new StringBuilder(); | ||
for (Postprocessor p: mPostprocessors) { | ||
if (name.length() > 0) { | ||
name.append(","); | ||
} | ||
name.append(p.getName()); | ||
} | ||
name.insert(0, "MultiPostProcessor ("); | ||
name.append(")"); | ||
return name.toString(); | ||
} | ||
|
||
@Override | ||
public CacheKey getPostprocessorCacheKey () { | ||
LinkedList<CacheKey> keys = new LinkedList<>(); | ||
for (Postprocessor p: mPostprocessors) { | ||
keys.push(p.getPostprocessorCacheKey()); | ||
} | ||
return new MultiCacheKey(keys); | ||
} | ||
|
||
@Override | ||
public CloseableReference<Bitmap> process(Bitmap sourceBitmap, PlatformBitmapFactory bitmapFactory) { | ||
CloseableReference<Bitmap> prevBitmap = null, nextBitmap = null; | ||
|
||
try { | ||
for (Postprocessor p : mPostprocessors) { | ||
nextBitmap = p.process(prevBitmap != null ? prevBitmap.get() : sourceBitmap, bitmapFactory); | ||
CloseableReference.closeSafely(prevBitmap); | ||
prevBitmap = nextBitmap.clone(); | ||
} | ||
return nextBitmap.clone(); | ||
} finally { | ||
CloseableReference.closeSafely(nextBitmap); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
ReactAndroid/src/main/java/com/facebook/react/views/image/ScaleTypeStartInside.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/** | ||
* Copyright (c) 2017-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
|
||
package com.facebook.react.views.image; | ||
|
||
import android.graphics.Matrix; | ||
import android.graphics.Rect; | ||
import com.facebook.drawee.drawable.ScalingUtils; | ||
|
||
public class ScaleTypeStartInside extends ScalingUtils.AbstractScaleType { | ||
public static final ScalingUtils.ScaleType INSTANCE = new ScaleTypeStartInside(); | ||
|
||
@Override | ||
public void getTransformImpl( | ||
Matrix outTransform, | ||
Rect parentRect, | ||
int childWidth, | ||
int childHeight, | ||
float focusX, | ||
float focusY, | ||
float scaleX, | ||
float scaleY) { | ||
float scale = Math.min(Math.min(scaleX, scaleY), 1.0f); | ||
float dx = parentRect.left; | ||
float dy = parentRect.top; | ||
outTransform.setScale(scale, scale); | ||
outTransform.postTranslate((int) (dx + 0.5f), (int) (dy + 0.5f)); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "start_inside"; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
question: do you need to clone the output?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This clones just the reference (a cheap operation), unless I'm missing something? So to gracefully handle any errors between allocating and returning the bitmap (e.g. I don't know if
new Canvas()
might throw, or if more code might be introduced in between), I treatoutput
as a local reference which gets released withinprocess()
, and explicitly make a new reference to return (keeping theBitmap
alive) right before releasing it.