我使用 Glide 库将图像加载到 imageView 中,但我不知道如何将图像缩放为可缩放

I used Glide library to load image into imageView and I don't know how to make image pinch to zoomable

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    //Variables
    ImageView imageView;
    imageView = (ImageView) findViewById(R.id.imageView);

    //Loading into ImageView
    Glide.with(this)
            .load("http://vrijeme.hr/bradar.gif")
            .into(imageView);
}

我也尝试过使用 Picasso 然后将它与 PhotoView 库连接但是它没有做任何事情,当我尝试捏合缩放时它根本没有缩放,这是该代码的一部分:

ImageView imageView;
PhotoViewAttacher photoView;

imageView = (ImageView) findViewById(R.id.imageView);
photoView = new PhotoViewAttacher(imageView);
Picasso.with(this)
       .load("link")
       .resize(1080,80)
       .into(imageView);

例如,您可以使用此库。 https://github.com/MikeOrtiz/TouchImageView

将您的图像加载到此小部件中,而不是 ImageView

示例用法:

private TouchImageView mContentView;
private private SimpleTarget target;

mContentView = (TouchImageView) findViewById(R.id.fullscreen_content);

target = new SimpleTarget<Bitmap>() {
        @Override
   public void onResourceReady(Bitmap bitmap, GlideAnimation glideAnimation) 
{
            // do something with the bitmap
            // for demonstration purposes, let's just set it to an ImageView
            mContentView.setImageBitmap(bitmap);
        }
    };



    Glide.with(this) // could be an issue!
            .load( imagePath )
            .asBitmap()
            .into(target);

注意,我也首先使用了 SimpleTarget,对于大图像使用 Glide 和捏缩放效果是很好的做法。

布局将是这样的:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.FullscreenActivity">


<com.yourPath.TouchImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/fullscreen_content"/>

</FrameLayout>

此外,有时在此设置后加载图像时会出现问题。 对我来说是这样的。 我重写了 TouchImageView class:

中的方法
@Override
public void setImageBitmap(Bitmap bm) {
    imageRenderedAtLeastOnce = false;
    super.setImageBitmap(bm);
    savePreviousImageValues();
    fitImageToView();
}

这里称为 PhotoView 的库非常受欢迎。
https://github.com/chrisbanes/PhotoView

拥有 13,500 多颗星,30 多位贡献者支持它,这么多人使用它,而且它很容易集成到一个项目中,它几乎感觉像是一个标准。

它也兼容 Glide ^.^


安装chrisbanes的官方文档)

将此添加到您的根 build.gradle 文件中(不是 您的模块 build.gradle 文件):

allprojects {
    repositories {
        maven { url "https://jitpack.io" }
    }
}

然后,将库添加到您的模块中 build.gradle

dependencies {
    implementation 'com.github.chrisbanes:PhotoView:latest.release.here'
}

在撰写本文时,latest.release.here2.1.4


用法chrisbanes的官方文档)

提供了 sample 展示了如何以更高级的方式使用库,但为了完整起见,这里是让 PhotoView 工作所需的全部内容:

<com.github.chrisbanes.photoview.PhotoView
    android:id="@+id/photo_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
PhotoView photoView = (PhotoView) findViewById(R.id.photo_view);
photoView.setImageResource(R.drawable.image);

就是这样!


Glide

的用法

没有任何变化!!

PhotoView photoView = (PhotoView) findViewById(R.id.photo_view);
Glide.with(this).load(imageUrl).into(photoView);