From 3575c856b41497995e002a178a0620a44a9192a0 Mon Sep 17 00:00:00 2001 From: seven332 Date: Wed, 24 Feb 2016 01:55:01 +0800 Subject: [PATCH] Add alpha animation for ImageView --- .../hippo/ehviewer/gallery/gl/ImageView.java | 17 +++++++++++++++++ .../ehviewer/gallery/gl/PagerLayoutManager.java | 10 +++++----- .../gallery/gl/ScrollLayoutManager.java | 4 ++-- .../com/hippo/gl/glrenderer/GLES20Canvas.java | 9 ++++++--- app/src/main/java/com/hippo/gl/view/GLView.java | 15 +++++++++++++++ 5 files changed, 45 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/com/hippo/ehviewer/gallery/gl/ImageView.java b/app/src/main/java/com/hippo/ehviewer/gallery/gl/ImageView.java index 103c30c08..776c2ad52 100644 --- a/app/src/main/java/com/hippo/ehviewer/gallery/gl/ImageView.java +++ b/app/src/main/java/com/hippo/ehviewer/gallery/gl/ImageView.java @@ -20,6 +20,8 @@ import android.graphics.Rect; import android.graphics.RectF; import android.support.annotation.IntDef; +import com.hippo.anani.AnimationUtils; +import com.hippo.gl.anim.AlphaAnimation; import com.hippo.gl.glrenderer.GLCanvas; import com.hippo.gl.glrenderer.Texture; import com.hippo.gl.view.GLView; @@ -56,6 +58,8 @@ public class ImageView extends GLView { public static final int START_POSITION_BOTTOM_RIGHT = 3; public static final int START_POSITION_CENTER = 4; + private static final long ALPHA_ANIMATION_DURING = 300L; + private Texture mTexture; private int mTextureWidth; private int mTextureHeight; @@ -74,6 +78,14 @@ public class ImageView extends GLView { private boolean mScaleOffsetDirty = true; private boolean mPositionInRootDirty = true; + private final AlphaAnimation mAlphaAnimation; + + public ImageView() { + mAlphaAnimation = new AlphaAnimation(0.0f, 1.0f); + mAlphaAnimation.setDuration(ALPHA_ANIMATION_DURING); + mAlphaAnimation.setInterpolator(AnimationUtils.FAST_SLOW_INTERPOLATOR); + } + @Scale public static int sanitizeScaleMode(int scaleMode) { if (scaleMode != SCALE_ORIGIN && @@ -193,6 +205,11 @@ public class ImageView extends GLView { if (mTextureHeight <= 0) { mTextureHeight = 1; } + + // Start alpha animation + if (isAttachedToRoot()) { + startAnimation(mAlphaAnimation, true); + } } else { mTextureWidth = 1; mTextureHeight = 1; diff --git a/app/src/main/java/com/hippo/ehviewer/gallery/gl/PagerLayoutManager.java b/app/src/main/java/com/hippo/ehviewer/gallery/gl/PagerLayoutManager.java index 93108368c..05c46b551 100644 --- a/app/src/main/java/com/hippo/ehviewer/gallery/gl/PagerLayoutManager.java +++ b/app/src/main/java/com/hippo/ehviewer/gallery/gl/PagerLayoutManager.java @@ -361,20 +361,20 @@ public class PagerLayoutManager extends GalleryView.LayoutManager { // Ensure pages if (mCurrent == null) { mCurrent = obtainPage(); - adapter.bind(mCurrent, index); galleryView.addComponent(mCurrent); + adapter.bind(mCurrent, index); } if (mPrevious == null && index > 0) { mPrevious = obtainPage(); - adapter.bind(mPrevious, index - 1); galleryView.addComponent(mPrevious); + adapter.bind(mPrevious, index - 1); } else if (mPrevious != null && index == 0) { removePage(mPrevious); } if (mNext == null && index < size - 1) { mNext = obtainPage(); - adapter.bind(mNext, index + 1); galleryView.addComponent(mNext); + adapter.bind(mNext, index + 1); } else if (mNext != null && index == size - 1) { removePage(mNext); } @@ -480,8 +480,8 @@ public class PagerLayoutManager extends GalleryView.LayoutManager { if (mIndex > 0) { mPrevious = obtainPage(); - mAdapter.bind(mPrevious, mIndex - 1); mGalleryView.addComponent(mPrevious); + mAdapter.bind(mPrevious, mIndex - 1); } } @@ -502,8 +502,8 @@ public class PagerLayoutManager extends GalleryView.LayoutManager { if (mIndex < size - 1) { mNext = obtainPage(); - adapter.bind(mNext, mIndex + 1); mGalleryView.addComponent(mNext); + adapter.bind(mNext, mIndex + 1); } } diff --git a/app/src/main/java/com/hippo/ehviewer/gallery/gl/ScrollLayoutManager.java b/app/src/main/java/com/hippo/ehviewer/gallery/gl/ScrollLayoutManager.java index 5f0b552e3..aca3210ad 100644 --- a/app/src/main/java/com/hippo/ehviewer/gallery/gl/ScrollLayoutManager.java +++ b/app/src/main/java/com/hippo/ehviewer/gallery/gl/ScrollLayoutManager.java @@ -192,8 +192,8 @@ public class ScrollLayoutManager extends GalleryView.LayoutManager { mIndex--; GalleryPageView page = obtainPage(); - mAdapter.bind(page, mIndex); galleryView.addComponent(page); + mAdapter.bind(page, mIndex); mPages.add(0, page); // Add interval @@ -239,8 +239,8 @@ public class ScrollLayoutManager extends GalleryView.LayoutManager { pages = null; // New page page = obtainPage(); - mAdapter.bind(page, index); galleryView.addComponent(page); + mAdapter.bind(page, index); mPages.add(page); } else { page = pages.next(); diff --git a/app/src/main/java/com/hippo/gl/glrenderer/GLES20Canvas.java b/app/src/main/java/com/hippo/gl/glrenderer/GLES20Canvas.java index 61308c61a..d358a5a46 100644 --- a/app/src/main/java/com/hippo/gl/glrenderer/GLES20Canvas.java +++ b/app/src/main/java/com/hippo/gl/glrenderer/GLES20Canvas.java @@ -16,6 +16,7 @@ package com.hippo.gl.glrenderer; import android.graphics.Bitmap; +import android.graphics.Color; import android.graphics.Rect; import android.graphics.RectF; import android.opengl.GLES20; @@ -838,9 +839,11 @@ public class GLES20Canvas implements GLCanvas { setAlpha(textureAlpha); drawTexture(texture, source, target); - float colorAlpha = cappedRatio * currentAlpha; - setAlpha(colorAlpha); - fillRect(target.left, target.top, target.width(), target.height(), toColor); + if (0 != Color.alpha(toColor)) { + float colorAlpha = cappedRatio * currentAlpha; + setAlpha(colorAlpha); + fillRect(target.left, target.top, target.width(), target.height(), toColor); + } restore(); } diff --git a/app/src/main/java/com/hippo/gl/view/GLView.java b/app/src/main/java/com/hippo/gl/view/GLView.java index 7fc6a50ad..d8f205b57 100644 --- a/app/src/main/java/com/hippo/gl/view/GLView.java +++ b/app/src/main/java/com/hippo/gl/view/GLView.java @@ -153,6 +153,21 @@ public class GLView implements TouchOwner { private OnClickListener mOnClickListener; private OnLongClickListener mOnLongClickListener; + public void startAnimation(CanvasAnimation animation, boolean atOnce) { + GLRoot root = getGLRoot(); + if (root == null) throw new IllegalStateException(); + mAnimation = animation; + if (mAnimation != null) { + mAnimation.start(); + if (atOnce) { + mAnimation.setStartTime(AnimationTime.get()); + } else { + root.registerLaunchedAnimation(mAnimation); + } + } + invalidate(); + } + /** * Set the enabled state of this view. *