In this tutorial you will learn how to use Picasso android library to load image from url.
在本教程中,您将学习如何使用Picasso android库从url加载图像。
Picasso is an open source android library which is developed and maintained by Square. Below are some features that make this library the best option for loading image from internet.
Picasso是由Square开发和维护的一个开源android库。 以下是使此库成为从Internet加载图像的最佳选择的一些功能。
Features
特征
Easy to use and reduces the code very much
易于使用,大大减少了代码
Automatic memory and cache management
自动内存和缓存管理
Allows image transformation
允许图像转换
You can also use Volley library, which is a great alternative of Picasso.
您还可以使用Volley库,它是毕加索的绝佳替代品。
Also Read: Android Volley Tutorial
另请阅读: Android Volley教程
![Picasso Android Tutorial – Load Image from URL](https://i-blog.csdnimg.cn/blog_migrate/9d57c823e3e3aa201f207d32594e226b.png)
Picasso Android教程 (Picasso Android Tutorial)
Before using Picasso we have to add its dependency in build.gradle file.
在使用Picasso之前,我们必须在build.gradle文件中添加其依赖项 。
compile 'com.squareup.picasso:picasso:2.5.2'
Also add internet access permission in AndroidMainfest.xml file.
还要在AndroidMainfest.xml文件中添加Internet访问权限。
<uses-permission android:name="android.permission.INTERNET"/>
正在载入图片 (Loading Image)
We can load image by just typing one line of code. It can be done in following way.
我们只需输入一行代码即可加载图像。 可以通过以下方式完成。
Picasso.with(context).load(image_url).into(imageview);
占位符和错误处理程序 (Place Holder and Error Handler)
We can specify an image as a place holder until the image is being loaded. We can also give an image as an error handler if any error occurs while loading the image.
在加载图像之前,我们可以将图像指定为占位符。 如果在加载图像时发生任何错误,我们还可以将图像作为错误处理程序。
Picasso.with(context).load(image_url).placeholder(placeholder_image).error(error_image)into(imageview);
影像转换 (Image Transformation)
As I already told that this library also supports image transformation. So we can change image dimensions to fit layouts and reduce memory size.
正如我已经说过的那样,该库还支持图像转换。 因此,我们可以更改图像尺寸以适合布局并减小内存大小。
Picasso.with(context).load(image_url).resize(width, height).rotate(degree).into(imageview);
Don’t you thing that it reduces the code very much?
您是否认为这会大大减少代码?
毕加索Android示例 (Picasso Android Example)
In this example I am loading image from url on button click.
在此示例中,我在单击按钮时从url加载图像。
Create a project with package name com.picassoandroid and add following code in respective files
创建一个程序包名称为com.picassoandroid的项目,并在相应文件中添加以下代码
activity_main.xml
activity_main.xml
<LinearLayout 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" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:id="@+id/img"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btn"
android:text="Load Image"/>
</LinearLayout>
MainActivity.java
MainActivity.java
package com.picassoandroid;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import com.squareup.picasso.Picasso;
public class MainActivity extends Activity {
ImageView img;
Button btn;
String url ="https://www.thecrazyprogrammer.com/wp-content/uploads/2015/07/The-Crazy-Programmer.png";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img = (ImageView)findViewById(R.id.img);
btn = (Button)findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Picasso.with(MainActivity.this).load(url).into(img);
}
});
}
}
The code is self explanatory, if still you are facing any difficulty in above Picasso android tutorial then feel free to ask it by commenting below.
该代码是不言自明的,如果您在上面的Picasso android教程中仍然遇到任何困难,请随时在下面的评论中提问。
Happy Coding!! 🙂 🙂
快乐编码! 🙂
翻译自: https://www.thecrazyprogrammer.com/2016/08/picasso-android-tutorial.html