mirror of
https://github.com/MatomoCamp/matomocamp-companion-android.git
synced 2024-09-19 16:13:46 +02:00
Implement approximate state restoration for PhotoView; fixes #20
This commit is contained in:
parent
b2e50f8e4d
commit
e86a5622b6
2 changed files with 110 additions and 2 deletions
|
@ -0,0 +1,107 @@
|
|||
package be.digitalia.fosdem.widgets;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.RectF;
|
||||
import android.os.Parcel;
|
||||
import android.os.Parcelable;
|
||||
import android.support.v4.os.ParcelableCompat;
|
||||
import android.support.v4.os.ParcelableCompatCreatorCallbacks;
|
||||
import android.util.AttributeSet;
|
||||
import android.view.ViewTreeObserver;
|
||||
|
||||
import uk.co.senab.photoview.PhotoView;
|
||||
|
||||
/**
|
||||
* PhotoView which saves and restores the current scale and approximate position.
|
||||
*/
|
||||
public class SaveStatePhotoView extends PhotoView {
|
||||
|
||||
public SaveStatePhotoView(Context context) {
|
||||
super(context);
|
||||
}
|
||||
|
||||
public SaveStatePhotoView(Context context, AttributeSet attr) {
|
||||
super(context, attr);
|
||||
}
|
||||
|
||||
public SaveStatePhotoView(Context context, AttributeSet attr, int defStyle) {
|
||||
super(context, attr, defStyle);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Parcelable onSaveInstanceState() {
|
||||
Parcelable superState = super.onSaveInstanceState();
|
||||
SavedState ss = new SavedState(superState);
|
||||
ss.scale = getScale();
|
||||
final RectF rect = getDisplayRect();
|
||||
final float overflowWidth = rect.width() - getWidth();
|
||||
if (overflowWidth > 0f) {
|
||||
ss.pivotX = -rect.left / overflowWidth;
|
||||
}
|
||||
final float overflowHeight = rect.height() - getHeight();
|
||||
if (overflowHeight > 0f) {
|
||||
ss.pivotY = -rect.top / overflowHeight;
|
||||
}
|
||||
return ss;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onRestoreInstanceState(Parcelable state) {
|
||||
if (!(state instanceof SavedState)) {
|
||||
super.onRestoreInstanceState(state);
|
||||
return;
|
||||
}
|
||||
|
||||
final SavedState ss = (SavedState) state;
|
||||
super.onRestoreInstanceState(ss.getSuperState());
|
||||
|
||||
getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
|
||||
@SuppressWarnings("deprecation")
|
||||
@Override
|
||||
public void onGlobalLayout() {
|
||||
float scale = Math.max(ss.scale, getMinimumScale());
|
||||
scale = Math.min(scale, getMaximumScale());
|
||||
setScale(scale, getWidth() * ss.pivotX, getHeight() * ss.pivotY, false);
|
||||
getViewTreeObserver().removeGlobalOnLayoutListener(this);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static class SavedState extends BaseSavedState {
|
||||
float scale = 1f;
|
||||
float pivotX = 0.5f;
|
||||
float pivotY = 0.5f;
|
||||
|
||||
public SavedState(Parcelable superState) {
|
||||
super(superState);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeToParcel(Parcel out, int flags) {
|
||||
super.writeToParcel(out, flags);
|
||||
out.writeFloat(scale);
|
||||
out.writeFloat(pivotX);
|
||||
out.writeFloat(pivotY);
|
||||
}
|
||||
|
||||
public static final Parcelable.Creator<SavedState> CREATOR
|
||||
= ParcelableCompat.newCreator(new ParcelableCompatCreatorCallbacks<SavedState>() {
|
||||
@Override
|
||||
public SavedState createFromParcel(Parcel in, ClassLoader loader) {
|
||||
return new SavedState(in);
|
||||
}
|
||||
|
||||
@Override
|
||||
public SavedState[] newArray(int size) {
|
||||
return new SavedState[size];
|
||||
}
|
||||
});
|
||||
|
||||
SavedState(Parcel in) {
|
||||
super(in);
|
||||
scale = in.readFloat();
|
||||
pivotX = in.readFloat();
|
||||
pivotY = in.readFloat();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,7 +1,8 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<uk.co.senab.photoview.PhotoView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
<be.digitalia.fosdem.widgets.SaveStatePhotoView
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:id="@+id/map"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:scaleType="fitCenter"
|
||||
android:src="@drawable/campusmap" />
|
||||
android:src="@drawable/campusmap"/>
|
||||
|
|
Loading…
Reference in a new issue