使用Fresco实现显示一张图片
仅仅是下载一张图片,在下载完之前,先显示一张站位图
效果图
源码
下载地址(Android Studio工程):http://download.csdn.net/detail/q4878802/9067397
原文参考
官方地址:http://fresco-cn.org/docs/getting-started.html#_
主要步骤
1. 添加网络权限
<!-- 添加网络权限 -->
<uses-permission android:name="android.permission.INTERNET" />
2. 初始化Fresco
// 初始化Fresco对象
Fresco.initialize(this);
始化要在setContentView之前完成,否则会报空指针异常,找不到SimpleDraweeView控件.
3. 在显示图片的xml布局的根节点,添加命名空间
xmlns:fresco="http://schemas.android.com/apk/res-auto"
4. 添加显示图片的SimpleDraweeView控件
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/my_image_view"
android:layout_width="300dp"
android:layout_height="match_parent"
fresco:placeholderImage="@mipmap/ic_launcher" />
这里的宽和高可以用具体的dp值,也可以用”match_parent”属性,但是不要用”wrap_content”属性
5. 加载图片
SimpleDraweeView simpleDraweeView = (SimpleDraweeView) findViewById(R.id.my_image_view);
Uri uri = Uri.parse("http://d.hiphotos.baidu.com/image/pic/item/faedab64034f78f07c22c8207d310a55b3191c33.jpg");
simpleDraweeView.setImageURI(uri);
初始化
1. 在清单文件中的application标签下添加name属性
android:name=".InitApplication"
2. 初始化类
package com.kongqw.frescodemo1;
import android.app.Application;
import com.facebook.drawee.backends.pipeline.Fresco;
/**
* Created by kongqw on 2015/8/31.
*/
public class InitApplication extends Application {
@Override
public void onCreate() {
// 初始化Fresco对象
Fresco.initialize(this);
super.onCreate();
}
}
XML页面布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fresco="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/my_image_view"
android:layout_width="300dp"
android:layout_height="match_parent"
fresco:placeholderImage="@mipmap/ic_launcher" />
</RelativeLayout>
测试类
package com.kongqw.frescodemo1;
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import com.facebook.drawee.view.SimpleDraweeView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SimpleDraweeView simpleDraweeView = (SimpleDraweeView) findViewById(R.id.my_image_view);
Uri uri = Uri.parse("http://d.hiphotos.baidu.com/image/pic/item/faedab64034f78f07c22c8207d310a55b3191c33.jpg");
simpleDraweeView.setImageURI(uri);
}
}