内容:
1、滑动优化(滑动时不加载图片,停止才加载)
2、第一次进入时手动加载
代码如下:
1、界面布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<ImageView
android:id="@+id/image"
android:src="@mipmap/ic_launcher"
android:layout_width="60dp"
android:layout_height="60dp" />
<LinearLayout
android:orientation="vertical"
android:layout_marginLeft="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/title_tv"
android:text="TITLE"
android:textSize="15dp"
android:maxLines="1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/content_tv"
android:text="CONTENT"
android:textSize="10dp"
android:maxLines="3"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.leixiansheng.news.MainActivity">
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</RelativeLayout>
2、开启异步解析数据
package com.example.leixiansheng.news;
public class NewsBean {
public String viewUrl;
public String title;
public String content;
}
package com.example.leixiansheng.news;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ListView listView;
private static String URL = "http://www.imooc.com/api/teacher?type=4&num=30";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.list_view);
new NewsAsyncTask().execute(URL);
}
/
public class NewsAdapter extends BaseAdapter implements AbsListView.OnScrollListener{
private List<NewsBean> newsBeanList;
private LayoutInflater inflater;
private ImageLoader imageLoader; //图片加载
private int start; //第一个元素
private int end; //最后一个元素
private boolean isFirstIn; //是否第一次进入
public static String[] URLS; //所有资源
public NewsAdapter(Context context, List<NewsBean> newsBeanList, ListView listView) {
this.newsBeanList = newsBeanList;
inflater = LayoutInflater.from(context);
imageLoader = new ImageLoader(listView);
URLS = new String[newsBeanList.size()];
for (int i = 0; i < newsBeanList.size(); i++) {
URLS[i] = newsBeanList.get(i).viewUrl;
}
isFirstIn = true;
listView.setOnScrollListener(this);
}
@Override
public int getCount() {
return newsBeanList.size();
}
@Override
public Object getItem(int i) {
return newsBeanList.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder viewHolder = null;
if (view == null) {
viewHolder = new ViewHolder();
view = inflater.inflate(R.layout.item, null);
viewHolder.imageView = (ImageView) view.findViewById(R.id.image);
viewHolder.title = (TextView) view.findViewById(R.id.title_tv);
viewHolder.content = (TextView) view.findViewById(R.id.content_tv);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
}
String url = newsBeanList.get(i).viewUrl;
viewHolder.imageView.setImageResource(R.mipmap.ic_launcher);
/
@Override
public void onScroll(AbsListView absListView, int i, int i1, int i2) {
start = i;
end = i + i1;
//第一次进入需要手动加载
if (isFirstIn && i1 > 0) {
imageLoader.loadImages(start, end);
isFirstIn = false;
}
}
}
package com.example.leixiansheng.news;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.util.LruCache;
import android.widget.ImageView;
import android.widget.ListView;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashSet;
import java.util.Set;
public class ImageLoader {
private ImageView mImageView;
private String mUrl;
//*&*创建缓存
private LruCache<String, Bitmap> lruCache;
private ListView listview;
private Set<NewsAsyncTask> mTask;
public ImageLoader(ListView listview) {
this.listview = listview;
mTask = new HashSet<>();
//*&*获取最大内存
int maxMemory = (int) Runtime.getRuntime().maxMemory();
//设置缓存大小
int lruCacheSize = maxMemory / 4;
lruCache = new LruCache<String, Bitmap>(lruCacheSize) {
@Override
protected int sizeOf(String key, Bitmap value) {
//获取每个数据大小
return value.getByteCount();
}
};
}
//添加数据到缓存
public void addBitmapToLruCache(String url, Bitmap bitmap) {
if (getBitmapFromLruCache(url) == null) {
lruCache.put(url, bitmap);
}
}
//从缓存中获取数据
public Bitmap getBitmapFromLruCache(String url) {
return lruCache.get(url);
}
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (mImageView.getTag().equals(mUrl)) {
mImageView.setImageBitmap((Bitmap) msg.obj);
}
}
};
public void showImageByThread(ImageView imageView, final String url) {
mImageView = imageView;
mUrl = url;
new Thread() {
@Override
public void run() {
super.run();
Bitmap bitmap = getBitmapFromURL(url);
Message message = Message.obtain();
message.obj = bitmap;
handler.sendMessage(message);
}
}.start();
}
public Bitmap getBitmapFromURL(String urlString) {
Bitmap bitmap;
InputStream is = null;
try {
URL url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
is = new BufferedInputStream(connection.getInputStream());
bitmap = BitmapFactory.decodeStream(is);
connection.disconnect();
//模拟网速卡顿时
// try {
// Thread.sleep(1000);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
return bitmap;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
public void showImageViewByAsyncTask(ImageView imageView, String url) {
//判断是否已经缓存
Bitmap bitmap = getBitmapFromLruCache(url);
//没有缓存则从新下载
if (bitmap == null) {
imageView.setImageResource(R.mipmap.ic_launcher);
} else {
imageView.setImageBitmap(bitmap);
}
}
//加载从start到end的所有图片
public void loadImages(int start, int end) {
for (int i = start; i < end; i++) {
String url = NewsAdapter.URLS[i];
//判断是否已经缓存
Bitmap bitmap = getBitmapFromLruCache(url);
//没有缓存则从新下载
if (bitmap == null) {
NewsAsyncTask task = new NewsAsyncTask(url);
task.execute(url);
mTask.add(task);
} else {
ImageView imageView = (ImageView) listview.findViewWithTag(url);
imageView.setImageBitmap(bitmap);
}
}
}
public void cancelAllTasks() {
if (mTask != null) {
for (NewsAsyncTask task : mTask) {
task.cancel(false);
}
}
}
private class NewsAsyncTask extends AsyncTask<String, Void, Bitmap> {
// private ImageView imageView;
private String url;
public NewsAsyncTask(String url) {
// this.imageView = imageView;
this.url = url;
}
@Override
protected Bitmap doInBackground(String... strings) {
String url = strings[0];
//从网络获取图片
Bitmap bitmap = getBitmapFromURL(url);
if (bitmap != null) {
//将不在缓存中的图片加入到缓存
addBitmapToLruCache(url, bitmap);
}
return bitmap;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
ImageView imageView = (ImageView) listview.findViewWithTag(url);
if (imageView != null && bitmap != null) {
imageView.setImageBitmap(bitmap);
}
mTask.remove(this);
}
}
}
4、注册声明权限
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.leixiansheng.news">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
以上所述是小编给大家介绍的Android 模拟新闻APP显示界面滑动优化实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程网网站的支持!
您可能感兴趣的文章:Android APP启动方式、启动流程及启动优化分析总结Android App内存优化之图片优化Android App应用启动分析与优化实例讲解Android app开发中ListView的基本使用及优化Android APP性能优化分析