如何将服务器图像URL转换为Drawable Int

湿婆

我在服务器中有图像,并且正在使用Picasso库将其加载到android应用中,我想放大和缩小应用中所有已加载的图像。我将https://developer.android.com/training/animation/zoom#java指向此链接放大图像视图。

在下面的代码中,当调用zoomImageFromThumb时,它们正在传递ImageViewholder和Drawable int

请帮助我将Image URl转换为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);
            }
        });````
 
rajan.kali

他们将drawableIdas作为参数Int并将其设置为image内部,ImageView但是在您的情况下,您可以使用picasso加载image到中Imageview,因此您可以像下面这样传递inturl而不是drawableIdintozoomImageFromThumb方法

 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()
            }
        }
    }

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何将int转换为drawable?

来自分类Dev

如何将List <KeyValuePair <int,int >>转换为int [] []?

来自分类Dev

如何将interface {}转换为[] int?

来自分类Dev

如何将字节转换为int?

来自分类Dev

如何将InputStream转换为int

来自分类Dev

如何将char []转换为int?

来自分类Dev

如何将int转换为String?

来自分类Dev

如何将char转换为int?

来自分类Dev

如何将“ int”转换为“ objectParameter”?

来自分类Dev

如何将nvarchar转换为int

来自分类Dev

如何将键值转换为int?

来自分类Dev

如何将int转换为id?

来自分类Dev

如何将IntPtr转换为int

来自分类Dev

如何将str转换为int?

来自分类Dev

如何将字节转换为int?

来自分类Dev

如何将TextView转换为int?

来自分类Dev

如何将int转换为String?

来自分类Dev

如何将char转换为int?

来自分类Dev

如何将int转换为DateTime

来自分类Dev

如何将int转换为字节?

来自分类Dev

如何将BufferedImage转换为int []?

来自分类Dev

如何将 Sysdate 转换为 Int?

来自分类Dev

如何将str转换为int

来自分类Dev

如何将int转换为int8_t?

来自分类Dev

如何将vector <int>转换为int *

来自分类Dev

如何将int列表转换为Nullable <int>列表?

来自分类Dev

如何将“ void(MyClass :: *)(int)”转换为“ void(*)(int)”?

来自分类Dev

如何将int []类型转换为int?[]

来自分类Dev

如何将int转换为List <int>