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

时间:2017-04-26 16:38:36

标签: java android imageview android-glide

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);

2 个答案:

答案 0 :(得分:5)

例如,您可以使用此库。 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和pinch缩放效果是一种很好的做法。

布局将是这样的:

<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类重写方法:

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

答案 1 :(得分:3)

此库称为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);