gpt4 book ai didi

android - 如何将服务器图像 url 转换为 Drawable Int

转载 作者:行者123 更新时间:2023-12-04 15:06:28 25 4
gpt4 key购买 nike

我在服务器中有图像并正在使用 Picasso 库将其加载到 Android 应用程序中,我想放大和缩小应用程序中所有加载的图像。我提到了https://developer.android.com/training/animation/zoom#java此链接可放大 imageView。

在下面的代码中调用 zoomImageFromThumb 时,它们传递了 ImageView holder 和 Drawable int

请帮助我Convert Image URl to Drawable Int作为参数传递,如果还有其他解决方案,请告诉我。

提前致谢

加载图片代码

Picasso.get()
.load(model.getImage_path())
.error(R.drawable.not_found)
.into(image_IV)

调用 zoomImageFromThumb

        thumb1View.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
zoomImageFromThumb(thumb1View, R.drawable.image1);
}
});````

最佳答案

他们将 drawableId 作为 Int 并将其设置为 ImageView 内的 image 但在你的情况下你可以使用 picassoimage 加载到 Imageview 中,因此您只需传递 url 而不是 drawableId 进入 zoomImageFromThumb 方法,如下所示

 private fun zoomImageFromThumb(thumbView: View, imageUrl: String) {
// If there's an animation in progress, cancel it
// immediately and proceed with this one.
currentAnimator?.cancel()

//load image using picasso
Picasso.get()
.load(imageUrl)
.error(R.drawable.not_found)
.into(findViewById<ImageView>(R.id.expanded_image))

// Calculate the starting and ending bounds for the zoomed-in image.
// This step involves lots of math. Yay, math.
val startBoundsInt = Rect()
val finalBoundsInt = Rect()
val globalOffset = Point()

// The start bounds are the global visible rectangle of the thumbnail,
// and the final bounds are the global visible rectangle of the container
// view. Also set the container view's offset as the origin for the
// bounds, since that's the origin for the positioning animation
// properties (X, Y).
thumbView.getGlobalVisibleRect(startBoundsInt)
findViewById<View>(R.id.container)
.getGlobalVisibleRect(finalBoundsInt, globalOffset)
startBoundsInt.offset(-globalOffset.x, -globalOffset.y)
finalBoundsInt.offset(-globalOffset.x, -globalOffset.y)

val startBounds = RectF(startBoundsInt)
val finalBounds = RectF(finalBoundsInt)

// Adjust the start bounds to be the same aspect ratio as the final
// bounds using the "center crop" technique. This prevents undesirable
// stretching during the animation. Also calculate the start scaling
// factor (the end scaling factor is always 1.0).
val startScale: Float
if ((finalBounds.width() / finalBounds.height() > startBounds.width() / startBounds.height())) {
// Extend start bounds horizontally
startScale = startBounds.height() / finalBounds.height()
val startWidth: Float = startScale * finalBounds.width()
val deltaWidth: Float = (startWidth - startBounds.width()) / 2
startBounds.left -= deltaWidth.toInt()
startBounds.right += deltaWidth.toInt()
} else {
// Extend start bounds vertically
startScale = startBounds.width() / finalBounds.width()
val startHeight: Float = startScale * finalBounds.height()
val deltaHeight: Float = (startHeight - startBounds.height()) / 2f
startBounds.top -= deltaHeight.toInt()
startBounds.bottom += deltaHeight.toInt()
}

// Hide the thumbnail and show the zoomed-in view. When the animation
// begins, it will position the zoomed-in view in the place of the
// thumbnail.
thumbView.alpha = 0f
expandedImageView.visibility = View.VISIBLE

// Set the pivot point for SCALE_X and SCALE_Y transformations
// to the top-left corner of the zoomed-in view (the default
// is the center of the view).
expandedImageView.pivotX = 0f
expandedImageView.pivotY = 0f

// Construct and run the parallel animation of the four translation and
// scale properties (X, Y, SCALE_X, and SCALE_Y).
currentAnimator = AnimatorSet().apply {
play(ObjectAnimator.ofFloat(
expandedImageView,
View.X,
startBounds.left,
finalBounds.left)
).apply {
with(ObjectAnimator.ofFloat(expandedImageView, View.Y, startBounds.top, finalBounds.top))
with(ObjectAnimator.ofFloat(expandedImageView, View.SCALE_X, startScale, 1f))
with(ObjectAnimator.ofFloat(expandedImageView, View.SCALE_Y, startScale, 1f))
}
duration = shortAnimationDuration.toLong()
interpolator = DecelerateInterpolator()
addListener(object : AnimatorListenerAdapter() {

override fun onAnimationEnd(animation: Animator) {
currentAnimator = null
}

override fun onAnimationCancel(animation: Animator) {
currentAnimator = null
}
})
start()
}

// Upon clicking the zoomed-in image, it should zoom back down
// to the original bounds and show the thumbnail instead of
// the expanded image.
expandedImageView.setOnClickListener {
currentAnimator?.cancel()

// Animate the four positioning/sizing properties in parallel,
// back to their original values.
currentAnimator = AnimatorSet().apply {
play(ObjectAnimator.ofFloat(expandedImageView, View.X, startBounds.left)).apply {
with(ObjectAnimator.ofFloat(expandedImageView, View.Y, startBounds.top))
with(ObjectAnimator.ofFloat(expandedImageView, View.SCALE_X, startScale))
with(ObjectAnimator.ofFloat(expandedImageView, View.SCALE_Y, startScale))
}
duration = shortAnimationDuration.toLong()
interpolator = DecelerateInterpolator()
addListener(object : AnimatorListenerAdapter() {

override fun onAnimationEnd(animation: Animator) {
thumbView.alpha = 1f
expandedImageView.visibility = View.GONE
currentAnimator = null
}

override fun onAnimationCancel(animation: Animator) {
thumbView.alpha = 1f
expandedImageView.visibility = View.GONE
currentAnimator = null
}
})
start()
}
}
}

关于android - 如何将服务器图像 url 转换为 Drawable Int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65987512/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com