- 浏览: 541582 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
tangyunliang:
大哥你太历害了谢谢
Android基于XMPP Smack Openfire开发IM【四】初步实现两个客户端通信 -
u013015029:
LZ,请问下,在// 添加消息到聊天窗口 , 这里获取Ed ...
Android基于XMPP Smack Openfire开发IM【四】初步实现两个客户端通信 -
endual:
怎么保持会话,我搞不懂啊
Android基于XMPP Smack Openfire开发IM【一】登录openfire服务器 -
donala_zq:
显示:[2013-11-30 11:50:36 - Andro ...
android-----------新浪微博 -
donala_zq:
哥,运行不了啊
android-----------新浪微博
先上效果图:
[img]
[/img]
嘎、换个图片吧 这张好看些 哈哈
[img]
[/img]
工程结构图:
[img]
[/img]
AsyncImageLoader:
ImageUtil
TestListViewActivity
main_foot.xml
main_list.xml
main.xml
AndroidManifest.xml
[img]
[/img]
嘎、换个图片吧 这张好看些 哈哈
[img]
[/img]
工程结构图:
[img]
[/img]
AsyncImageLoader:
package cn.anycall.testlist; import java.lang.ref.SoftReference; import java.util.HashMap; import android.graphics.drawable.Drawable; import android.os.Handler; import android.os.Message; import android.widget.ImageView; public class AsyncImageLoader { //SoftReference是软引用,是为了更好的为了系统回收变量 private static HashMap<String, SoftReference<Drawable>> imageCache; static { imageCache = new HashMap<String, SoftReference<Drawable>>(); } public AsyncImageLoader() { } public Drawable loadDrawable(final String imageUrl,final ImageView imageView, final ImageCallback imageCallback){ if (imageCache.containsKey(imageUrl)) { //从缓存中获取 SoftReference<Drawable> softReference = imageCache.get(imageUrl); Drawable drawable = softReference.get(); System.out.println("111111111111111111111111111111"); if (drawable != null) { System.out.println("11111111111122222222211111111111111111"); return drawable; } } final Handler handler = new Handler() { public void handleMessage(Message message) { imageCallback.imageLoaded((Drawable) message.obj, imageView,imageUrl); } }; //建立新一个新的线程下载图片 new Thread() { @Override public void run() { System.out.println("11111111111133333333333211111111111111111"); Drawable drawable = null; try { drawable = ImageUtil.geRoundDrawableFromUrl(imageUrl, 20); } catch (Exception e) { e.printStackTrace(); } imageCache.put(imageUrl, new SoftReference<Drawable>(drawable)); Message message = handler.obtainMessage(0, drawable); handler.sendMessage(message); } }.start(); return null; } //回调接口 public interface ImageCallback { public void imageLoaded(Drawable imageDrawable,ImageView imageView, String imageUrl); } }
ImageUtil
package cn.anycall.testlist; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import android.graphics.Bitmap; import android.graphics.Bitmap.Config; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.ColorMatrix; import android.graphics.ColorMatrixColorFilter; import android.graphics.Paint; import android.graphics.PixelFormat; import android.graphics.PorterDuff.Mode; import android.graphics.PorterDuffXfermode; import android.graphics.Rect; import android.graphics.RectF; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; public class ImageUtil { public static InputStream getRequest(String path) throws Exception { URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(5000); if (conn.getResponseCode() == 200){ return conn.getInputStream(); } return null; } public static byte[] readInputStream(InputStream inStream) throws Exception { ByteArrayOutputStream outSteam = new ByteArrayOutputStream(); byte[] buffer = new byte[4096]; int len = 0; while ((len = inStream.read(buffer)) != -1) { outSteam.write(buffer, 0, len); } outSteam.close(); inStream.close(); return outSteam.toByteArray(); } public static Drawable loadImageFromUrl(String url){ URL m; InputStream i = null; try { m = new URL(url); i = (InputStream) m.getContent(); } catch (MalformedURLException e1) { e1.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } Drawable d = Drawable.createFromStream(i, "src"); return d; } public static Drawable getDrawableFromUrl(String url) throws Exception{ return Drawable.createFromStream(getRequest(url),null); } public static Bitmap getBitmapFromUrl(String url) throws Exception{ byte[] bytes = getBytesFromUrl(url); return byteToBitmap(bytes); } public static Bitmap getRoundBitmapFromUrl(String url,int pixels) throws Exception{ byte[] bytes = getBytesFromUrl(url); Bitmap bitmap = byteToBitmap(bytes); return toRoundCorner(bitmap, pixels); } public static Drawable geRoundDrawableFromUrl(String url,int pixels) throws Exception{ byte[] bytes = getBytesFromUrl(url); BitmapDrawable bitmapDrawable = (BitmapDrawable)byteToDrawable(bytes); return toRoundCorner(bitmapDrawable, pixels); } public static byte[] getBytesFromUrl(String url) throws Exception{ return readInputStream(getRequest(url)); } public static Bitmap byteToBitmap(byte[] byteArray){ if(byteArray.length!=0){ return BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length); } else { return null; } } public static Drawable byteToDrawable(byte[] byteArray){ ByteArrayInputStream ins = new ByteArrayInputStream(byteArray); return Drawable.createFromStream(ins, null); } public static byte[] Bitmap2Bytes(Bitmap bm){ ByteArrayOutputStream baos = new ByteArrayOutputStream(); bm.compress(Bitmap.CompressFormat.PNG, 100, baos); return baos.toByteArray(); } public static Bitmap drawableToBitmap(Drawable drawable) { Bitmap bitmap = Bitmap .createBitmap( drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565); Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); drawable.draw(canvas); return bitmap; } /** * 图片去色,返回灰度图片 * @param bmpOriginal 传入的图片 * @return 去色后的图片 */ public static Bitmap toGrayscale(Bitmap bmpOriginal) { int width, height; height = bmpOriginal.getHeight(); width = bmpOriginal.getWidth(); Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565); Canvas c = new Canvas(bmpGrayscale); Paint paint = new Paint(); ColorMatrix cm = new ColorMatrix(); cm.setSaturation(0); ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm); paint.setColorFilter(f); c.drawBitmap(bmpOriginal, 0, 0, paint); return bmpGrayscale; } /** * 去色同时加圆角 * @param bmpOriginal 原图 * @param pixels 圆角弧度 * @return 修改后的图片 */ public static Bitmap toGrayscale(Bitmap bmpOriginal, int pixels) { return toRoundCorner(toGrayscale(bmpOriginal), pixels); } /** * 把图片变成圆角 * @param bitmap 需要修改的图片 * @param pixels 圆角的弧度 * @return 圆角图片 */ public static Bitmap toRoundCorner(Bitmap bitmap, int pixels) { Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888); Canvas canvas = new Canvas(output); final int color = 0xff424242; final Paint paint = new Paint(); final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); final RectF rectF = new RectF(rect); final float roundPx = pixels; paint.setAntiAlias(true); canvas.drawARGB(0, 0, 0, 0); paint.setColor(color); canvas.drawRoundRect(rectF, roundPx, roundPx, paint); paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); canvas.drawBitmap(bitmap, rect, rect, paint); return output; } /** * 使圆角功能支持BitampDrawable * @param bitmapDrawable * @param pixels * @return */ public static BitmapDrawable toRoundCorner(BitmapDrawable bitmapDrawable, int pixels) { Bitmap bitmap = bitmapDrawable.getBitmap(); bitmapDrawable = new BitmapDrawable(toRoundCorner(bitmap, pixels)); return bitmapDrawable; } }
TestListViewActivity
package cn.anycall.testlist; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.app.ListActivity; import android.content.Context; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.os.Handler; import android.os.Looper; import android.os.Message; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.Button; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.TextView; import cn.anycall.testlist.AsyncImageLoader.ImageCallback; public class TestListViewActivity extends ListActivity { private List<Map<String, Object>> mData = new ArrayList<Map<String, Object>>(); public final class ViewHolder { public ImageView img; public TextView title; public TextView info; } public List<HashMap<String, String>> data = new ArrayList<HashMap<String, String>>(); public int a = 0; public Button button; public LinearLayout layloading; public MyHandler myHandler; public MyAdapter adapter ; private AsyncImageLoader asyncImageLoader = new AsyncImageLoader(); public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); myHandler = new MyHandler(); ListView listView = getListView();// 得到ListView LinearLayout listFooter = (LinearLayout) LayoutInflater.from(this) .inflate(R.layout.main_foot, null); listView.addFooterView(listFooter);// 添加FooterView button = (Button) findViewById(R.id.more); layloading = (LinearLayout) findViewById(R.id.loading); button.setVisibility(View.VISIBLE); layloading.setVisibility(View.GONE); adapter = new MyAdapter(this); setListAdapter(adapter); button.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { //起一个线程更新后台数据 MyThread m = new MyThread(); new Thread(m).start(); } }); for(int i=0;i<20;i++){ a++; Map<String, Object> map = new HashMap<String, Object>(); map.put("img", R.drawable.icon); map.put("title", "G"+a); map.put("info", "google"+a); mData.add(map); } } public class MyAdapter extends BaseAdapter { private LayoutInflater mInflater; public MyAdapter(Context context) { this.mInflater = LayoutInflater.from(context); } public int getCount() { return mData.size(); } public Object getItem(int arg0) { return null; } public long getItemId(int arg0) { return 0; } public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null; if (convertView == null) { holder = new ViewHolder(); convertView = mInflater.inflate(R.layout.main_list, null); holder.img = (ImageView)convertView.findViewById(R.id.img); holder.title = (TextView) convertView .findViewById(R.id.listtitle); holder.info = (TextView) convertView .findViewById(R.id.listtext); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } //holder.img.setBackgroundResource((Integer)mData.get(position).get("img")); holder.title.setText((String) mData.get(position).get("title")); holder.info.setText((String) mData.get(position).get("info")); //异步加载图片 Drawable cachedImage = asyncImageLoader.loadDrawable("http://t02.pic.sogou.com/eb95234678ecb28d.jpg",holder.img, new ImageCallback(){ public void imageLoaded(Drawable imageDrawable,ImageView imageView, String imageUrl) { imageView.setImageDrawable(imageDrawable); } }); System.out.println(cachedImage); if (cachedImage == null) { holder.img.setImageResource(R.drawable.icon); } else { holder.img.setImageDrawable(cachedImage); } return convertView; } } //更新后台数据 class MyThread implements Runnable { public void run() { String msglist = "1"; Message msg = new Message(); Bundle b = new Bundle();// 存放数据 b.putString("rmsg", msglist); msg.setData(b); TestListViewActivity.this.myHandler.sendMessage(msg); // 向Handler发送消息,更新UI try { Thread.sleep(5000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } msglist = "2"; msg = new Message(); b = new Bundle();// 存放数据 b.putString("rmsg", msglist); msg.setData(b); TestListViewActivity.this.myHandler.sendMessage(msg); // 向Handler发送消息,更新UI } } class MyHandler extends Handler { public MyHandler() { } public MyHandler(Looper L) { super(L); } // 子类必须重写此方法,接受数据 @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub super.handleMessage(msg); // 此处可以更新UI Bundle b = msg.getData(); String rmsg = b.getString("rmsg"); if ("1".equals(rmsg)) { // do nothing button.setVisibility(View.GONE); layloading.setVisibility(View.VISIBLE); }else if ("2".equals(rmsg)) { try { Thread.sleep(3000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } for(int i=0;i<20;i++){ a++; Map<String, Object> map = new HashMap<String, Object>(); map.put("img", R.drawable.icon); map.put("title", "G"+a); map.put("info", "google"+a); mData.add(map); } button.setVisibility(View.VISIBLE); layloading.setVisibility(View.GONE); adapter.notifyDataSetChanged(); } } } }
main_foot.xml
<?xml version="1.0" encoding="UTF-8"?> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:minHeight="?android:listPreferredItemHeight" xmlns:android="http://schemas.android.com/apk/res/android" android:background="#FFFFFF" > <Button android:textSize="16.0sp" android:textColor="#ff545454" android:gravity="center" android:id="@+id/more" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="加载下20条" /> <LinearLayout android:gravity="center" android:layout_gravity="center" android:orientation="horizontal" android:id="@+id/loading" android:layout_width="fill_parent" android:layout_height="fill_parent"> <ProgressBar android:layout_gravity="center_vertical" android:id="@+id/footprogress" android:layout_width="wrap_content" android:layout_height="wrap_content" android:indeterminateBehavior="repeat" style="?android:progressBarStyleSmallInverse" /> <TextView android:textColor="#ff000000" android:gravity="left|center" android:padding="3.0px" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="加载中" /> </LinearLayout> </LinearLayout>
main_list.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout android:id="@+id/RelativeLayout01" android:layout_width="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content" android:paddingBottom="4dip" android:paddingLeft="12dip" android:paddingRight="12dip" android:background="#FFFFFF" > <ImageView android:paddingTop="12dip" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_width="100dp" android:layout_height="100dp" android:id="@+id/img" /> <TextView android:text="TextView01" android:layout_height="wrap_content" android:textSize="20dip" android:layout_width="fill_parent" android:id="@+id/listtitle" android:layout_toRightOf="@id/img" /> <TextView android:text="TextView02" android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/listtext" android:layout_toRightOf="@id/img" android:layout_below="@id/listtitle" /> </RelativeLayout>
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" > <ListView android:id="@id/android:list" android:layout_width="fill_parent" android:layout_height="wrap_content" android:drawSelectorOnTop="false" android:footerDividersEnabled="false" android:scrollbars="vertical" /> </LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="cn.anycall.testlist" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="4" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".TestListViewActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <!-- 允许应用打开网络套接口 --> <uses-permission android:name="android.permission.INTERNET"></uses-permission> </manifest>
发表评论
-
Android中如何模拟一次点击(touch)事件
2014-05-06 10:41 0在Android中有时需要模拟某一个View的touch事件, ... -
Android程序Crash时的异常上报
2014-04-28 18:15 0http://blog.csdn.net/singwhatiw ... -
android程序中证书签名校验的方法
2014-04-28 17:58 2033android程序中证书签名校验的方法一 2013-02 ... -
MD5理解错了,哎
2014-03-17 14:14 0MD5只对数据加密是无法解密的,也就是说,你把100加密后,就 ... -
Android 获取网络时间
2014-03-12 11:42 2053Android 获取网络时间 在网上看到的最常见的方式有: ... -
SQLite清空表并将自增列归零
2014-03-05 18:02 1573SQLite清空表并将自增列归零 作者:Zhu Yanfeng ... -
Handler小看一下
2013-11-11 16:42 0android handler调用post方法还是阻塞 su ... -
Frame Animation小看一下
2013-10-12 16:30 829Demo运行效果图: 源码: -
动画小学一下
2013-10-12 16:14 756转自: http://www.eoeandroid.com/f ... -
Android 动画之ScaleAnimation应用详解
2013-10-12 15:49 1034===============eoeAndroid社区推荐:= ... -
android开发中的一个工具类
2013-06-19 16:04 0package com.wanpu.login.dialog; ... -
android TextView怎么设置个别字体颜色并换行?
2013-06-20 09:25 1718(1)、TextView 设置个别字体颜色 TextView ... -
Android开发之文件下载,状态时显示下载进度,点击自动安装
2013-05-07 15:38 1471在进行软件升级时,需要进行文件下载,在这里实现自定义的文件下载 ... -
android中的状态保存
2013-04-07 14:21 990package com.zzl.call; import ... -
android动画基础:tween动画
2013-04-06 11:21 1283工程结构图: [img] [/img] 四个动画的xml ... -
面试中遇到的几个问题
2013-06-09 11:56 1040SAX与DOM之间的区别 SAX ( ... -
Android获取其他包的Context实例,然后调用它的方法,反射!!!
2013-03-25 10:32 1239Android中有Context的概念,想必大家都知道。Con ... -
Android的内存机制和常见泄漏情形
2013-03-06 16:55 826一、 Android的内存机制 Android的程序由Ja ... -
JUnit测试小小demo
2013-03-06 16:37 1204运行效果图: [img] [/img] 项目结构图 ... -
android开发中的异常小工具
2013-03-04 15:53 911package com.zzl.tools; impor ...
相关推荐
在“listview添加按钮”的场景下,我们通常会在ListView的底部或者顶部添加一个按钮,以供用户执行特定的操作,比如添加新的数据项。这个功能对于许多应用,如通讯录、任务列表或购物车等,都是至关重要的。 首先,...
为了提供更好的用户体验,当开始加载更多数据时,可以在ListView底部显示一个“加载中”的提示视图,加载完毕后移除。这样用户可以清楚地看到数据正在加载。 8. **错误处理与重试机制**: 如果加载数据过程中出现...
在ListView底部的Footer View中,通常会有一个按钮或触摸区域,用户点击后可以快速回到列表的顶部。这通常通过设置一个OnClickListener来实现,点击事件触发时,可以调用ListView的smoothScrollToPosition(0)方法,...
然后,我们需要为返回顶部按钮添加点击事件,使其在被点击时能将ListView滚动到顶部: ```java topButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { listView....
然而,在实际应用中,我们往往需要在ListView的顶部和底部添加自定义视图,例如广告条、筛选器或者加载更多按钮。本教程将详细介绍如何在ListView中添加头部和底部,以及解决由此引发的点击错位问题。 1. **添加...
"listview分页 -- 底部加载更多"是一个常见的用户交互模式,它提高了用户体验,避免了初次加载时的等待时间,并节省了设备资源。 在Android开发中,实现ListView的分页加载主要有以下几个关键知识点: 1. **...
`Android ListView滚动到底部自动加载数据`是一个常见的功能需求,特别是在实现无限滚动或者分页加载的场景下,比如社交媒体应用、电商应用的商品列表等。当用户滚动到ListView的底部时,系统自动触发加载更多数据的...
Footer View是添加在ListView底部的一个视图,它可以是一行文字、一个按钮或者其他任何类型的视图。添加Footer View的主要目的是为了扩展ListView的功能,使其能够承载一些额外的信息或交互。 步骤1:创建Footer ...
在本文中,我们将深入探讨ListView的动态加载以及如何实现点击底部加载更多的功能。 一、ListView的基本概念 ListView是Android SDK中的一个视图容器,它可以显示多行数据,每一行对应一个数据项。通过适配器...
综上所述,实现ListView底部加载更多的功能,需要理解ListView的工作原理,熟悉Adapter的使用,并能够监听和处理滚动事件。通过这个小例子,开发者可以学习到如何在实际项目中提升用户体验,同时也能掌握更多的...
`ListView中的头尾按钮`这个主题指的是如何在ListView的顶部和底部添加额外的按钮,以提供更多的交互功能。这种设计通常用于实现如加载更多、刷新等功能。下面将详细讲解如何在ListView中实现头尾按钮,并探讨相关...
我们在看微博或是到网上商城如淘宝中购物时,每一个商家都会有很多商品,如果把某商家的所有商品都一次性加载过来,会造成手机卡顿,影响用户体验,所以每次加载少量数据,使用分页的形式,分多页加载,会很好的提高...
首先,将固定视图(如底部按钮)放在最外层布局的底部,然后将ScrollView作为其直接子视图。在ScrollView内部,再放置一个LinearLayout(或其他适当的布局),并将ListView作为LinearLayout的子视图。这样,当...
要实现在ListView底部点击加载更多,我们通常需要一个监听器,当用户滚动到ListView的底部时触发加载更多数据的请求。这可以通过重写ListView的OnScrollListener的onScroll()方法来实现。在onScroll()中,我们需要...
滑到底部自动加载功能(又称Infinite Scroll或Endless Scroll)是许多社交应用、新闻客户端等常见的特性,它允许用户在滚动到底部时自动加载更多的数据,无需手动点击加载按钮。这个特性提高了用户体验,使得用户...
这种功能在社交应用、新闻阅读器、电商应用等场景中非常常见,因为它提供了无缝的用户体验,使得用户无需手动点击加载按钮即可获取新内容。 实现"listview上拉自动加载"主要涉及到以下几个关键知识点: 1. **监听...
这个功能允许用户在滚动到ListView底部时自动加载更多数据,而无需点击任何加载按钮。这种设计模式尤其适用于社交网络、新闻应用和电商应用等需要连续加载新内容的场景。 实现`ListView滑动到底部自动加载`的核心...
当用户点击删除按钮后,通常会弹出一个底部对话框来确认操作。这可以通过使用AlertDialog或者自定义布局来实现。在对话框中,提供“确定”和“取消”按钮,点击“确定”执行删除操作,点击“取消”则关闭对话框不做...
因此,如果在ListView底部添加了一个固定视图,它可能会被误认为是Item的一部分而被复用,从而导致底部View的消失。 要解决这个问题,我们可以采取以下策略: 1. 使用`addFooterView()`方法:ListView提供了`...