**第一步:添加依赖,如**
/*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**
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="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">
<!--RecyclerView布局-->
<android.support.v7.widget.RecyclerView
android:id="@+id/rv"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
</android.support.constraint.ConstraintLayout>
**layout_item**
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--SimpleDraweeView控件,放置图片-->
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/tu"
android:layout_width="100dp"
android:layout_height="100dp"
/>
</LinearLayout>
**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);
}
}
**最后别忘了添加网络权限,注册图片加载**
Fresco获取动图,RecyclerView展示(只展示动图)
最新推荐文章于 2021-05-25 16:51:18 发布