NekoX/TMessagesProj/src/main/java/org/telegram/ui/Components/SizeNotifierFrameLayout.java

185 lines
6.9 KiB
Java
Raw Normal View History

2015-05-21 23:27:27 +02:00
/*
2019-01-23 18:03:33 +01:00
* This is the source code of Telegram for Android v. 5.x.x.
2015-05-21 23:27:27 +02:00
* It is licensed under GNU GPL v. 2 or later.
* You should have received a copy of the license in this archive (see LICENSE).
*
2019-01-23 18:03:33 +01:00
* Copyright Nikolai Kudashov, 2013-2018.
2015-05-21 23:27:27 +02:00
*/
package org.telegram.ui.Components;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
2016-10-11 13:57:01 +02:00
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
2015-05-21 23:27:27 +02:00
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
2017-03-31 01:58:05 +02:00
import android.os.Build;
2015-05-21 23:27:27 +02:00
import android.view.View;
import android.widget.FrameLayout;
2015-09-24 22:52:02 +02:00
import org.telegram.messenger.AndroidUtilities;
2017-03-31 01:58:05 +02:00
import org.telegram.ui.ActionBar.ActionBar;
2015-05-21 23:27:27 +02:00
public class SizeNotifierFrameLayout extends FrameLayout {
private Rect rect = new Rect();
private Drawable backgroundDrawable;
private int keyboardHeight;
2015-06-29 19:12:11 +02:00
private int bottomClip;
private SizeNotifierFrameLayoutDelegate delegate;
2018-07-30 04:07:02 +02:00
private boolean occupyStatusBar = true;
2019-01-23 18:03:33 +01:00
private WallpaperParallaxEffect parallaxEffect;
private float translationX;
private float translationY;
private float parallaxScale = 1.0f;
2019-02-08 03:30:32 +01:00
private boolean paused = true;
2015-05-21 23:27:27 +02:00
2015-06-29 19:12:11 +02:00
public interface SizeNotifierFrameLayoutDelegate {
void onSizeChanged(int keyboardHeight, boolean isWidthGreater);
2015-05-21 23:27:27 +02:00
}
2015-06-29 19:12:11 +02:00
public SizeNotifierFrameLayout(Context context) {
super(context);
2015-05-21 23:27:27 +02:00
setWillNotDraw(false);
}
2019-01-23 18:03:33 +01:00
public void setBackgroundImage(Drawable bitmap, boolean motion) {
2015-05-21 23:27:27 +02:00
backgroundDrawable = bitmap;
2019-01-23 18:03:33 +01:00
if (motion) {
if (parallaxEffect == null) {
parallaxEffect = new WallpaperParallaxEffect(getContext());
parallaxEffect.setCallback((offsetX, offsetY) -> {
translationX = offsetX;
translationY = offsetY;
invalidate();
});
if (getMeasuredWidth() != 0 && getMeasuredHeight() != 0) {
parallaxScale = parallaxEffect.getScale(getMeasuredWidth(), getMeasuredHeight());
}
}
2019-02-08 03:30:32 +01:00
if (!paused) {
parallaxEffect.setEnabled(true);
}
2019-01-23 18:03:33 +01:00
} else if (parallaxEffect != null) {
parallaxEffect.setEnabled(false);
parallaxEffect = null;
2019-02-08 03:30:32 +01:00
parallaxScale = 1.0f;
2019-01-23 18:03:33 +01:00
translationX = 0;
translationY = 0;
}
2017-03-31 01:58:05 +02:00
invalidate();
2015-05-21 23:27:27 +02:00
}
public Drawable getBackgroundImage() {
return backgroundDrawable;
}
2015-06-29 19:12:11 +02:00
public void setDelegate(SizeNotifierFrameLayoutDelegate delegate) {
2015-05-21 23:27:27 +02:00
this.delegate = delegate;
}
2018-07-30 04:07:02 +02:00
public void setOccupyStatusBar(boolean value) {
occupyStatusBar = value;
}
2019-01-23 18:03:33 +01:00
public void onPause() {
if (parallaxEffect != null) {
parallaxEffect.setEnabled(false);
}
2019-02-08 03:30:32 +01:00
paused = true;
2019-01-23 18:03:33 +01:00
}
public void onResume() {
if (parallaxEffect != null) {
parallaxEffect.setEnabled(true);
}
2019-02-08 03:30:32 +01:00
paused = false;
2019-01-23 18:03:33 +01:00
}
2015-05-21 23:27:27 +02:00
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
notifyHeightChanged();
}
2015-06-29 19:12:11 +02:00
public int getKeyboardHeight() {
View rootView = getRootView();
getWindowVisibleDisplayFrame(rect);
2019-08-22 01:53:26 +02:00
if (rect.bottom == 0 && rect.top == 0) {
return 0;
}
2015-06-29 19:12:11 +02:00
int usableViewHeight = rootView.getHeight() - (rect.top != 0 ? AndroidUtilities.statusBarHeight : 0) - AndroidUtilities.getViewInset(rootView);
2019-08-22 01:53:26 +02:00
return Math.max(0, usableViewHeight - (rect.bottom - rect.top));
2015-06-29 19:12:11 +02:00
}
2015-05-21 23:27:27 +02:00
public void notifyHeightChanged() {
2019-07-18 15:01:39 +02:00
if (parallaxEffect != null) {
parallaxScale = parallaxEffect.getScale(getMeasuredWidth(), getMeasuredHeight());
}
2015-05-21 23:27:27 +02:00
if (delegate != null) {
2015-06-29 19:12:11 +02:00
keyboardHeight = getKeyboardHeight();
2015-05-21 23:27:27 +02:00
final boolean isWidthGreater = AndroidUtilities.displaySize.x > AndroidUtilities.displaySize.y;
2019-01-23 18:03:33 +01:00
post(() -> {
if (delegate != null) {
delegate.onSizeChanged(keyboardHeight, isWidthGreater);
2015-05-21 23:27:27 +02:00
}
});
}
}
2015-06-29 19:12:11 +02:00
public void setBottomClip(int value) {
bottomClip = value;
}
2015-05-21 23:27:27 +02:00
@Override
protected void onDraw(Canvas canvas) {
if (backgroundDrawable != null) {
if (backgroundDrawable instanceof ColorDrawable) {
2015-06-29 19:12:11 +02:00
if (bottomClip != 0) {
canvas.save();
canvas.clipRect(0, 0, getMeasuredWidth(), getMeasuredHeight() - bottomClip);
}
2015-05-21 23:27:27 +02:00
backgroundDrawable.setBounds(0, 0, getMeasuredWidth(), getMeasuredHeight());
backgroundDrawable.draw(canvas);
2015-06-29 19:12:11 +02:00
if (bottomClip != 0) {
canvas.restore();
}
2016-10-11 13:57:01 +02:00
} else if (backgroundDrawable instanceof BitmapDrawable) {
BitmapDrawable bitmapDrawable = (BitmapDrawable) backgroundDrawable;
if (bitmapDrawable.getTileModeX() == Shader.TileMode.REPEAT) {
2015-06-29 19:12:11 +02:00
canvas.save();
2016-10-11 13:57:01 +02:00
float scale = 2.0f / AndroidUtilities.density;
canvas.scale(scale, scale);
backgroundDrawable.setBounds(0, 0, (int) Math.ceil(getMeasuredWidth() / scale), (int) Math.ceil(getMeasuredHeight() / scale));
backgroundDrawable.draw(canvas);
2015-06-29 19:12:11 +02:00
canvas.restore();
2016-10-11 13:57:01 +02:00
} else {
2018-07-30 04:07:02 +02:00
int actionBarHeight = (isActionBarVisible() ? ActionBar.getCurrentActionBarHeight() : 0) + (Build.VERSION.SDK_INT >= 21 && occupyStatusBar ? AndroidUtilities.statusBarHeight : 0);
2017-03-31 01:58:05 +02:00
int viewHeight = getMeasuredHeight() - actionBarHeight;
2016-10-11 13:57:01 +02:00
float scaleX = (float) getMeasuredWidth() / (float) backgroundDrawable.getIntrinsicWidth();
2017-03-31 01:58:05 +02:00
float scaleY = (float) (viewHeight + keyboardHeight) / (float) backgroundDrawable.getIntrinsicHeight();
2016-10-11 13:57:01 +02:00
float scale = scaleX < scaleY ? scaleY : scaleX;
2019-01-23 18:03:33 +01:00
int width = (int) Math.ceil(backgroundDrawable.getIntrinsicWidth() * scale * parallaxScale);
int height = (int) Math.ceil(backgroundDrawable.getIntrinsicHeight() * scale * parallaxScale);
int x = (getMeasuredWidth() - width) / 2 + (int) translationX;
int y = (viewHeight - height + keyboardHeight) / 2 + actionBarHeight + (int) translationY;
2017-12-08 18:35:59 +01:00
canvas.save();
canvas.clipRect(0, actionBarHeight, width, getMeasuredHeight() - bottomClip);
2019-06-04 12:14:50 +02:00
backgroundDrawable.setAlpha(255);
2016-10-11 13:57:01 +02:00
backgroundDrawable.setBounds(x, y, x + width, y + height);
backgroundDrawable.draw(canvas);
2017-12-08 18:35:59 +01:00
canvas.restore();
2015-06-29 19:12:11 +02:00
}
2015-05-21 23:27:27 +02:00
}
} else {
super.onDraw(canvas);
}
}
2017-03-31 01:58:05 +02:00
protected boolean isActionBarVisible() {
return true;
}
2015-05-21 23:27:27 +02:00
}