今天学习使用了Fresco开源框架。以下是简单使用步骤:
Frasco的介绍:是facebook为程序员提供的Android加载图片的类库。
Android Studio中使用Frasco步骤:
1、引入到项目中:
dependencies {
compile 'com.facebook.fresco:fresco:0.6.0+'
}
2、如果你仅仅是想简单下载一张网络图片,在下载完成之前,显示一张占位图,
那么简单使用 SimpleDraweeView 即可。
在AndroidManifest.xml文件中 获取网络权限:
<uses-permission android:name="android.permission.INTERNET"/>
3、在 Application 初始化时,在应用调用 setContentView() 之前,进行初始化:
Fresco.initialize(context);
4、在xml布局文件中, 加入命名空间:
<!-- 其他元素 -->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fresco="http://schemas.android.com/apk/res-auto">
5、加入SimpleDraweeView:
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/my_image_view"
android:layout_width="20dp"
android:layout_height="20dp"
fresco:placeholderImage="@drawable/my_drawable"
/>
6、开始加载图片
Uri uri = Uri.parse("https://raw.githubusercontent.com/facebook/fresco/gh-pages/static/fresco-logo.png");
SimpleDraweeView draweeView = (SimpleDraweeView) findViewById(R.id.my_image_view);
draweeView.setImageURI(uri);
大家刚使用Fresco时可能会遇到一些错误。以下链接正是我遇到的错误的解决办法,希望能给大家一些参考。
http://blog.csdn.net/yy1300326388/article/details/45023489
我的应用:
MainActivity.java
package lzl.edu.com.news.util;
import android.util.Log;
import android.util.Xml;
import org.xmlpull.v1.XmlPullParser;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import lzl.edu.com.news.dao.NewsItem;
/**
* Created by admin on 2015/10/13.
*/
public class GetAllNews {
public List<NewsItem> getNews(String path){
List<NewsItem> newsItemList = null;
try {
URL url = new URL(path);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setConnectTimeout(5000);
Log.i("recode---",connection.getResponseCode()+"");
if(connection.getResponseCode() == 200){
//进行xml解析。因为服务器端提供的是xml文件,只能这么解析了。
InputStream is = connection.getInputStream();
newsItemList = getXmlParse(is);
}
return newsItemList;
}catch (Exception e){
e.printStackTrace();
}
return null;
}
public List<NewsItem> getXmlParse(InputStream is){
List<NewsItem> itemList = null;
//创建一个xmlPullParser实例
XmlPullParser parser = Xml.newPullParser();
try {
//设置输入流,并指明编码方式
parser.setInput(is, "utf-8");
int eventType = parser.getEventType();
//定义New的对象
NewsItem newsItem = null;
//循环读取xml文件中的属性
while(eventType!=XmlPullParser.END_DOCUMENT){
try {
switch (eventType) {
case XmlPullParser.START_DOCUMENT://判断是否是文档的开头
//创建数组
itemList = new ArrayList<>();
break;
case XmlPullParser.START_TAG: //判断当前是否是元素标签元素的开始
String name = parser.getName();
if (name.equals("item")) {
//创建对象
newsItem = new NewsItem();
} else if (name.equals("title")) {
String title = parser.nextText();
newsItem.setTitle(title);
} else if (name.equals("description")) {
String description = parser.nextText();
newsItem.setDescription(description);
} else if (name.equals("image")) {
String image = parser.nextText();
newsItem.setImage(image);
} else if (name.equals("type")) {
int type = Integer.parseInt(parser.nextText());
newsItem.setType(type);
} else if (name.equals("comment")) {
int comment = Integer.parseInt(parser.nextText());
newsItem.setComment(comment);
}
break;
case XmlPullParser.END_TAG:
String mName = parser.getName();
if (mName.equals("item"))
itemList.add(newsItem);
break;
default:
break;
}
}catch (Exception e){
e.printStackTrace();
}
//进入下一个元素并触发相应的时间
eventType = parser.next();
}
}catch (Exception e){
e.printStackTrace();
}
return itemList;
}
}
NewsItem.java
package lzl.edu.com.news.dao;
/**
* Created by admin on 2015/10/13.
*/
public class NewsItem {
private String title;
private String description;
private String image;
private int type;
private int comment;
public NewsItem(){
}
public void setTitle(String title) {
this.title = title;
}
public void setDescription(String description) {
this.description = description;
}
public void setImage(String image) {
this.image = image;
}
public void setType(int type) {
this.type = type;
}
public void setComment(int comment) {
this.comment = comment;
}
public String getTitle() {
return title;
}
public String getDescription() {
return description;
}
public String getImage() {
return image;
}
public int getType() {
return type;
}
public int getComment() {
return comment;
}
@Override
public String toString() {
return "NewsItem{" +
"title='" + title + '\'' +
", description='" + description + '\'' +
", image='" + image + '\'' +
", type=" + type +
", comment=" + comment +
'}';
}
}
news_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
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_marginTop="10dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:id="@+id/image"
android:layout_width="100dp"
fresco:placeholderImage="@mipmap/ic_launcher"
android:layout_height="100dp" />
<LinearLayout
android:id="@+id/linear"
android:layout_marginLeft="105dp"
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:orientation="vertical"
android:layout_height="wrap_content">
<TextView
android:id="@+id/title"
android:text="标题"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/content"
android:text="内容"
android:textColor="#BB000000"
android:textSize="15sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<TextView
android:id="@+id/comment"
android:textSize="10sp"
android:text="评论:123"
android:textColor="#E57373"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
activity_main.xml
<RelativeLayout 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" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<ListView
android:id="@+id/news_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"></ListView>
<RelativeLayout
android:id="@+id/relative"
android:visibility="gone"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ProgressBar
android:id="@+id/progress"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:layout_centerInParent="true"
android:layout_below="@+id/progress"
android:text="正在加载..."
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
</RelativeLayout>