![](/img/trans.png)
[英]Android Glide - How do I load another image if my original image takes too long to load?
[英]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);
这个名为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.here
是2.1.4
。
用法( chrisbanes的官方文档)
提供了一个示例,展示了如何以更高级的方式使用该库,但为了完整起见,这里是让 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);
例如,您可以使用此库。 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 类中的方法:
@Override
public void setImageBitmap(Bitmap bm) {
imageRenderedAtLeastOnce = false;
super.setImageBitmap(bm);
savePreviousImageValues();
fitImageToView();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.