大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
这篇文章将为大家详细讲解有关怎么Android应用中实现一个APP显示界面滑动优化,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
在南涧等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供成都网站建设、网站建设 网站设计制作按需网站制作,公司网站建设,企业网站建设,成都品牌网站建设,全网整合营销推广,外贸网站建设,南涧网站建设费用合理。
1、界面布局
2、开启异步解析数据
package com.example.leixiansheng.news; /** * Created by Leixiansheng on 2017/3/21. */ 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); } //*&*异步加载,处理耗时任务,UI更新 class NewsAsyncTask extends AsyncTask> { @Override protected List doInBackground(String... strings) { return getJsonData(strings[0]); } @Override protected void onPostExecute(List newsBeen) { super.onPostExecute(newsBeen); NewsAdapter adapter = new NewsAdapter(MainActivity.this, newsBeen,listView); listView.setAdapter(adapter); } } //*&*JSON解析网页获取数据 private List getJsonData(String url) { List newsBeanList = new ArrayList<>(); try { String jsonString = readSteam(new URL(url).openStream()); Log.i("DATA", jsonString); JSONObject jsonObject; NewsBean newsBean; try { jsonObject = new JSONObject(jsonString); JSONArray jsonArray = jsonObject.getJSONArray("data"); for (int i = 0; i < jsonArray.length(); i++) { jsonObject = jsonArray.getJSONObject(i); newsBean = new NewsBean(); newsBean.content = jsonObject.getString("description"); newsBean.title = jsonObject.getString("name"); newsBean.viewUrl = jsonObject.getString("picSmall"); newsBeanList.add(newsBean); } } catch (JSONException e) { e.printStackTrace(); } } catch (IOException e) { e.printStackTrace(); } return newsBeanList; } //*&*读取数据流 private String readSteam(InputStream is) { InputStreamReader isr; String result = ""; try { String line = ""; isr = new InputStreamReader(is, "utf-8"); BufferedReader br = new BufferedReader(isr); try { while ((line = br.readLine()) != null) { result += line; } } catch (IOException e) { e.printStackTrace(); } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return result; } }
3、自定义适配器(在此处设置滑动监听,以此来判断什么时候加载资源)
package com.example.leixiansheng.news; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AbsListView; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; import java.util.List; /** * Created by Leixiansheng on 2017/3/21. */ public class NewsAdapter extends BaseAdapter implements AbsListView.OnScrollListener{ private ListnewsBeanList; private LayoutInflater inflater; private ImageLoader imageLoader; //图片加载 private int start; //第一个元素 private int end; //最后一个元素 private boolean isFirstIn; //是否第一次进入 public static String[] URLS; //所有资源 public NewsAdapter(Context context, List 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); //*&*设置标签,避免快速滑动listview出现位置误差 viewHolder.imageView.setTag(url); // new ImageLoader().showImageByThread(viewHolder.imageView, url); imageLoader.showImageViewByAsyncTask(viewHolder.imageView,url); viewHolder.title.setText(newsBeanList.get(i).title); viewHolder.content.setText(newsBeanList.get(i).content); return view; } //*&*优化 class ViewHolder { public TextView title; public TextView content; private ImageView imageView; } //滑动监听 @Override public void onScrollStateChanged(AbsListView absListView, int i) { if (i == SCROLL_STATE_IDLE) { //停止状态:加载图片 imageLoader.loadImages(start, end); } else { //滑动状态:停止加载 imageLoader.cancelAllTasks(); } } /** * * @param absListView * @param i 第一个元素 * @param i1 元素数量 * @param i2 */ @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; /** * Created by Leixiansheng on 2017/3/21. */ public class ImageLoader { private ImageView mImageView; private String mUrl; //*&*创建缓存 private LruCachelruCache; private ListView listview; private Set mTask; public ImageLoader(ListView listview) { this.listview = listview; mTask = new HashSet<>(); //*&*获取最大内存 int maxMemory = (int) Runtime.getRuntime().maxMemory(); //设置缓存大小 int lruCacheSize = maxMemory / 4; lruCache = new LruCache (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 { // 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、注册声明权限
关于怎么Android应用中实现一个APP显示界面滑动优化就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。