声明一个View 和一个 Uri
private SimpleDraweeView draweeView;
private Uri uri;
//要加载的图片
uri = Uri.parse("http://imgstatic.baidu.com/img/image/shouye/xiaohua0605.jpg");
//给view实例化
draweeView = (SimpleDraweeView) findViewById(R.id.sdv_image);
draweeView.setImageURI(uri);
/*
要想使用Fresco 必须要在oncreate()里面做Fresco的初始化
*/
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Fresco.initialize(this);这个地方很重要 要放在 setContentView之前 因为要在布局里面用Fresco的控件所以要先声明出来
setContentView(R.layout.activity_main);
uri = Uri.parse("http://imgstatic.baidu.com/img/image/shouye/xiaohua0605.jpg");
draweeView = (SimpleDraweeView) findViewById(R.id.sdv_image);
draweeView.setImageURI(uri);
}之前和大家分享了Facebook开源的Fresco,那都是皮毛,今天给大家分享点使用的,简单的用Fresco来加载URL图片,并且实现加载时的默认图片:附上重要代码:
</pre><pre name="code" class="java">
下面我们来看布局:
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.frescodemo.MainActivity" >
//此处添加Fresco的控件
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/sdv_image"
android:layout_width="120dp"//此地方一定要写具体的值 dp 不要写wrap_content之类的,之后还会给大家分享为什么这么写
android:layout_height="120dp"
fresco:placeholderImage="@drawable/ic_launcher"//这个就是展位图的地方,很好用 一张图片就OK
></com.facebook.drawee.view.SimpleDraweeView>
</RelativeLayout>
点击打开链接demo