Skip to content

Commit

Permalink
fix: transition
Browse files Browse the repository at this point in the history
  • Loading branch information
SherlockGougou committed Jan 2, 2025
1 parent 7fbdf5d commit 6b521c4
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ class ImagePreviewActivity : AppCompatActivity(), Handler.Callback, View.OnClick

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
overrideActivityTransition(OVERRIDE_TRANSITION_OPEN, R.anim.fade_in, R.anim.fade_out)
} else {
Expand All @@ -109,15 +110,10 @@ class ImagePreviewActivity : AppCompatActivity(), Handler.Callback, View.OnClick
imageInfoList.clear()
imageInfoList.addAll(ImagePreview.instance.getImageInfoList())
if (imageInfoList.isEmpty()) {
onBackPressed()
finish()
return
}

// 设置共享元素过渡
supportPostponeEnterTransition()
// 启动共享元素过渡
supportStartPostponedEnterTransition()

// 回调
ImagePreview.instance.setOnFinishListener(this)

Expand All @@ -143,15 +139,6 @@ class ImagePreviewActivity : AppCompatActivity(), Handler.Callback, View.OnClick
fmImageShowOriginContainer = findViewById(R.id.fm_image_show_origin_container)
fmCenterProgressContainer = findViewById(R.id.fm_center_progress_container)

setAlpha(0f)
// setAlpha,从0到1
handlerHolder.postDelayed({
setAlpha(1f)
}, 300)
handlerHolder.postDelayed({
consControllerOverlay.visibility = View.VISIBLE
}, 600)

// 顶部和底部margin
refreshUIMargin()

Expand Down Expand Up @@ -302,11 +289,6 @@ class ImagePreviewActivity : AppCompatActivity(), Handler.Callback, View.OnClick
ImagePreview.instance.bigImagePageChangeListener?.onPageScrollStateChanged(state)
}
})

// 如果当前第一个就是视频类型,需要手动调用一次
// if (imageInfoList[currentItem].type == Type.VIDEO) {
// fragmentList[currentItem].onSelected()
// }
}

private fun refreshUIMargin() {
Expand Down Expand Up @@ -359,13 +341,21 @@ class ImagePreviewActivity : AppCompatActivity(), Handler.Callback, View.OnClick
}
}

override fun onFinish() {
onBackPressed()
}

override fun onBackPressed() {
finish()
}

override fun finish() {
super.finish()
for (fragment in fragmentList) {
fragment.onRelease()
}
ImagePreview.instance.onPageFinishListener?.onFinish(this)
ImagePreview.instance.reset()
super.finish()
}

override fun onDestroy() {
Expand Down Expand Up @@ -518,7 +508,7 @@ class ImagePreviewActivity : AppCompatActivity(), Handler.Callback, View.OnClick
} else if (i == R.id.btn_show_origin) {
handlerHolder.sendEmptyMessage(0)
} else if (i == R.id.imgCloseButton) {
onBackPressed()
finish()
}
}

Expand Down Expand Up @@ -652,10 +642,6 @@ class ImagePreviewActivity : AppCompatActivity(), Handler.Callback, View.OnClick
decorView.systemUiVisibility = option
}

override fun onFinish() {
onBackPressed()
}

companion object {
fun activityStart(context: Activity) {
val intent = Intent()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.view.ViewGroup.MarginLayoutParams
Expand Down Expand Up @@ -59,6 +60,8 @@ import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.Target
import java.io.File
import java.util.Locale
import kotlin.compareTo
import kotlin.div
import kotlin.math.abs

/**
Expand Down Expand Up @@ -142,29 +145,35 @@ class ImagePreviewFragment : Fragment() {
val phoneHei = getPhoneHei(imagePreviewActivity.applicationContext)
// 手势拖拽事件
if (ImagePreview.instance.isEnableDragClose) {
dragCloseView.setOnAlphaChangeListener { event, translationY ->
if (translationY > 0) {
ImagePreview.instance.onPageDragListener?.onDrag(imagePreviewActivity, imagePreviewActivity.parentView, event, translationY)
} else {
ImagePreview.instance.onPageDragListener?.onDragEnd(imagePreviewActivity, imagePreviewActivity.parentView)
}
val yAbs = abs(translationY)
val percent = yAbs / phoneHei
val number = 1.0f - percent
imagePreviewActivity.setAlpha(number)
if (imageAnim.visibility == View.VISIBLE) {
imageAnim.scaleY = number
imageAnim.scaleX = number
}
if (imageStatic.visibility == View.VISIBLE) {
imageStatic.scaleY = number
imageStatic.scaleX = number
dragCloseView.setOnAlphaChangeListener(object : DragCloseView.onAlphaChangedListener {
override fun onTranslationYChanged(event: MotionEvent?, translationY: Float) {
if (translationY > 0) {
ImagePreview.instance.onPageDragListener?.onDrag(imagePreviewActivity, imagePreviewActivity.parentView, event, translationY)
} else {
ImagePreview.instance.onPageDragListener?.onDragEnd(imagePreviewActivity, imagePreviewActivity.parentView)
}
val yAbs = abs(translationY)
val percent = yAbs / phoneHei
val number = 1.0f - percent
imagePreviewActivity.setAlpha(number)
if (imageAnim.visibility == View.VISIBLE) {
imageAnim.scaleY = number
imageAnim.scaleX = number
}
if (imageStatic.visibility == View.VISIBLE) {
imageStatic.scaleY = number
imageStatic.scaleX = number
}
if (videoView.visibility == View.VISIBLE) {
videoView.scaleY = number
videoView.scaleX = number
}
}
if (videoView.visibility == View.VISIBLE) {
videoView.scaleY = number
videoView.scaleX = number

override fun onExit() {
imagePreviewActivity.setAlpha(0f)
}
}
})
}
// 点击事件(视频类型不支持点击关闭)
imageStatic.setOnClickListener { v ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,19 @@ private void onActionUp() {
boolean enableUpDragClose = ImagePreview.Companion.getInstance().isEnableUpDragClose();
if (enableUpDragClose) {
if (Math.abs(mTranslationY) > MAX_EXIT_Y) {
exit(mTranslationY);
if (null != mOnAlphaChangedListener) {
mOnAlphaChangedListener.onExit();
}
exit();
} else {
resetCallBackAnimation();
}
} else {
if (mTranslationY > MAX_EXIT_Y) {
exit(mTranslationY);
if (null != mOnAlphaChangedListener) {
mOnAlphaChangedListener.onExit();
}
exit();
} else {
resetCallBackAnimation();
}
Expand All @@ -176,10 +182,9 @@ public void onAnimationEnd(@NonNull Animator animation) {
animatorY.start();
}

private void exit(float currentY) {
// 默认的退出动画
private void exit() {
ValueAnimator animatorExit;
if (currentY > 0) {
if (mTranslationY > 0) {
animatorExit = ValueAnimator.ofFloat(mTranslationY, getHeight());
} else {
animatorExit = ValueAnimator.ofFloat(mTranslationY, -getHeight());
Expand All @@ -198,7 +203,6 @@ public void onAnimationStart(@NonNull Animator animation) {

@Override
public void onAnimationEnd(@NonNull Animator animation) {
reset();
((Activity) getContext()).finish();
}

Expand Down Expand Up @@ -245,5 +249,7 @@ public void onAnimationRepeat(@NonNull Animator animation) {
public interface onAlphaChangedListener {

void onTranslationYChanged(MotionEvent event, float translationY);

void onExit();
}
}
8 changes: 0 additions & 8 deletions library/src/main/res/anim/fade_in.xml
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/decelerate_interpolator">
<scale
android:duration="300"
android:fromXScale="0"
android:fromYScale="0"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="1.0"
android:toYScale="1.0" />
<alpha
android:duration="300"
android:fromAlpha="0.0"
Expand Down
8 changes: 0 additions & 8 deletions library/src/main/res/anim/fade_out.xml
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/decelerate_interpolator">
<scale
android:duration="300"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="0"
android:toYScale="0" />
<alpha
android:duration="300"
android:fromAlpha="1.0"
Expand Down
5 changes: 2 additions & 3 deletions library/src/main/res/layout/sh_layout_preview.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
android:id="@+id/rootView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00000000">
android:background="#000000">

<cc.shinichi.library.view.HackyViewPager
android:id="@+id/viewPager"
Expand All @@ -26,8 +26,7 @@
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/consControllerOverlay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone">
android:layout_height="match_parent">

<TextView
android:id="@+id/tv_indicator"
Expand Down

0 comments on commit 6b521c4

Please sign in to comment.