Android piccaso缩放小图像以适应大图像视图而不影响图像质量

时间:2018-03-14 10:21:21

标签: android android-imageview picasso

我已经看过许多缩小大图像缩放的例子,并且它很小。但我的问题是如何在不损失其质量的情况下增加图像尺寸以适应特定高度。下面是我试图实现的代码:

  [![Picasso.with(context).load(context.getResources().getString(R.string.host) + aiAndroidResponse.getImage_url()).into(new Target() {
                    @Override
                    public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom loadedFrom) {

                        //whatever algorithm here to compute size
                        float ratio = (float) bitmap.getHeight() / (float) bitmap.getWidth();
                        float heightFloat = ((float) role_image.getWidth()) * ratio;

                        final android.view.ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) role_image.getLayoutParams();

                        layoutParams.height = (int) heightFloat;
                        layoutParams.width = (int) role_image.getWidth();
                        role_image.setLayoutParams(layoutParams);
                        role_image.setImageBitmap(bitmap);
                    }

                    @Override
                    public void onBitmapFailed(Drawable errorDrawable) {

                    }

                    @Override
                    public void onPrepareLoad(Drawable placeHolderDrawable) {

                    }


                });][1]][1]
下面的

是线性布局垂直的xml文件:

 <ImageView
                android:id="@+id/role_image"
                android:layout_width="100dp"
                android:layout_height="match_parent"
                android:src="@drawable/profile_default" />

请看下面这张图片在android屏幕上看到模糊

enter image description here

1 个答案:

答案 0 :(得分:1)

  

如何在不损失质量的情况下增加图像尺寸以适应特定高度

这当然是不可能的。