先看效果:
1.布局:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" 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="com.example.administrator.fresco.MainActivity"> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/my_image_view" android:layout_centerHorizontal="true" android:layout_marginTop="12dp" android:onClick="loadInternetImage" android:text="加载网络图片" /> <com.facebook.drawee.view.SimpleDraweeView android:id="@+id/my_image_view" android:layout_width="400dp" android:layout_height="400dp" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" fresco:roundAsCircle="true"其中fresco:roundingBorderWidth="2dp"fresco:roundedCornerRadius="10dp" fresco:roundingBorderColor="@color/colorAccent" fresco:placeholderImage="@mipmap/ic_launcher" fresco:placeholderImageScaleType="fitCenter"/></RelativeLayout>
roundAsCircle是否为圆形,roundingBorderWidth为圆形边框,placeholderImage为占位图。 2.导入依赖
compile 'com.facebook.fresco:fresco:1.5.0' compile 'com.facebook.fresco:animated-gif:1.5.0'//gif动图 3.MainActivity:package com.example.administrator.fresco; import android.net.Uri; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import com.facebook.drawee.backends.pipeline.Fresco; import com.facebook.drawee.interfaces.DraweeController; import com.facebook.drawee.view.SimpleDraweeView; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Fresco.initialize(this);//需要在setContentView之前初始化 setContentView(R.layout.activity_main); Button mButton = (Button) findViewById(R.id.button); mButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //点击事件加载网络图片代码 // 动图 Uri uri = Uri.parse("res://" + getPackageName() + "/" + R.drawable.aaa); Uri uri = Uri.parse("http://dynamic-image.yesky.com/740x-/uploadImages/2015/163/50/690V3VHW0P77.jpg"); SimpleDraweeView draweeView = (SimpleDraweeView) findViewById(R.id.my_image_view); DraweeController controller = Fresco.newDraweeControllerBuilder() .setUri(uri) .setAutoPlayAnimations(true) .build(); draweeView.setController(controller); } }); } }