[android] Move image to the top if it is overlapping the toolbar

This commit is contained in:
Torsten Grote
2018-11-29 19:12:38 -02:00
parent 7b22d3b84d
commit 8a4a343147

View File

@@ -34,6 +34,7 @@ import static android.view.View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
import static android.view.View.SYSTEM_UI_FLAG_LAYOUT_STABLE; import static android.view.View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
import static android.view.View.VISIBLE; import static android.view.View.VISIBLE;
import static android.view.WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS; import static android.view.WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
import static android.widget.ImageView.ScaleType.FIT_START;
import static com.bumptech.glide.load.engine.DiskCacheStrategy.NONE; import static com.bumptech.glide.load.engine.DiskCacheStrategy.NONE;
import static java.util.Objects.requireNonNull; import static java.util.Objects.requireNonNull;
import static org.briarproject.briar.android.util.UiUtils.formatDateAbsolute; import static org.briarproject.briar.android.util.UiUtils.formatDateAbsolute;
@@ -47,6 +48,7 @@ public class ImageActivity extends BriarActivity
private PullDownLayout layout; private PullDownLayout layout;
private AppBarLayout appBarLayout; private AppBarLayout appBarLayout;
private PhotoView photoView;
@Override @Override
public void injectActivity(ActivityComponent component) { public void injectActivity(ActivityComponent component) {
@@ -94,7 +96,7 @@ public class ImageActivity extends BriarActivity
dateView.setText(date); dateView.setText(date);
// Image View // Image View
PhotoView photoView = findViewById(R.id.photoView); photoView = findViewById(R.id.photoView);
if (SDK_INT >= 16) { if (SDK_INT >= 16) {
photoView.setOnClickListener(view -> toggleSystemUi()); photoView.setOnClickListener(view -> toggleSystemUi());
window.getDecorView().setSystemUiVisibility( window.getDecorView().setSystemUiVisibility(
@@ -113,16 +115,19 @@ public class ImageActivity extends BriarActivity
} }
@Override @Override
public boolean onResourceReady(Drawable resource, public boolean onResourceReady(Drawable resource, Object model,
Object model, Target<Drawable> target, Target<Drawable> target, DataSource dataSource,
DataSource dataSource, boolean isFirstResource) { boolean isFirstResource) {
if (SDK_INT >= 21 && if (SDK_INT >= 21 && !(resource instanceof Animatable)) {
!(resource instanceof Animatable)) {
// set transition name only when not animatable, // set transition name only when not animatable,
// because the animation won't start otherwise // because the animation won't start otherwise
photoView.setTransitionName( photoView.setTransitionName(
attachment.getTransitionName()); attachment.getTransitionName());
} }
// Move image to the top if overlapping toolbar
if (isOverlappingToolbar(resource)) {
photoView.setScaleType(FIT_START);
}
supportStartPostponedEnterTransition(); supportStartPostponedEnterTransition();
return false; return false;
} }
@@ -209,4 +214,20 @@ public class ImageActivity extends BriarActivity
.start(); .start();
} }
private boolean isOverlappingToolbar(Drawable drawable) {
int width = drawable.getIntrinsicWidth();
int height = drawable.getIntrinsicHeight();
float widthPercentage = photoView.getWidth() / (float) width;
float heightPercentage = photoView.getHeight() / (float) height;
float scaleFactor = Math.min(widthPercentage, heightPercentage);
int realWidth = (int) (width * scaleFactor);
int realHeight = (int) (height * scaleFactor);
// return if photo doesn't use the full width,
// because it will be moved to the right otherwise
if (realWidth < photoView.getWidth()) return false;
int drawableTop = (photoView.getHeight() - realHeight) / 2;
return drawableTop < appBarLayout.getBottom() &&
drawableTop != appBarLayout.getTop();
}
} }