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);
答案 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.here
是2.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);