Fresco加载网络图片(圆形)
xml布局 <?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" android:orientation="vertical" tools:context="com.example.shexinhai_1128.MainActivity"> <ListView android:id="@+id/mylist" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center"> </ListView> </LinearLayout>
适配器布局 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" xmlns:fresco="http://schemas.android.com/apk/res-auto"> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="100dp"> <com.facebook.drawee.view.SimpleDraweeView android:id="@+id/myface" android:layout_width="100dp" android:layout_height="100dp" fresco:actualImageScaleType="focusCrop" fresco:placeholderImageScaleType="fitCenter" fresco:progressBarImageScaleType="centerInside" fresco:progressBarAutoRotateInterval="5000" fresco:failureImageScaleType="centerInside" fresco:retryImageScaleType="centerCrop" fresco:fadeDuration="5000" fresco:backgroundImage="@android:color/holo_orange_light" fresco:roundAsCircle="true" > </com.facebook.drawee.view.SimpleDraweeView> <TextView android:id="@+id/mytext" android:layout_width="match_parent" android:layout_height="100dp" android:gravity="center" android:text="世界欢迎你 "/> </LinearLayout> </RelativeLayout>
MainActivity package com.example.shexinhai_1128; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.ListView; import com.facebook.drawee.backends.pipeline.Fresco; import com.google.gson.Gson; import java.io.IOException; import java.util.List; import okhttp3.Call; import okhttp3.Callback; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; public class MainActivity extends AppCompatActivity { private List<Bean.ResultBean.DataBean> list; private ListView listView; String url = "http://v.juhe.cn/toutiao/index?type=top&key=9ced3a8395ae78665be001031a4e82c0"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Fresco.initialize(this); setContentView(R.layout.activity_main); listView = findViewById(R.id.mylist); getServerData(); } //OkHttp Get请求数据 private void getServerData() { OkHttpClient okHttp = new OkHttpClient(); Request request = new Request.Builder().url(url).build(); Call call = okHttp.newCall(request); call.enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(Call call, Response response) throws IOException { final String json = response.body().string(); Gson gson = new Gson(); Bean bean = gson.fromJson(json,Bean.class); List<Bean.ResultBean.DataBean> list = bean.getResult().getData(); final MyAdapter adapter = new MyAdapter(MainActivity.this,list); runOnUiThread(new Runnable() { @Override public void run() { //Toast.makeText(MainActivity.this,"请求成功"+json,Toast.LENGTH_LONG).show(); listView.setAdapter(adapter); } }); } }); } }
适配器 (MyAdapter) package com.example.shexinhai_1128; import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; import com.facebook.drawee.backends.pipeline.Fresco; import com.facebook.drawee.generic.RoundingParams; import com.facebook.drawee.interfaces.DraweeController; import com.facebook.drawee.view.SimpleDraweeView; import java.util.List; /** * Created by 佘鑫海 on 2017/11/28. */ public class MyAdapter extends BaseAdapter { private Context context; private List<Bean.ResultBean.DataBean> list; public MyAdapter(Context context, List<Bean.ResultBean.DataBean>list){ this.context = context; this.list = list; } @Override public int getCount() { return list.size(); } @Override public Object getItem(int i) { return i; } @Override public long getItemId(int i) { return i; } @Override public View getView(int i, View view, ViewGroup viewGroup) { view=View.inflate(context,R.layout.activity_item,null); TextView tv= view.findViewById(R.id.mytext); SimpleDraweeView image= view.findViewById(R.id.myface); tv.setText(list.get(i).getTitle()); DraweeController controller = Fresco.newDraweeControllerBuilder() .setUri(list.get(i).getThumbnail_pic_s()) .setAutoPlayAnimations(true) .build(); image.setController(controller); RoundingParams roundingParams = image.getHierarchy().getRoundingParams(); roundingParams.setBorder(R.color.colorAccent, 1); roundingParams.setRoundAsCircle(true); image.getHierarchy().setRoundingParams(roundingParams); return view; } }
初始化Fresco package com.example.shexinhai_1128; import android.app.Application; import com.facebook.drawee.backends.pipeline.Fresco; /** * Created by 佘鑫海 on 2017/11/28. */ public class MyApplication extends Application { @Override public void onCreate() { super.onCreate(); // 初始化Fresco Fresco.initialize(this); } }