-
Notifications
You must be signed in to change notification settings - Fork 271
Tapping on an image opens pinch-to-zoom view #1491
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
Open
amarek
wants to merge
3
commits into
wallabag:master
Choose a base branch
from
amarek:image-zoom
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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 hidden or 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 hidden or 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 hidden or 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,10 @@ | ||
| document.addEventListener("DOMContentLoaded", function() { | ||
| document.addEventListener("click", function(e) { | ||
| var target = e.target; | ||
| if (target.tagName === "IMG" && target.src) { | ||
| e.preventDefault(); | ||
| e.stopPropagation(); | ||
| hostImageController.onImageClicked(target.src); | ||
| } | ||
| }, true); | ||
| }); |
147 changes: 147 additions & 0 deletions
147
app/src/main/java/fr/gaulupeau/apps/Poche/ui/ImageViewActivity.java
This file contains hidden or 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,147 @@ | ||
| package fr.gaulupeau.apps.Poche.ui; | ||
|
|
||
| import android.graphics.Bitmap; | ||
| import android.graphics.BitmapFactory; | ||
| import android.os.Bundle; | ||
| import android.util.Log; | ||
| import android.view.View; | ||
| import android.widget.ProgressBar; | ||
|
|
||
| import androidx.appcompat.app.AppCompatActivity; | ||
|
|
||
| import com.github.panpf.zoomimage.ZoomImageView; | ||
|
|
||
| import java.io.File; | ||
|
|
||
| import fr.gaulupeau.apps.InThePoche.R; | ||
| import fr.gaulupeau.apps.Poche.network.ImageCacheUtils; | ||
| import fr.gaulupeau.apps.Poche.network.WallabagConnection; | ||
| import okhttp3.OkHttpClient; | ||
| import okhttp3.Request; | ||
| import okhttp3.Response; | ||
| import okhttp3.ResponseBody; | ||
|
|
||
| public class ImageViewActivity extends AppCompatActivity { | ||
|
|
||
| public static final String EXTRA_IMAGE_URL = "ImageViewActivity.imageUrl"; | ||
| public static final String EXTRA_ARTICLE_ID = "ImageViewActivity.articleId"; | ||
|
|
||
| private static final String TAG = ImageViewActivity.class.getSimpleName(); | ||
|
|
||
| @Override | ||
| protected void onCreate(Bundle savedInstanceState) { | ||
| super.onCreate(savedInstanceState); | ||
| setContentView(R.layout.activity_image_view); | ||
|
|
||
| ZoomImageView photoView = findViewById(R.id.photoView); | ||
| ProgressBar progressBar = findViewById(R.id.progressBar); | ||
|
|
||
| String imageUrl = getIntent().getStringExtra(EXTRA_IMAGE_URL); | ||
| long articleId = getIntent().getLongExtra(EXTRA_ARTICLE_ID, -1); | ||
|
|
||
| if (imageUrl == null || imageUrl.isEmpty()) { | ||
| Log.w(TAG, "onCreate() no image URL"); | ||
| finish(); | ||
| return; | ||
| } | ||
|
|
||
| photoView.setOnClickListener(v -> finish()); | ||
|
|
||
| new Thread(() -> { | ||
| Bitmap bitmap = loadBitmap(imageUrl, articleId); | ||
| runOnUiThread(() -> { | ||
| progressBar.setVisibility(View.GONE); | ||
| if (bitmap != null) { | ||
| photoView.setImageBitmap(bitmap); | ||
| } else { | ||
| Log.w(TAG, "onCreate() failed to load image"); | ||
| finish(); | ||
| } | ||
| }); | ||
| }).start(); | ||
| } | ||
|
|
||
| private Bitmap loadBitmap(String imageUrl, long articleId) { | ||
| // Try loading from local cache first | ||
| if (articleId >= 0) { | ||
| try { | ||
| File file = ImageCacheUtils.getCachedImageFile(imageUrl, articleId); | ||
| if (file != null) { | ||
| Bitmap bitmap = decodeFileScaled(file.getAbsolutePath()); | ||
| if (bitmap != null) return bitmap; | ||
| } | ||
| } catch (Exception e) { | ||
| Log.w(TAG, "loadBitmap() cache load failed", e); | ||
| } | ||
| } | ||
|
|
||
| // Try loading from file:// URL (cached images served to WebView) | ||
| if (imageUrl.startsWith("file://")) { | ||
| try { | ||
| String path = imageUrl.substring("file://".length()); | ||
| Bitmap bitmap = decodeFileScaled(path); | ||
| if (bitmap != null) return bitmap; | ||
| } catch (Exception e) { | ||
| Log.w(TAG, "loadBitmap() file URL load failed", e); | ||
| } | ||
| } | ||
|
|
||
| // Fall back to loading from network | ||
| try { | ||
| OkHttpClient client = WallabagConnection.createClient(); | ||
| Request request = new Request.Builder().url(imageUrl).build(); | ||
| try (Response response = client.newCall(request).execute()) { | ||
| ResponseBody body = response.body(); | ||
| if (response.isSuccessful() && body != null) { | ||
| return decodeBytesScaled(body.bytes()); | ||
| } | ||
| } | ||
| } catch (Exception e) { | ||
| Log.w(TAG, "loadBitmap() remote load failed", e); | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| // 2048 keeps the bitmap (~16MB @ ARGB_8888) under the Canvas draw limit and | ||
| // within GL_MAX_TEXTURE_SIZE on older GPUs. Article images are typically <2048px wide anyway. | ||
| private static final int MAX_BITMAP_DIMENSION = 2048; | ||
|
|
||
| private static Bitmap decodeFileScaled(String path) { | ||
| BitmapFactory.Options options = new BitmapFactory.Options(); | ||
| options.inJustDecodeBounds = true; | ||
| BitmapFactory.decodeFile(path, options); | ||
| options.inSampleSize = computeSampleSize(options.outWidth, options.outHeight); | ||
| options.inJustDecodeBounds = false; | ||
| try { | ||
| return BitmapFactory.decodeFile(path, options); | ||
| } catch (OutOfMemoryError e) { | ||
| Log.w(TAG, "decodeFileScaled() out of memory"); | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| private static Bitmap decodeBytesScaled(byte[] data) { | ||
| BitmapFactory.Options options = new BitmapFactory.Options(); | ||
| options.inJustDecodeBounds = true; | ||
| BitmapFactory.decodeByteArray(data, 0, data.length, options); | ||
| options.inSampleSize = computeSampleSize(options.outWidth, options.outHeight); | ||
| options.inJustDecodeBounds = false; | ||
| try { | ||
| return BitmapFactory.decodeByteArray(data, 0, data.length, options); | ||
| } catch (OutOfMemoryError e) { | ||
| Log.w(TAG, "decodeBytesScaled() out of memory"); | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| private static int computeSampleSize(int width, int height) { | ||
| int sampleSize = 1; | ||
| while (width > 0 && height > 0 | ||
| && (width / sampleSize > MAX_BITMAP_DIMENSION | ||
| || height / sampleSize > MAX_BITMAP_DIMENSION)) { | ||
| sampleSize *= 2; | ||
| } | ||
| return sampleSize; | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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,20 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <FrameLayout | ||
| xmlns:android="http://schemas.android.com/apk/res/android" | ||
| android:layout_width="match_parent" | ||
| android:layout_height="match_parent" | ||
| android:background="#000000"> | ||
|
|
||
| <com.github.panpf.zoomimage.ZoomImageView | ||
| android:id="@+id/photoView" | ||
| android:layout_width="match_parent" | ||
| android:layout_height="match_parent" /> | ||
|
|
||
| <ProgressBar | ||
| android:id="@+id/progressBar" | ||
| android:layout_width="wrap_content" | ||
| android:layout_height="wrap_content" | ||
| android:layout_gravity="center" | ||
| android:indeterminateTint="@android:color/white" /> | ||
|
|
||
| </FrameLayout> |
Oops, something went wrong.
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.
We should add the
android:configChanges="keyboardHidden|orientation|screenSize"here, analog to the other activities, so that this image view acitvity does not get recreated when we rotate the phone while viewing the image.