标签:ndt instance save idt open public .com idg etc
一、新建项目
二、下载Picasso的jar包:http://square.github.io/picasso/
将jar包导入工程
三、注册清单中配置权限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
四、布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <Button android:id="@+id/btn_load" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="加载网络图片" /> <Button android:id="@+id/btn_size_load" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="加载网络图片(设置大小)" /> <Button android:id="@+id/btn_err_load" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="加载网络图片(设置出错图片)" /> </LinearLayout> <ImageView android:id="@+id/iv_img" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
五、使用代码
package com.example.picassodemo; import android.support.v7.app.AppCompatActivity; 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 AppCompatActivity { private Button btnLoad, btnSizeLoad, btnErrLoad; private ImageView ivImg; private String url = "http://i.zeze.com/attachment/forum/201605/06/214815xnd5dz5t58fndt85.jpg"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); initEvent(); } private void initView() { btnLoad = (Button) findViewById(R.id.btn_load); btnSizeLoad = (Button) findViewById(R.id.btn_size_load); btnErrLoad = (Button) findViewById(R.id.btn_err_load); ivImg = (ImageView) findViewById(R.id.iv_img); } private void initEvent() { btnLoad.setOnClickListener(myOnClickListener); btnSizeLoad.setOnClickListener(myOnClickListener); btnErrLoad.setOnClickListener(myOnClickListener); } private View.OnClickListener myOnClickListener = new View.OnClickListener() { @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_load: //加载网络图片 Picasso.with(MainActivity.this).load(url).into(ivImg); break; case R.id.btn_size_load: //加载网络图片(设置大小) Picasso.with(MainActivity.this).load(url).resize(120, 120).into(ivImg); break; case R.id.btn_err_load: //加载网络图片(设置错误图片) Picasso.with(MainActivity.this).load(url).error(R.mipmap.ic_launcher).into(ivImg); break; } } }; }
六、运行效果
标签:ndt instance save idt open public .com idg etc
原文地址:http://www.cnblogs.com/bky1225987336/p/6385649.html