Skip to content
This repository has been archived by the owner on Oct 13, 2023. It is now read-only.

Commit

Permalink
Merge pull request #110 from sceneren/master
Browse files Browse the repository at this point in the history
Fix #104,#98
  • Loading branch information
mikaelzero authored May 24, 2022
2 parents a731c20 + 3a6240d commit c089c19
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 55 deletions.
1 change: 0 additions & 1 deletion local.properties
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@

18 changes: 14 additions & 4 deletions mojito/src/main/java/net/mikaelzero/mojito/MojitoView.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Color;
import android.content.res.Configuration;
import android.graphics.Rect;
import android.graphics.RectF;
import android.os.Build;
Expand Down Expand Up @@ -46,7 +46,7 @@ public class MojitoView extends FrameLayout {
private float mMoveDownTranslateY;
private float mTranslateX;

private final float MAX_TRANSLATE_Y;
private float MAX_TRANSLATE_Y;

FrameLayout contentLayout;
View backgroundView;
Expand All @@ -57,8 +57,8 @@ public class MojitoView extends FrameLayout {
private int mOriginHeight;
private int mOriginWidth;

private final int screenWidth;
private final int screenHeight;
private int screenWidth;
private int screenHeight;
private int targetImageTop;
private int targetImageWidth;
private int targetImageHeight;
Expand Down Expand Up @@ -99,6 +99,7 @@ public MojitoView(Context context, AttributeSet attrs, int defStyleAttr) {
screenWidth = ScreenUtils.getScreenWidth(context);
screenHeight = Mojito.mojitoConfig().transparentNavigationBar() ? ScreenUtils.getScreenHeight(context) : ScreenUtils.getAppScreenHeight(context);
MAX_TRANSLATE_Y = screenHeight * Mojito.mojitoConfig().maxTransYRatio();
Log.e("MojitoView", "screenWidth = " + screenWidth + " screenHeight = " + screenHeight + " MAX_TRANSLATE_Y = " + MAX_TRANSLATE_Y);

addView(LayoutInflater.from(getContext()).inflate(R.layout.layout_content, null), 0);
contentLayout = findViewById(R.id.contentLayout);
Expand All @@ -107,6 +108,15 @@ public MojitoView(Context context, AttributeSet attrs, int defStyleAttr) {
imageWrapper = new MarginViewWrapper(contentLayout);
}

@Override
protected void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
screenWidth = ScreenUtils.getScreenWidth(getContext());
screenHeight = Mojito.mojitoConfig().transparentNavigationBar() ? ScreenUtils.getScreenHeight(getContext()) : ScreenUtils.getAppScreenHeight(getContext());
MAX_TRANSLATE_Y = screenHeight * Mojito.mojitoConfig().maxTransYRatio();
Log.e("MojitoView==>2", "screenWidth = " + screenWidth + " screenHeight = " + screenHeight + " MAX_TRANSLATE_Y = " + MAX_TRANSLATE_Y);
}

public void showWithoutView(int realWidth, int realHeight, boolean showImmediately) {
this.realWidth = realWidth;
this.realHeight = realHeight;
Expand Down
154 changes: 104 additions & 50 deletions mojito/src/main/java/net/mikaelzero/mojito/ui/ImageMojitoFragment.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.mikaelzero.mojito.ui

import android.content.res.Configuration
import android.graphics.BitmapFactory
import android.net.Uri
import android.os.Bundle
Expand Down Expand Up @@ -38,7 +39,11 @@ class ImageMojitoFragment : Fragment(), IMojitoFragment, OnMojitoViewCallback {
private var iProgress: IProgress? = null
private var fragmentCoverLoader: FragmentCoverLoader? = null

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = FragmentImageBinding.inflate(inflater, container, false)
return binding.root
}
Expand All @@ -59,7 +64,10 @@ class ImageMojitoFragment : Fragment(), IMojitoFragment, OnMojitoViewCallback {
}
fragmentCoverLoader = ImageMojitoActivity.fragmentCoverLoader?.providerInstance()
binding.imageCoverLayout.removeAllViews()
val fragmentCoverAttachView = fragmentCoverLoader?.attach(this, fragmentConfig.targetUrl == null || fragmentConfig.autoLoadTarget)
val fragmentCoverAttachView = fragmentCoverLoader?.attach(
this,
fragmentConfig.targetUrl == null || fragmentConfig.autoLoadTarget
)
if (fragmentCoverAttachView != null) {
binding.imageCoverLayout.visibility = View.VISIBLE
binding.imageCoverLayout.addView(fragmentCoverAttachView)
Expand All @@ -75,12 +83,16 @@ class ImageMojitoFragment : Fragment(), IMojitoFragment, OnMojitoViewCallback {
if (ImageMojitoActivity.hasShowedAnimMap[fragmentConfig.position] == true) 1f else if (fragmentConfig.showImmediately) 1f else 0f
)
binding.mojitoView.setOnMojitoViewCallback(this)
binding.mojitoView.setContentLoader(contentLoader, fragmentConfig.originUrl, fragmentConfig.targetUrl)
binding.mojitoView.setContentLoader(
contentLoader,
fragmentConfig.originUrl,
fragmentConfig.targetUrl
)
showView = contentLoader?.providerRealView()

contentLoader?.onTapCallback(object : OnTapCallback {
override fun onTap(view: View, x: Float, y: Float) {
binding.mojitoView.backToMin()
backToMin()
ImageMojitoActivity.onMojitoListener?.onClick(view, x, y, fragmentConfig.position)
}
})
Expand All @@ -91,10 +103,21 @@ class ImageMojitoFragment : Fragment(), IMojitoFragment, OnMojitoViewCallback {
contentLoader?.onLongTapCallback(object : OnLongTapCallback {
override fun onLongTap(view: View, x: Float, y: Float) {
if (!binding.mojitoView.isDrag) {
ImageMojitoActivity.onMojitoListener?.onLongClick(activity, view, x, y, fragmentConfig.position)
ImageMojitoActivity.onMojitoListener?.onLongClick(
activity,
view,
x,
y,
fragmentConfig.position
)
}
}
})

loadImage()
}

private fun loadImage() {
val isFile: Boolean = File(fragmentConfig.originUrl).isFile
val uri = if (isFile) {
Uri.fromFile(File(fragmentConfig.originUrl))
Expand All @@ -117,25 +140,43 @@ class ImageMojitoFragment : Fragment(), IMojitoFragment, OnMojitoViewCallback {
if (isDetached || context == null) {
return@post
}
startAnim(ScreenUtils.getScreenWidth(context), ScreenUtils.getScreenHeight(context), originLoadFail = true, needLoadImageUrl = fragmentConfig.originUrl)
startAnim(
ScreenUtils.getScreenWidth(context),
ScreenUtils.getScreenHeight(context),
originLoadFail = true,
needLoadImageUrl = fragmentConfig.originUrl
)
}
}
})
}

override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig)
loadImage()
}

private fun startAnim(image: File) {
val realSizes = getRealSizeFromFile(image)
startAnim(realSizes[0], realSizes[1])
}


private fun startAnim(w: Int, h: Int, originLoadFail: Boolean = false, needLoadImageUrl: String = "") {
private fun startAnim(
w: Int,
h: Int,
originLoadFail: Boolean = false,
needLoadImageUrl: String = ""
) {
if (!fragmentConfig.showImmediately) {
ImageMojitoActivity.onMojitoListener?.onStartAnim(fragmentConfig.position)
}
if (fragmentConfig.viewParams == null) {
binding.mojitoView.showWithoutView(w, h, if (ImageMojitoActivity.hasShowedAnimMap[fragmentConfig.position] == true) true else fragmentConfig.showImmediately)
binding.mojitoView.showWithoutView(
w,
h,
if (ImageMojitoActivity.hasShowedAnimMap[fragmentConfig.position] == true) true else fragmentConfig.showImmediately
)
} else {
binding.mojitoView.putData(
fragmentConfig.viewParams!!.getLeft(), fragmentConfig.viewParams!!.getTop(),
Expand All @@ -148,11 +189,15 @@ class ImageMojitoFragment : Fragment(), IMojitoFragment, OnMojitoViewCallback {
val targetEnable = if (ImageMojitoActivity.multiContentLoader == null) {
true
} else {
ImageMojitoActivity.multiContentLoader?.providerEnableTargetLoad(fragmentConfig.position) ?: false
ImageMojitoActivity.multiContentLoader?.providerEnableTargetLoad(fragmentConfig.position)
?: false
}
//查看原图的情况下 如果缩略图加载失败了 需要先加载缩略图 再根据条件判断是否要去加载原图
if (originLoadFail && needLoadImageUrl.isNotEmpty()) {
loadImageWithoutCache(needLoadImageUrl, fragmentConfig.targetUrl != null && targetEnable)
loadImageWithoutCache(
needLoadImageUrl,
fragmentConfig.targetUrl != null && targetEnable
)
} else if (fragmentConfig.targetUrl != null && targetEnable) {
replaceImageUrl(fragmentConfig.targetUrl!!)
} else if (needLoadImageUrl.isNotEmpty()) {
Expand All @@ -174,61 +219,69 @@ class ImageMojitoFragment : Fragment(), IMojitoFragment, OnMojitoViewCallback {
} else {
!fragmentConfig.autoLoadTarget
}
mImageLoader?.loadImage(showView.hashCode(), Uri.parse(url), onlyRetrieveFromCache, object : DefaultImageCallback() {
override fun onStart() {
handleImageOnStart()
}
mImageLoader?.loadImage(
showView.hashCode(),
Uri.parse(url),
onlyRetrieveFromCache,
object : DefaultImageCallback() {
override fun onStart() {
handleImageOnStart()
}

override fun onProgress(progress: Int) {
handleImageOnProgress(progress)
}
override fun onProgress(progress: Int) {
handleImageOnProgress(progress)
}

override fun onFail(error: Exception?) {
loadImageFail(onlyRetrieveFromCache)
}
override fun onFail(error: Exception?) {
loadImageFail(onlyRetrieveFromCache)
}

override fun onSuccess(image: File) {
mainHandler.post {
if (isDetached || context == null) {
return@post
override fun onSuccess(image: File) {
mainHandler.post {
if (isDetached || context == null) {
return@post
}
handleImageOnSuccess(image)
}
handleImageOnSuccess(image)
}
}
})
})
}

/**
* 如果图片还未加载出来 则加载图片 最后通知修改宽高
*/
private fun loadImageWithoutCache(url: String, needHandleTarget: Boolean = false) {
mImageLoader?.loadImage(showView.hashCode(), Uri.parse(url), false, object : DefaultImageCallback() {
override fun onStart() {
handleImageOnStart()
}
mImageLoader?.loadImage(
showView.hashCode(),
Uri.parse(url),
false,
object : DefaultImageCallback() {
override fun onStart() {
handleImageOnStart()
}

override fun onProgress(progress: Int) {
handleImageOnProgress(progress)
}
override fun onProgress(progress: Int) {
handleImageOnProgress(progress)
}

override fun onFail(error: Exception?) {
loadImageFail(false)
}
override fun onFail(error: Exception?) {
loadImageFail(false)
}

override fun onSuccess(image: File) {
mainHandler.post {
if (isDetached || context == null) {
return@post
}
handleImageOnSuccess(image)
val realSizes = getRealSizeFromFile(image)
binding.mojitoView.resetSize(realSizes[0], realSizes[1])
if (needHandleTarget) {
replaceImageUrl(fragmentConfig.targetUrl!!)
override fun onSuccess(image: File) {
mainHandler.post {
if (isDetached || context == null) {
return@post
}
handleImageOnSuccess(image)
val realSizes = getRealSizeFromFile(image)
binding.mojitoView.resetSize(realSizes[0], realSizes[1])
if (needHandleTarget) {
replaceImageUrl(fragmentConfig.targetUrl!!)
}
}
}
}
})
})
}

private fun handleImageOnStart() {
Expand Down Expand Up @@ -288,7 +341,8 @@ class ImageMojitoFragment : Fragment(), IMojitoFragment, OnMojitoViewCallback {

private fun loadImageFail(onlyRetrieveFromCache: Boolean) {
if (!onlyRetrieveFromCache) {
val errorDrawableResId = if (fragmentConfig.errorDrawableResId != 0) fragmentConfig.errorDrawableResId else mojitoConfig().errorDrawableResId()
val errorDrawableResId =
if (fragmentConfig.errorDrawableResId != 0) fragmentConfig.errorDrawableResId else mojitoConfig().errorDrawableResId()
if (errorDrawableResId != 0) {
mViewLoadFactory?.loadContentFail(showView!!, errorDrawableResId)
}
Expand Down

0 comments on commit c089c19

Please sign in to comment.