Fresco获取动图,RecyclerView展示(只展示动图)

    **第一步:添加依赖,如**
    /*recyclerview依赖*/
    compile 'com.android.support:recyclerview-v7:26.1.0'
    /*fresco加载图片*/
    compile 'com.facebook.fresco:fresco:0.11.0'
    /*fresco加载动图*/
    compile 'com.facebook.fresco:animated-gif:0.12.0'
    **第二步,上代码**
    **activity_main**
    "1.0" encoding="utf-8"?>
"http://schemas.android.com/apk/res/android"
    xmlns:app="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.bawei.lss_gif.MainActivity">

"@+id/rv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">



    **layout_item**
    "1.0" encoding="utf-8"?>
"http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

"@+id/tu"
    android:layout_width="100dp"
    android:layout_height="100dp"
    />


  **MainActivity** 
  public class MainActivity extends AppCompatActivity {

    private RecyclerView mRv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        mRv.setLayoutManager(new LinearLayoutManager(this));

        Myadapter myadapter = new Myadapter(MainActivity.this);
        mRv.setAdapter(myadapter);
    }

    private void initView() {
        mRv = (RecyclerView) findViewById(R.id.rv);
    }
}
**Myadapter**
public class Myadapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
  private Context context;
  String[] path=new String[]{
          "http://qq.yh31.com/tp/zjbq/201711191845013603.gif",
          "http://qq.yh31.com/tp/zjbq/201711191845013603.gif",
          "http://qq.yh31.com/tp/zjbq/201711191845013603.gif",
          "http://qq.yh31.com/tp/zjbq/201711191845013603.gif",
          "http://qq.yh31.com/tp/zjbq/201711191845013603.gif",
          "http://qq.yh31.com/tp/zjbq/201711191845013603.gif",
          "http://qq.yh31.com/tp/zjbq/201711191845013603.gif",
          "http://qq.yh31.com/tp/zjbq/201711191845013603.gif"
  };

    public Myadapter(Context context) {
        this.context = context;

    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view=View.inflate(context,R.layout.layout_item,null);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
       MyViewHolder myViewHolder= (MyViewHolder) holder;

        DraweeController draweeController = Fresco.newDraweeControllerBuilder()
                .setUri(path[position])
                .setAutoPlayAnimations(true) // 设置加载图片完成后是否直接进行播放
                .build();
        myViewHolder.tu.setController(draweeController);
    }

    @Override
    public int getItemCount() {
        return path.length;
    }
    class MyViewHolder extends RecyclerView.ViewHolder{
        SimpleDraweeView tu;
        public MyViewHolder(View itemView) {
            super(itemView);
            tu = itemView.findViewById(R.id.tu);
        }
    }
}
**Myapp**
public class Myapp extends Application{
    @Override
    public void onCreate() {
        super.onCreate();
        Fresco.initialize(this);
    }
}
**最后别忘了添加网络权限,注册图片加载**

你可能感兴趣的