简单的Fresco加载图片
来源:互联网 发布:端口占用查询工具 编辑:程序博客网 时间:2024/04/23 17:19
package com.example.frescolistview;import android.os.AsyncTask;import android.os.Bundle;import android.os.Handler;import android.support.v7.app.AppCompatActivity;import android.widget.ListView;import com.google.gson.Gson;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.URL;import java.util.List;public class MainActivity extends AppCompatActivity { private List<Bean.DataBean> list; private ListView lv; private MyBaseadapter adapter; private Handler handler=new Handler(){ Runnable runnable=new Runnable() { @Override public void run() { try { MainActivity.this.wait(1000); } catch (Exception e) { e.printStackTrace(); } } }; }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv= (ListView) findViewById(R.id.lv); data(); } public void data(){ jiexi("http://result.eolinker.com/iYXEPGn4e9c6dafce6e5cdd23287d2bb136ee7e9194d3e9?uri=vedio"); } public void jiexi(String path){ new AsyncTask<String,Void,String>(){ @Override protected void onPostExecute(String s) { if (s!=null){ Gson gson=new Gson(); Bean bean = gson.fromJson(s, Bean.class); list = bean.getData(); adapter=new MyBaseadapter(MainActivity.this,list); lv.setAdapter(adapter); } } @Override protected String doInBackground(String... params) { try { URL url=new URL(params[0]); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setReadTimeout(3000); connection.setConnectTimeout(3000); int code = connection.getResponseCode(); if (code==200){ InputStream is = connection.getInputStream(); return info.readFromNetWork(is); } } catch (Exception e) { e.printStackTrace(); } return null; } }.execute(path); }}
以上便是MainActivity文件主要就是负责实现图片的listview布局展示
package com.example.frescolistview;import android.app.Application;import com.facebook.drawee.backends.pipeline.Fresco;/** * Created by HP on 2017/11/28. */public class MyApp extends Application { @Override public void onCreate() { super.onCreate(); Fresco.initialize(this); }}以上便是首先通过初始化Fresco文件进行加载最后一定记得在maniface文件下注册
package com.example.frescolistview;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.interfaces.DraweeController;import com.facebook.drawee.view.SimpleDraweeView;import java.util.List;/** * Created by HP on 2017/11/28. */public class MyBaseadapter extends BaseAdapter { private List<Bean.DataBean> list; private Context context; public MyBaseadapter(Context context, List<Bean.DataBean> list) { this.context=context; this.list=list; } @Override public int getCount() { return list!=null?list.size():0; } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { convertView=View.inflate(context,R.layout.item,null); SimpleDraweeView simpleDraweeView=(SimpleDraweeView) convertView.findViewById(R.id.main_simple_drawee_view); TextView tv=(TextView) convertView.findViewById(R.id.tv); tv.setText(list.get(position).getTitle()); DraweeController controller = Fresco.newDraweeControllerBuilder() .setUri(list.get(position).getImage_url()) .setAutoPlayAnimations(true) .build(); simpleDraweeView.setController(controller); return convertView; }}
以上便是适配器文件的配置
<?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" tools:context="com.example.frescolistview.MainActivity"><ListView android:id="@+id/lv" android:layout_width="match_parent" android:layout_height="match_parent"></ListView></LinearLayout>以上是xmlmain-activity
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:fresco="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> <com.facebook.drawee.view.SimpleDraweeView android:layout_width="100dp" android:layout_height="100dp" fresco:roundAsCircle="true" android:id="@+id/main_simple_drawee_view" fresco:placeholderImage="@mipmap/ic_launcher"/> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentTop="true" android:layout_toRightOf="@+id/main_simple_drawee_view" android:layout_toEndOf="@+id/main_simple_drawee_view"> <TextView android:layout_marginTop="20dp" android:id="@+id/tv" android:layout_width="match_parent" android:layout_height="wrap_content" /> <TextView android:layout_marginTop="20dp" android:id="@+id/tv1" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout></RelativeLayout>以上就是item文件
dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:26.+' compile 'com.android.support.constraint:constraint-layout:1.0.2' testCompile 'junit:junit:4.12' compile 'com.facebook.fresco:animated-gif:0.12.0' compile 'com.facebook.fresco:fresco:0.11.0' compile 'com.google.code.gson:gson:2.2.4'
最后在附送上依赖这边是简单的Fresco的使用
阅读全文
0 0
- 简单的Fresco加载图片
- Fresco图片加载框架的简单介绍
- Fresco图片加载框架的简单实用
- Fresco 图片加载框架的简单使用
- Android强大的图片加载框架Fresco简单用法
- Fresco图片加载框架一些简单使用
- 简单的Fresco加载图片和GIF图片加载与停止
- Android 强大的图片加载库Fresco,Fresco介绍
- Fresco-加载图片基础[详细图解Fresco的使用]
- Fresco-加载图片基础[详细图解Fresco的使用]
- 图片加载神器fresco----fresco的基本使用
- Fresco,一个强大的图片加载组件
- 强大Android图片加载的框架:Fresco
- Fresco强大的图片加载器
- Fresco 一个强大的图片加载框架
- Fresco强大的图片加载框架
- 网络图片加载库 Fresco 的使用
- Fresco加载图片的几种方式
- Problem L 求m到n之和 C语言函数
- 开放封闭原则
- 火焰图
- java-super关键字
- 22、TensorFlow 实现双隐层分类器
- 简单的Fresco加载图片
- JAVA中IO流
- 早安1128
- Windows 下 zookeeper 部署
- Grafana+Prometheus系统监控之Redis
- java手动实现HTTP响应response
- Uncaught ReferenceError: $ is not defined? 的解决方法
- 依赖倒转原则
- ThinkPHP5框架集成第三方登陆SDK实现登陆功能