activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <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" xmlns:fresco="http://schemas.android.com/apk/res-auto" android:orientation="vertical" android:padding="20dp" tools:context="com.bwie.fresco.MainActivity"> <com.facebook.drawee.view.SimpleDraweeView android:id="@+id/sdv1" android:layout_width="150dp" android:layout_height="150dp" fresco:placeholderImage="@mipmap/ic_launcher_round"/> <com.facebook.drawee.view.SimpleDraweeView android:id="@+id/sdv2" android:layout_width="150dp" android:layout_height="150dp" android:layout_marginTop="100dp" fresco:placeholderImage="@mipmap/ic_launcher_round"/> </LinearLayout>
activity_gif.xml
<?xml version="1.0" encoding="utf-8"?> <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" tools:context="com.bwie.fresco.GifActivity"> <android.support.v7.widget.RecyclerView android:id="@+id/rv" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
item.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:fresco="http://schemas.android.com/apk/res-auto" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <com.facebook.drawee.view.SimpleDraweeView android:id="@+id/sdv" fresco:viewAspectRatio="1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="5dp" fresco:placeholderImage="@mipmap/ic_launcher_round"/> </LinearLayout>加载图片的appMyApp--去清单文件里面注册public class MyApp extends Application { @Override public void onCreate() { super.onCreate(); Fresco.initialize(this); } }加载图片的bean工具类Imagepublic class Image { public String urlGif; public Image(String urlGif) { this.urlGif = urlGif; } public Uri getUriGif(){ return Uri.parse(urlGif); } }MainActivitypublic class MainActivity extends AppCompatActivity { private SimpleDraweeView mSdv1; private SimpleDraweeView mSdv2; private DraweeController controller; private DraweeController controllerWebp; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mSdv1 = findViewById(R.id.sdv1); mSdv2 = findViewById(R.id.sdv2); Uri uri = Uri.parse("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1511780233036&di=" + "2d0f0fa03ff8bf72b73ad46411f6435e&imgtype=0&src=http%3A%2F%2Fimage.kejixun.com%2F2016%2F1020%2F20161020052639586.jpg"); // Uri uri = Uri.parse("http://image.soogif.com/AffqqcX53mdvI3ipNxj735loE1PjmuyR.gif_s200x0"); controller = Fresco.newDraweeControllerBuilder() .setUri(uri) .setAutoPlayAnimations(true) .build(); mSdv1.setController(controller); Uri webpuri = Uri.parse("https://p.upyun.com/demo/webp/webp/animated-gif-0.webp"); controllerWebp = Fresco.newDraweeControllerBuilder() .setUri(webpuri) .setAutoPlayAnimations(true) .build(); mSdv2.setController(controllerWebp); mSdv2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(MainActivity.this, GifActivity.class); startActivity(intent); } }); } }GifActivitypublic class GifActivity extends AppCompatActivity { private RecyclerView mRv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_gif); mRv = (RecyclerView) findViewById(R.id.rv); GridLayoutManager manager = new GridLayoutManager(this,3); mRv.setLayoutManager(manager); mRv.setAdapter(new GifAdapter()); } }GifAdapterpublic class GifAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { List<Image> images = new ArrayList<>(); public GifAdapter() { for (int i = 0; i < 10; i++) { images.add(new Image("http://image.soogif.com/WO81V2zm6meci3kViBz8vtWdZt7ehldP.gif_s200x0")); images.add(new Image("http://image.soogif.com/cSJysItqn9BLIcsb2DpiGiEQZctsF2iG.gif_s200x0")); images.add(new Image("http://image.soogif.com/AffqqcX53mdvI3ipNxj735loE1PjmuyR.gif_s200x0")); } } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, null); return new MyViewHolder(view); } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { Image image = images.get(position); MyViewHolder holder1 = (MyViewHolder) holder; DraweeController controller = Fresco.newDraweeControllerBuilder() .setUri(image.getUriGif()) .setAutoPlayAnimations(true) .build(); holder1.sdv.setController(controller); } @Override public int getItemCount() { return images == null ? 0:images.size(); } class MyViewHolder extends RecyclerView.ViewHolder{ private SimpleDraweeView sdv; public MyViewHolder(View itemView) { super(itemView); sdv = itemView.findViewById(R.id.sdv); } } }用到的依赖包compile 'com.facebook.fresco:fresco:0.11.0' compile 'com.facebook.fresco:animated-gif:0.12.0' compile 'com.facebook.fresco:animated-webp:0.12.0' compile 'com.facebook.fresco:webpsupport:0.12.0' compile 'com.android.support:recyclerview-v7:26.1.0'