当前位置: 首页 > news >正文

dedecms蓝色企业网站模板免费下载百度电脑版官方下载

dedecms蓝色企业网站模板免费下载,百度电脑版官方下载,成都微网站建设,品牌网站建设优化公司哪家好本文内容 环境 项目结构 演示自定义 ListView 显示网络上 JSON 歌曲列表 参考资料 本文最开始看的是一个国人翻译的文章#xff0c;没有源代码可下载#xff0c;根据文中提供的代码片段#xff0c;自己新建的项目#xff08;比较可恶的是#xff0c;没有图标图片资源…本文内容 环境 项目结构 演示自定义 ListView 显示网络上 JSON 歌曲列表 参考资料 本文最开始看的是一个国人翻译的文章没有源代码可下载根据文中提供的代码片段自己新建的项目比较可恶的是没有图标图片资源只能自己乱搞但程序不是很稳定有时能显示出列表中的缩略图有时显示不出来还在主线程访问了网络。但在文章评论中作者给出英文原文链接本来想这下没事了吧结果下载源代码运行后还是有问题~仔细看英文原文原来他也是根据 Github 上一个项目搞的只是添加了式样以及显示完整的歌曲列表包括歌曲名、歌手名、缩略图、时长。 看来只能自己研究了式样用英文原文的改变主要有两个一个是英文原文的网络歌曲列表是 .xml 格式文件本文采用 .json 格式二是调整了网络访问。 环境 Windows 2008 R2 64 位 Eclipse ADT V22.6.2Android 4.4.3 SAMSUNG GT-I9008LAndroid OS 2.2.2   项目结构 图 1 项目结构   演示自定义 ListView 显示网络上 JSON 歌曲列表 演示运行结果如下所示本文只给出核心代码。点击此处下载自己调试一下。 图 2 主程序 自定义式样 gradient_bg.xml、gradient_bg_hover.xml、list_selector.xml 以及 image_bg.xml定义列表背景选中和没选中的式样以及缩略图的背景。 main.xml 主页面及其后台 main.xml ?xml version1.0 encodingutf-8? LinearLayout xmlns:androidhttp://schemas.android.com/apk/res/android android:layout_widthfill_parent android:layout_heightfill_parent android:orientationvertical   ListView android:idid/mylist android:layout_widthfill_parent android:layout_heightwrap_content android:divider#b5b5b5 android:dividerHeight1dp android:listSelectordrawable/list_selector /   Button android:idid/btn_clear android:layout_widthfill_parent android:layout_heightwrap_content android:text清除缓存, 重新加载 /   /LinearLayout CustomizedListView.java package com.example.androidhive;   import org.json.JSONArray;   import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.widget.ListView;   public class CustomizedListView extends Activity { String url http://files.cnblogs.com/liuning8023/Android_Music_Demo_json_array.xml;   ListView list; LazyAdapter adapter; /* Button btn; */   private Handler handler new Handler() { Override public void handleMessage(Message msg) { if (msg.what 0x123) { Request rq new Request(); JSONArray jsonArr rq.getJsonFromUrl(url); list (ListView) findViewById(R.id.mylist); adapter new LazyAdapter(CustomizedListView.this, this, jsonArr); list.setAdapter(adapter);   /* * btn (Button) findViewById(R.id.btn_clear); * btn.setOnClickListener(new OnClickListener() { * * Override public void onClick(View arg0) { * adapter.imageLoader.clearCache(); * adapter.notifyDataSetChanged(); } }); */ } super.handleMessage(msg); } };   Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);   new Thread(new Runnable() { Override public void run() { Message msg new Message(); msg.what 0x123; handler.sendMessage(msg); } }).start();   }   } list_row.xml ?xml version1.0 encodingutf-8? RelativeLayout xmlns:androidhttp://schemas.android.com/apk/res/android android:layout_widthfill_parent android:layout_heightwrap_content android:backgrounddrawable/list_selector android:orientationhorizontal android:padding5dip   !-- ListRow Left sied Thumbnail image -- LinearLayout android:idid/thumbnail android:layout_widthwrap_content android:layout_heightwrap_content android:padding3dip android:layout_alignParentLefttrue android:backgrounddrawable/image_bg android:layout_marginRight5dip ImageView android:idid/list_image android:layout_width50dip android:layout_height50dip android:srcdrawable/rihanna/ /LinearLayout !-- Title Of Song-- TextView android:idid/title android:layout_widthwrap_content android:layout_heightwrap_content android:layout_alignTopid/thumbnail android:layout_toRightOfid/thumbnail android:textRihanna Love the way lie android:textColor#040404 android:typefacesans android:textSize15dip android:textStylebold/   !-- Artist Name -- TextView android:idid/artist android:layout_widthfill_parent android:layout_heightwrap_content android:layout_belowid/title android:textColor#343434 android:textSize10dip android:layout_marginTop1dip android:layout_toRightOfid/thumbnail android:textJust gona stand there and ... /   !-- Rightend Duration -- TextView android:idid/duration android:layout_widthwrap_content android:layout_heightwrap_content android:layout_alignParentRighttrue android:layout_alignTopid/title android:gravityright android:text5:45 android:layout_marginRight5dip android:textSize10dip android:textColor#10bcc9 android:textStylebold/ !-- Rightend Arrow -- ImageView android:layout_widthwrap_content android:layout_heightwrap_content android:srcdrawable/arrow android:layout_alignParentRighttrue android:layout_centerVerticaltrue/   /RelativeLayout LazyAdapter.java package com.example.androidhive;   import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject;   import android.app.Activity; import android.content.Context; import android.os.Handler; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView;   public class LazyAdapter extends BaseAdapter {   private Activity activity; private JSONArray data; private static LayoutInflater inflater null; public ImageLoader imageLoader;   public LazyAdapter(Activity a, Handler handler, JSONArray jsonArr) { activity a; data jsonArr; inflater (LayoutInflater) activity .getSystemService(Context.LAYOUT_INFLATER_SERVICE); imageLoader new ImageLoader(activity.getApplicationContext()); }   public int getCount() { return data null ? 0 : data.length(); }   public Object getItem(int position) { return position; }   public long getItemId(int position) { return position; }   public View getView(int position, View convertView, ViewGroup parent) { View vi convertView; if (convertView null) vi inflater.inflate(R.layout.list_row, null);   JSONObject song null;   ImageView image (ImageView) vi.findViewById(R.id.list_image); TextView tvartist (TextView) vi.findViewById(R.id.artist); TextView tvtitle (TextView) vi.findViewById(R.id.title); TextView tvduration (TextView) vi.findViewById(R.id.duration);   try { song data.getJSONObject(position); imageLoader.DisplayImage(song.getString(thumb_url), image); tvartist.setText(song.getString(artist)); tvtitle.setText(song.getString(title)); tvduration.setText(song.getString(duration)); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); }   return vi; } } 其他工具类 FileCache.java 在外存缓存图片 package com.example.androidhive;   import java.io.File; import android.content.Context;   public class FileCache {   private File cacheDir;   public FileCache(Context context) { // Find the dir to save cached images if (android.os.Environment.getExternalStorageState().equals( android.os.Environment.MEDIA_MOUNTED)) cacheDir new File( android.os.Environment.getExternalStorageDirectory(), LazyList); else cacheDir context.getCacheDir(); if (!cacheDir.exists()) cacheDir.mkdirs(); }   public File getFile(String url) { // I identify images by hashcode. Not a perfect solution, good for the // demo. String filename String.valueOf(url.hashCode()); // Another possible solution (thanks to grantland) // String filename URLEncoder.encode(url); File f new File(cacheDir, filename); return f;   }   public void clear() { File[] files cacheDir.listFiles(); if (files null) return; for (File f : files) f.delete(); }   } MemoryCache.java 在内存缓存图片 package com.example.androidhive;   import java.util.Collections; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.Map; import java.util.Map.Entry; import android.graphics.Bitmap; import android.util.Log;   public class MemoryCache {   private static final String TAG MemoryCache; private MapString, Bitmap cacheCollections.synchronizedMap( new LinkedHashMapString, Bitmap(10,1.5f,true));//Last argument true for LRU ordering private long size0;//current allocated size private long limit1000000;//max memory in bytes   public MemoryCache(){ //use 25% of available heap size setLimit(Runtime.getRuntime().maxMemory()/4); } public void setLimit(long new_limit){ limitnew_limit; Log.i(TAG, MemoryCache will use up to limit/1024./1024.MB); }   public Bitmap get(String id){ try{ if(!cache.containsKey(id)) return null; //NullPointerException sometimes happen here http://code.google.com/p/osmdroid/issues/detail?id78 return cache.get(id); }catch(NullPointerException ex){ ex.printStackTrace(); return null; } }   public void put(String id, Bitmap bitmap){ try{ if(cache.containsKey(id)) size-getSizeInBytes(cache.get(id)); cache.put(id, bitmap); sizegetSizeInBytes(bitmap); checkSize(); }catch(Throwable th){ th.printStackTrace(); } } private void checkSize() { Log.i(TAG, cache sizesize lengthcache.size()); if(sizelimit){ IteratorEntryString, Bitmap itercache.entrySet().iterator();//least recently accessed item will be the first one iterated while(iter.hasNext()){ EntryString, Bitmap entryiter.next(); size-getSizeInBytes(entry.getValue()); iter.remove(); if(sizelimit) break; } Log.i(TAG, Clean cache. New size cache.size()); } }   public void clear() { try{ //NullPointerException sometimes happen here http://code.google.com/p/osmdroid/issues/detail?id78 cache.clear(); size0; }catch(NullPointerException ex){ ex.printStackTrace(); } }   long getSizeInBytes(Bitmap bitmap) { if(bitmapnull) return 0; return bitmap.getRowBytes() * bitmap.getHeight(); } } ImageLoader.java 加载图片并缓存 package com.example.androidhive;   import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; import java.util.Collections; import java.util.Map; import java.util.WeakHashMap; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors;   import android.os.Handler; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.widget.ImageView;   public class ImageLoader {   MemoryCache memoryCache new MemoryCache(); FileCache fileCache; private MapImageView, String imageViews Collections .synchronizedMap(new WeakHashMapImageView, String()); ExecutorService executorService; Handler handler new Handler();// handler to display images in UI thread   public ImageLoader(Context context) { fileCache new FileCache(context); executorService Executors.newFixedThreadPool(5); }   final int stub_id R.drawable.stub;   /* * 显示图片 若缓存存在则显示否则获取 */ public void DisplayImage(String url, ImageView imageView) { imageViews.put(imageView, url); Bitmap bitmap memoryCache.get(url); // 根据 url 从内存获得图片 if (bitmap ! null) imageView.setImageBitmap(bitmap); else { queuePhoto(url, imageView); imageView.setImageResource(stub_id); } }   private void queuePhoto(String url, ImageView imageView) { PhotoToLoad p new PhotoToLoad(url, imageView); executorService.submit(new PhotosLoader(p)); }   private Bitmap getBitmap(String url) { File f fileCache.getFile(url);   // from SD cache Bitmap b decodeFile(f); if (b ! null) return b;   // from web try { Bitmap bitmap null; URL imageUrl new URL(url); HttpURLConnection conn (HttpURLConnection) imageUrl .openConnection();   conn.setRequestMethod(GET); // Sets the flag indicating whether this URLConnection allows input. // conn.setDoInput(true); conn.setConnectTimeout(3000); conn.setReadTimeout(3000); // Flag to define whether the protocol will automatically follow // redirects or not. conn.setInstanceFollowRedirects(true); int response_code conn.getResponseCode(); if (response_code 200) { InputStream is conn.getInputStream(); OutputStream os new FileOutputStream(f); StreamUtils.CopyStream(is, os); os.close(); conn.disconnect(); bitmap decodeFile(f); return bitmap; } else { conn.disconnect(); return null; }   } catch (Throwable ex) { ex.printStackTrace(); if (ex instanceof OutOfMemoryError) memoryCache.clear(); return null; } }   // decodes image and scales it to reduce memory consumption private Bitmap decodeFile(File f) { try { // decode image size BitmapFactory.Options o new BitmapFactory.Options(); o.inJustDecodeBounds true; FileInputStream stream1 new FileInputStream(f); BitmapFactory.decodeStream(stream1, null, o); stream1.close();   // Find the correct scale value. It should be the power of 2. final int REQUIRED_SIZE 70; int width_tmp o.outWidth, height_tmp o.outHeight; int scale 1; while (true) { if (width_tmp / 2 REQUIRED_SIZE || height_tmp / 2 REQUIRED_SIZE) break; width_tmp / 2; height_tmp / 2; scale * 2; }   // decode with inSampleSize BitmapFactory.Options o2 new BitmapFactory.Options(); o2.inSampleSize scale; FileInputStream stream2 new FileInputStream(f); Bitmap bitmap BitmapFactory.decodeStream(stream2, null, o2); stream2.close(); return bitmap; } catch (FileNotFoundException e) { } catch (IOException e) { e.printStackTrace(); } return null; }   // Task for the queue private class PhotoToLoad { public String url; public ImageView imageView;   public PhotoToLoad(String u, ImageView i) { url u; imageView i; } }   class PhotosLoader implements Runnable { PhotoToLoad photoToLoad;   PhotosLoader(PhotoToLoad photoToLoad) { this.photoToLoad photoToLoad; }   Override public void run() { try { if (imageViewReused(photoToLoad)) return; Bitmap bmp getBitmap(photoToLoad.url); memoryCache.put(photoToLoad.url, bmp); if (imageViewReused(photoToLoad)) return; BitmapDisplayer bd new BitmapDisplayer(bmp, photoToLoad); handler.post(bd); } catch (Throwable th) { th.printStackTrace(); } } }   boolean imageViewReused(PhotoToLoad photoToLoad) { String tag imageViews.get(photoToLoad.imageView); if (tag null || !tag.equals(photoToLoad.url)) return true; return false; }   // Used to display bitmap in the UI thread class BitmapDisplayer implements Runnable { Bitmap bitmap; PhotoToLoad photoToLoad;   public BitmapDisplayer(Bitmap b, PhotoToLoad p) { bitmap b; photoToLoad p; }   public void run() { if (imageViewReused(photoToLoad)) return; if (bitmap ! null) photoToLoad.imageView.setImageBitmap(bitmap); else photoToLoad.imageView.setImageResource(stub_id); } }   public void clearCache() { memoryCache.clear(); fileCache.clear(); }   } Request.java 访问网络 package com.example.androidhive;   import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.URL;   import org.json.JSONArray; import org.json.JSONException;   public class Request {   public Request() { }   public JSONArray getJsonFromUrl(String urlStr) {   try { URL url new URL(urlStr); HttpURLConnection conn (HttpURLConnection) url.openConnection(); conn.setRequestMethod(GET); // Sets the flag indicating whether this URLConnection allows input. // conn.setDoInput(true); conn.setConnectTimeout(3000); conn.setReadTimeout(3000); // Flag to define whether the protocol will automatically follow // redirects or not. conn.setInstanceFollowRedirects(true);   int response_code conn.getResponseCode(); if (response_code 200) { InputStream in conn.getInputStream(); InputStreamReader inputReader new InputStreamReader(in); BufferedReader buffReader new BufferedReader(inputReader);   String line , JsonStr ; while ((line buffReader.readLine()) ! null) { JsonStr line; } JSONArray jsonArray new JSONArray(JsonStr); return jsonArray; } else { conn.disconnect(); return null; }   } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } } StreamUtils.java 缓存时的I/O操作 package com.example.androidhive;   import java.io.InputStream; import java.io.OutputStream;   public class StreamUtils { public static void CopyStream(InputStream is, OutputStream os) { final int buffer_size1024; try { byte[] bytesnew byte[buffer_size]; for(;;) { int countis.read(bytes, 0, buffer_size); if(count-1) break; os.write(bytes, 0, count); } } catch(Exception ex){} } }   参考资料 Github LazyList androidhive android-custom-listview-with-imgae-and-text androidhive android android-custom-listview-with-image-and-text 译文 这三个链接的关系是第二个链接的演示是根据第一个链接完成的第三个链接翻译的第二个链接。但遗憾的是英文原文是有bug的程序运行不稳定。 Android ListView 和 ***Adapter 从本地/网络获取歌曲列表 Android LazyList 从网络获取图片并缓存   下载 Demo转载于:https://www.cnblogs.com/liuning8023/p/3829914.html
http://www.pierceye.com/news/238148/

相关文章:

  • 好的公司网站有什么用烟台建设集团招聘信息网站
  • 网站制作需要多长时间网站代建设费用
  • 淘宝客网站设计台州建设银行官方网站
  • 婚纱网站建设规划书2023全国企业公司大黄页
  • 网站seo的关键词排名怎么做的wordpress 在线留言
  • 建一个c2c网站要多少钱小程序云开发文档
  • asp网站合法上虞网站设计
  • 网站 用什么数据库蛋糕店网站建设方案
  • 网站上的动效是用ae做的网站开发实训小结
  • wordpress建站怎么上传网站没有备案信息该怎么做
  • 沈阳网站推广有什么技巧软件开发工具通常也称为什么工具
  • 黑龙江龙采做网站如何网站建设制作解决方案
  • 百度推广自己做网站吗网页设计软件下载网站
  • wordpress内核源码分析南宁网站优化推广
  • 物流网站做那个好服务器怎么安装WordPress
  • 网站开发怎么兼容浏览器中国优秀设计网站有哪些内容
  • 黄冈网站官方登录平台做网站的条件
  • 潍坊网站建设推广公司网站建设类的手机软件
  • 建设小学网站建设网站代理
  • 怎么查看网站根目录网站建设费记什么科目
  • 文昌市规划建设管理局网站网站与个人网站
  • 昆明网站建设推荐q479185700上墙现在最火的推广平台有哪些
  • 长兴县城乡建设局网站wordpress的留言功能
  • 建设企业网站地址asp.net 4.0网站开...
  • 制作个人网站步骤提升学历励志语录
  • 福州建站服务管理页面布局标准格式
  • 做一个公司网站一般需要多少钱营销型网站功能表
  • 为什么菜市场不可以做网站河南阿里巴巴网站建设
  • asp.net动态的网站开发手机海报制作免费软件
  • 网站建设前准备龙岗网站优化公司案例