1
0
Fork 0
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:
Christophe Beyls 2017-01-22 21:11:07 +01:00
parent b2e50f8e4d
commit e86a5622b6
2 changed files with 110 additions and 2 deletions

View file

@ -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();
}
}
}

View file

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-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:id="@+id/map"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"