This source file includes following definitions.
- onSingleTap
- onLongPress
- onPopupZoomerShown
- onPopupZoomerHidden
- getOverlayCornerRadius
- getOverlayDrawable
- constrain
- constrain
- setOnTapListener
- setOnVisibilityChangedListener
- setBitmap
- scroll
- startAnimation
- hideImmediately
- isShowing
- setLastTouch
- setTargetBounds
- initDimensions
- acceptZeroSizeView
- onDraw
- show
- hide
- convertTouchPoint
- isTouchOutsideArea
- onTouchEvent
- getInterpolation
package org.chromium.content.browser;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Path.Direction;
import android.graphics.PointF;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Region.Op;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.SystemClock;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Interpolator;
import android.view.animation.OvershootInterpolator;
import org.chromium.content.R;
class PopupZoomer extends View {
private static final String LOGTAG = "PopupZoomer";
private static final int ZOOM_BOUNDS_MARGIN = 25;
private static final long ANIMATION_DURATION = 300;
public static interface OnTapListener {
public boolean onSingleTap(View v, MotionEvent event);
public boolean onLongPress(View v, MotionEvent event);
}
private OnTapListener mOnTapListener = null;
public static interface OnVisibilityChangedListener {
public void onPopupZoomerShown(PopupZoomer zoomer);
public void onPopupZoomerHidden(PopupZoomer zoomer);
}
private OnVisibilityChangedListener mOnVisibilityChangedListener = null;
private static Drawable sOverlayDrawable;
private static Rect sOverlayPadding;
private static float sOverlayCornerRadius;
private final Interpolator mShowInterpolator = new OvershootInterpolator();
private final Interpolator mHideInterpolator = new ReverseInterpolator(mShowInterpolator);
private boolean mAnimating = false;
private boolean mShowing = false;
private long mAnimationStartTime = 0;
private long mTimeLeft = 0;
private boolean mNeedsToInitDimensions;
private RectF mViewClipRect;
private Rect mTargetBounds;
private Bitmap mZoomedBitmap;
private float mShiftX = 0, mShiftY = 0;
private float mScale = 1.0f;
private RectF mClipRect;
private float mLeftExtrusion, mTopExtrusion, mRightExtrusion, mBottomExtrusion;
private final PointF mTouch = new PointF();
private float mPopupScrollX, mPopupScrollY;
private float mMinScrollX, mMaxScrollX;
private float mMinScrollY, mMaxScrollY;
private GestureDetector mGestureDetector;
private static float getOverlayCornerRadius(Context context) {
if (sOverlayCornerRadius == 0) {
try {
sOverlayCornerRadius = context.getResources().getDimension(
R.dimen.link_preview_overlay_radius);
} catch (Resources.NotFoundException e) {
Log.w(LOGTAG, "No corner radius resource for PopupZoomer overlay found.");
sOverlayCornerRadius = 1.0f;
}
}
return sOverlayCornerRadius;
}
private static Drawable getOverlayDrawable(Context context) {
if (sOverlayDrawable == null) {
try {
sOverlayDrawable = context.getResources().getDrawable(
R.drawable.ondemand_overlay);
} catch (Resources.NotFoundException e) {
Log.w(LOGTAG, "No drawable resource for PopupZoomer overlay found.");
sOverlayDrawable = new ColorDrawable();
}
sOverlayPadding = new Rect();
sOverlayDrawable.getPadding(sOverlayPadding);
}
return sOverlayDrawable;
}
private static float constrain(float amount, float low, float high) {
return amount < low ? low : (amount > high ? high : amount);
}
private static int constrain(int amount, int low, int high) {
return amount < low ? low : (amount > high ? high : amount);
}
public PopupZoomer(Context context) {
super(context);
setVisibility(INVISIBLE);
setFocusable(true);
setFocusableInTouchMode(true);
GestureDetector.SimpleOnGestureListener listener =
new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2,
float distanceX, float distanceY) {
if (mAnimating) return true;
if (isTouchOutsideArea(e1.getX(), e1.getY())) {
hide(true);
} else {
scroll(distanceX, distanceY);
}
return true;
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
return handleTapOrPress(e, false);
}
@Override
public void onLongPress(MotionEvent e) {
handleTapOrPress(e, true);
}
private boolean handleTapOrPress(MotionEvent e, boolean isLongPress) {
if (mAnimating) return true;
float x = e.getX();
float y = e.getY();
if (isTouchOutsideArea(x, y)) {
hide(true);
} else if (mOnTapListener != null) {
PointF converted = convertTouchPoint(x, y);
MotionEvent event = MotionEvent.obtainNoHistory(e);
event.setLocation(converted.x, converted.y);
if (isLongPress) {
mOnTapListener.onLongPress(PopupZoomer.this, event);
} else {
mOnTapListener.onSingleTap(PopupZoomer.this, event);
}
hide(true);
}
return true;
}
};
mGestureDetector = new GestureDetector(context, listener);
}
public void setOnTapListener(OnTapListener listener) {
mOnTapListener = listener;
}
public void setOnVisibilityChangedListener(OnVisibilityChangedListener listener) {
mOnVisibilityChangedListener = listener;
}
public void setBitmap(Bitmap bitmap) {
if (mZoomedBitmap != null) {
mZoomedBitmap.recycle();
mZoomedBitmap = null;
}
mZoomedBitmap = bitmap;
Canvas canvas = new Canvas(mZoomedBitmap);
Path path = new Path();
RectF canvasRect = new RectF(0, 0, canvas.getWidth(), canvas.getHeight());
float overlayCornerRadius = getOverlayCornerRadius(getContext());
path.addRoundRect(canvasRect, overlayCornerRadius, overlayCornerRadius, Direction.CCW);
canvas.clipPath(path, Op.XOR);
Paint clearPaint = new Paint();
clearPaint.setXfermode(new PorterDuffXfermode(Mode.SRC));
clearPaint.setColor(Color.TRANSPARENT);
canvas.drawPaint(clearPaint);
}
private void scroll(float x, float y) {
mPopupScrollX = constrain(mPopupScrollX - x, mMinScrollX, mMaxScrollX);
mPopupScrollY = constrain(mPopupScrollY - y, mMinScrollY, mMaxScrollY);
invalidate();
}
private void startAnimation(boolean show) {
mAnimating = true;
mShowing = show;
mTimeLeft = 0;
if (show) {
setVisibility(VISIBLE);
mNeedsToInitDimensions = true;
if (mOnVisibilityChangedListener != null) {
mOnVisibilityChangedListener.onPopupZoomerShown(this);
}
} else {
long endTime = mAnimationStartTime + ANIMATION_DURATION;
mTimeLeft = endTime - SystemClock.uptimeMillis();
if (mTimeLeft < 0) mTimeLeft = 0;
}
mAnimationStartTime = SystemClock.uptimeMillis();
invalidate();
}
private void hideImmediately() {
mAnimating = false;
mShowing = false;
mTimeLeft = 0;
if (mOnVisibilityChangedListener != null) {
mOnVisibilityChangedListener.onPopupZoomerHidden(this);
}
setVisibility(INVISIBLE);
mZoomedBitmap.recycle();
mZoomedBitmap = null;
}
public boolean isShowing() {
return mShowing || mAnimating;
}
public void setLastTouch(float x, float y) {
mTouch.x = x;
mTouch.y = y;
}
private void setTargetBounds(Rect rect) {
mTargetBounds = rect;
}
private void initDimensions() {
if (mTargetBounds == null || mTouch == null) return;
mScale = (float) mZoomedBitmap.getWidth() / mTargetBounds.width();
float l = mTouch.x - mScale * (mTouch.x - mTargetBounds.left);
float t = mTouch.y - mScale * (mTouch.y - mTargetBounds.top);
float r = l + mZoomedBitmap.getWidth();
float b = t + mZoomedBitmap.getHeight();
mClipRect = new RectF(l, t, r, b);
int width = getWidth();
int height = getHeight();
mViewClipRect = new RectF(ZOOM_BOUNDS_MARGIN,
ZOOM_BOUNDS_MARGIN,
width - ZOOM_BOUNDS_MARGIN,
height - ZOOM_BOUNDS_MARGIN);
mShiftX = 0;
mShiftY = 0;
if (mClipRect.left < ZOOM_BOUNDS_MARGIN) {
mShiftX = ZOOM_BOUNDS_MARGIN - mClipRect.left;
mClipRect.left += mShiftX;
mClipRect.right += mShiftX;
} else if (mClipRect.right > width - ZOOM_BOUNDS_MARGIN) {
mShiftX = (width - ZOOM_BOUNDS_MARGIN - mClipRect.right);
mClipRect.right += mShiftX;
mClipRect.left += mShiftX;
}
if (mClipRect.top < ZOOM_BOUNDS_MARGIN) {
mShiftY = ZOOM_BOUNDS_MARGIN - mClipRect.top;
mClipRect.top += mShiftY;
mClipRect.bottom += mShiftY;
} else if (mClipRect.bottom > height - ZOOM_BOUNDS_MARGIN) {
mShiftY = height - ZOOM_BOUNDS_MARGIN - mClipRect.bottom;
mClipRect.bottom += mShiftY;
mClipRect.top += mShiftY;
}
mMinScrollX = mMaxScrollX = mMinScrollY = mMaxScrollY = 0;
if (mViewClipRect.right + mShiftX < mClipRect.right) {
mMinScrollX = mViewClipRect.right - mClipRect.right;
}
if (mViewClipRect.left + mShiftX > mClipRect.left) {
mMaxScrollX = mViewClipRect.left - mClipRect.left;
}
if (mViewClipRect.top + mShiftY > mClipRect.top) {
mMaxScrollY = mViewClipRect.top - mClipRect.top;
}
if (mViewClipRect.bottom + mShiftY < mClipRect.bottom) {
mMinScrollY = mViewClipRect.bottom - mClipRect.bottom;
}
mClipRect.intersect(mViewClipRect);
mLeftExtrusion = mTouch.x - mClipRect.left;
mRightExtrusion = mClipRect.right - mTouch.x;
mTopExtrusion = mTouch.y - mClipRect.top;
mBottomExtrusion = mClipRect.bottom - mTouch.y;
float percentX =
(mTouch.x - mTargetBounds.centerX()) / (mTargetBounds.width() / 2.f) + .5f;
float percentY =
(mTouch.y - mTargetBounds.centerY()) / (mTargetBounds.height() / 2.f) + .5f;
float scrollWidth = mMaxScrollX - mMinScrollX;
float scrollHeight = mMaxScrollY - mMinScrollY;
mPopupScrollX = scrollWidth * percentX * -1f;
mPopupScrollY = scrollHeight * percentY * -1f;
mPopupScrollX = constrain(mPopupScrollX, mMinScrollX, mMaxScrollX);
mPopupScrollY = constrain(mPopupScrollY, mMinScrollY, mMaxScrollY);
}
protected boolean acceptZeroSizeView() {
return false;
}
@Override
protected void onDraw(Canvas canvas) {
if (!isShowing() || mZoomedBitmap == null) return;
if (!acceptZeroSizeView() && (getWidth() == 0 || getHeight() == 0)) return;
if (mNeedsToInitDimensions) {
mNeedsToInitDimensions = false;
initDimensions();
}
canvas.save();
float time = (SystemClock.uptimeMillis() - mAnimationStartTime + mTimeLeft) /
((float) ANIMATION_DURATION);
time = constrain(time, 0, 1);
if (time >= 1) {
mAnimating = false;
if (!isShowing()) {
hideImmediately();
return;
}
} else {
invalidate();
}
float fractionAnimation;
if (mShowing) {
fractionAnimation = mShowInterpolator.getInterpolation(time);
} else {
fractionAnimation = mHideInterpolator.getInterpolation(time);
}
canvas.drawARGB((int) (80 * fractionAnimation), 0, 0, 0);
canvas.save();
float scale = fractionAnimation * (mScale - 1.0f) / mScale + 1.0f / mScale;
float unshiftX = -mShiftX * (1.0f - fractionAnimation) / mScale;
float unshiftY = -mShiftY * (1.0f - fractionAnimation) / mScale;
RectF rect = new RectF();
rect.left = mTouch.x - mLeftExtrusion * scale + unshiftX;
rect.top = mTouch.y - mTopExtrusion * scale + unshiftY;
rect.right = mTouch.x + mRightExtrusion * scale + unshiftX;
rect.bottom = mTouch.y + mBottomExtrusion * scale + unshiftY;
canvas.clipRect(rect);
canvas.scale(scale, scale, rect.left, rect.top);
canvas.translate(mPopupScrollX, mPopupScrollY);
canvas.drawBitmap(mZoomedBitmap, rect.left, rect.top, null);
canvas.restore();
Drawable overlayNineTile = getOverlayDrawable(getContext());
overlayNineTile.setBounds((int) rect.left - sOverlayPadding.left,
(int) rect.top - sOverlayPadding.top,
(int) rect.right + sOverlayPadding.right,
(int) rect.bottom + sOverlayPadding.bottom);
int alpha = constrain((int) (fractionAnimation * 255), 0, 255);
overlayNineTile.setAlpha(alpha);
overlayNineTile.draw(canvas);
canvas.restore();
}
public void show(Rect rect) {
if (mShowing || mZoomedBitmap == null) return;
setTargetBounds(rect);
startAnimation(true);
}
public void hide(boolean animation) {
if (!mShowing) return;
if (animation) {
startAnimation(false);
} else {
hideImmediately();
}
}
private PointF convertTouchPoint(float x, float y) {
x -= mShiftX;
y -= mShiftY;
x = mTouch.x + (x - mTouch.x - mPopupScrollX) / mScale;
y = mTouch.y + (y - mTouch.y - mPopupScrollY) / mScale;
return new PointF(x, y);
}
private boolean isTouchOutsideArea(float x, float y) {
return !mClipRect.contains(x, y);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
mGestureDetector.onTouchEvent(event);
return true;
}
private static class ReverseInterpolator implements Interpolator {
private final Interpolator mInterpolator;
public ReverseInterpolator(Interpolator i) {
mInterpolator = i;
}
@Override
public float getInterpolation(float input) {
input = 1.0f - input;
if (mInterpolator == null) return input;
return mInterpolator.getInterpolation(input);
}
}
}