xiaoguozi's Blog
Pay it forword - 我并不觉的自豪,我所尝试的事情都失败了······习惯原本生活的人不容易改变,就算现状很糟,他们也很难改变,在过程中,他们还是放弃了······他们一放弃,大家就都是输家······让爱传出去,很困难,也无法预料,人们需要更细心的观察别人,要随时注意才能保护别人,因为他们未必知道自己要什么·····

另外一个问题就是加载速度,如果应用中图片加载速度很慢的话,那么用户同样会等到崩溃。

那么如何处理好图片资源的获取和管理呢?

异步下载

本地缓存

异步下载

大家都知道,在android应用中UI线程5秒没响应的话就会抛出无响应异常,对于远程获取大的资源来说,这种异常还是很容易就会抛出来的,那么怎么避免这种问题的产生。在android中提供两种方法来做这件事情:

启动一个新的线程来获取资源,完成后通过Handler机制发送消息,并在UI线程中处理消息,从而达到在异步线程中获取图片,然后通过Handler Message来更新UI线程的过程。

使用android中提供的AsyncTask来完成。

具体的做法这里就不介绍了,查下API就可以了,或者是google、baidu下。这里主要来说本地缓存。

本地缓存

对于图片资源来说,你不可能让应用每次获取的时候都重新到远程去下载(ListView),这样会浪费资源,但是你又不能让所有图片资源都放到内存 中去(虽然这样加载会比较快),因为图片资源往往会占用很大的内存空间,容易导致OOM。那么如果下载下来的图片保存到SDCard中,下次直接从 SDCard上去获取呢?这也是一种做法,我看了下,还是有不少应用采用这种方式的。采用LRU等一些算法可以保证sdcard被占用的空间只有一小部 分,这样既保证了图片的加载、节省了流量、又使SDCard的空间只占用了一小部分。另外一种做法是资源直接保存在内存中,然后设置过期时间和LRU规 则。

sdcard保存:

在sdcard上开辟一定的空间,需要先判断sdcard上剩余空间是否足够,如果足够的话就可以开辟一些空间,比如10M

当需要获取图片时,就先从sdcard上的目录中去找,如果找到的话,使用该图片,并更新图片最后被使用的时间。如果找不到,通过URL去download

去服务器端下载图片,如果下载成功了,放入到sdcard上,并使用,如果失败了,应该有重试机制。比如3次。

下载成功后保存到sdcard上,需要先判断10M空间是否已经用完,如果没有用完就保存,如果空间不足就根据LRU规则删除一些最近没有被用户的资源。

关键代码:

保存图片到SD卡上

  1. private void saveBmpToSd(Bitmap bm, Stringurl) {
  2. if (bm == null) {
  3. Log.w(TAG, " trying to savenull bitmap");
  4. return;
  5. }
  6. //判断sdcard上的空间
  7. if (FREE_SD_SPACE_NEEDED_TO_CACHE >freeSpaceOnSd()) {
  8. Log.w(TAG, "Low free space onsd, do not cache");
  9. return;
  10. }
  11. String filename =convertUrlToFileName(url);
  12. String dir = getDirectory(filename);
  13. File file = new File(dir +"/" + filename);
  14. try {
  15. file.createNewFile();
  16. OutputStream outStream = newFileOutputStream(file);
  17. bm.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
  18. outStream.flush();
  19. outStream.close();
  20. Log.i(TAG, "Image saved tosd");
  21. } catch (FileNotFoundException e) {
  22. Log.w(TAG,"FileNotFoundException");
  23. } catch (IOException e) {
  24. Log.w(TAG,"IOException");
  25. }
  26. }

计算sdcard上的空间:

  1. /**
  2. * 计算sdcard上的剩余空间
  3. * @return
  4. */
  5. private int freeSpaceOnSd() {
  6. StatFs stat = newStatFs(Environment.getExternalStorageDirectory() .getPath());
  7. double sdFreeMB = ((double)stat.getAvailableBlocks() * (double) stat.getBlockSize()) / MB;
  8. return (int) sdFreeMB;
  9. }

修改文件的最后修改时间

  1. /**
  2. * 修改文件的最后修改时间
  3. * @param dir
  4. * @param fileName
  5. */
  6. private void updateFileTime(String dir,String fileName) {
  7. File file = new File(dir,fileName);
  8. long newModifiedTime =System.currentTimeMillis();
  9. file.setLastModified(newModifiedTime);
  10. }

本地缓存优化

  1. /**
  2. *计算存储目录下的文件大小,当文件总大小大于规定的CACHE_SIZE或者sdcard剩余空间小于FREE_SD_SPACE_NEEDED_TO_CACHE的规定
  3. * 那么删除40%最近没有被使用的文件
  4. * @param dirPath
  5. * @param filename
  6. */
  7. private void removeCache(String dirPath) {
  8. File dir = new File(dirPath);
  9. File[] files = dir.listFiles();
  10. if (files == null) {
  11. return;
  12. }
  13. int dirSize = 0;
  14. for (int i = 0; i < files.length;i++) {
  15. if(files[i].getName().contains(WHOLESALE_CONV)) {
  16. dirSize += files[i].length();
  17. }
  18. }
  19. if (dirSize > CACHE_SIZE * MB ||FREE_SD_SPACE_NEEDED_TO_CACHE > freeSpaceOnSd()) {
  20. int removeFactor = (int) ((0.4 *files.length) + 1);
  21. Arrays.sort(files, newFileLastModifSort());
  22. Log.i(TAG, "Clear some expiredcache files ");
  23. for (int i = 0; i <removeFactor; i++) {
  24. if(files[i].getName().contains(WHOLESALE_CONV)) {
  25. files[i].delete();
  26. }
  27. }
  28. }
  29. }
  30. /**
  31. * 删除过期文件
  32. * @param dirPath
  33. * @param filename
  34. */
  35. private void removeExpiredCache(StringdirPath, String filename) {
  36. File file = new File(dirPath,filename);
  37. if (System.currentTimeMillis() -file.lastModified() > mTimeDiff) {
  38. Log.i(TAG, "Clear some expiredcache files ");
  39. file.delete();
  40. }
  41. }

文件使用时间排序

  1. /**
  2. * TODO 根据文件的最后修改时间进行排序 *
  3. */
  4. classFileLastModifSort implements Comparator<File>{
  5. public int compare(File arg0, File arg1) {
  6. if (arg0.lastModified() >arg1.lastModified()) {
  7. return 1;
  8. } else if (arg0.lastModified() ==arg1.lastModified()) {
  9. return 0;
  10. } else {
  11. return -1;
  12. }
  13. }
  14. }

内存保存:

在内存中保存的话,只能保存一定的量,而不能一直往里面放,需要设置数据的过期时间、LRU等算法。这里有一个方法是把常用的数据放到一个缓存中 (A),不常用的放到另外一个缓存中(B)。当要获取数据时先从A中去获取,如果A中不存在那么再去B中获取。B中的数据主要是A中LRU出来的数据,这 里的内存回收主要针对B内存,从而保持A中的数据可以有效的被命中。

先定义A缓存:

  1. private final HashMap<String, Bitmap>mHardBitmapCache = new LinkedHashMap<String, Bitmap>(HARD_CACHE_CAPACITY/ 2, 0.75f, true) {
  2. @Override
  3. protected booleanremoveEldestEntry(LinkedHashMap.Entry<String, Bitmap> eldest) {
  4. if (size() >HARD_CACHE_CAPACITY) {
  5. //当map的size大于30时,把最近不常用的key放到mSoftBitmapCache中,从而保证mHardBitmapCache的效率
  6. mSoftBitmapCache.put(eldest.getKey(), newSoftReference<Bitmap>(eldest.getValue()));
  7. return true;
  8. } else
  9. return false;
  10. }
  11. };

再定于B缓存:

  1. /**
  2. *当mHardBitmapCache的key大于30的时候,会根据LRU算法把最近没有被使用的key放入到这个缓存中。
  3. *Bitmap使用了SoftReference,当内存空间不足时,此cache中的bitmap会被垃圾回收掉
  4. */
  5. private final staticConcurrentHashMap<String, SoftReference<Bitmap>> mSoftBitmapCache =new ConcurrentHashMap<String,SoftReference<Bitmap>>(HARD_CACHE_CAPACITY / 2);

从缓存中获取数据:

  1. /**
  2. * 从缓存中获取图片
  3. */
  4. private Bitmap getBitmapFromCache(Stringurl) {
  5. // 先从mHardBitmapCache缓存中获取
  6. synchronized (mHardBitmapCache) {
  7. final Bitmap bitmap =mHardBitmapCache.get(url);
  8. if (bitmap != null) {
  9. //如果找到的话,把元素移到linkedhashmap的最前面,从而保证在LRU算法中是最后被删除
  10. mHardBitmapCache.remove(url);
  11. mHardBitmapCache.put(url,bitmap);
  12. return bitmap;
  13. }
  14. }
  15. //如果mHardBitmapCache中找不到,到mSoftBitmapCache中找
  16. SoftReference<Bitmap>bitmapReference = mSoftBitmapCache.get(url);
  17. if (bitmapReference != null) {
  18. final Bitmap bitmap =bitmapReference.get();
  19. if (bitmap != null) {
  20. return bitmap;
  21. } else {
  22. mSoftBitmapCache.remove(url);
  23. }
  24. }
  25. return null;
  26. }

如果缓存中不存在,那么就只能去服务器端去下载:

  1. /**
  2. * 异步下载图片
  3. */
  4. class ImageDownloaderTask extendsAsyncTask<String, Void, Bitmap> {
  5. private static final int IO_BUFFER_SIZE= 4 * 1024;
  6. private String url;
  7. private finalWeakReference<ImageView> imageViewReference;
  8. public ImageDownloaderTask(ImageViewimageView) {
  9. imageViewReference = newWeakReference<ImageView>(imageView);
  10. }
  11. @Override
  12. protected BitmapdoInBackground(String... params) {
  13. final AndroidHttpClient client =AndroidHttpClient.newInstance("Android");
  14. url = params[0];
  15. final HttpGet getRequest = newHttpGet(url);
  16. try {
  17. HttpResponse response =client.execute(getRequest);
  18. final int statusCode =response.getStatusLine().getStatusCode();
  19. if (statusCode !=HttpStatus.SC_OK) {
  20. Log.w(TAG, "从" +url + "中下载图片时出错!,错误码:" + statusCode);
  21. return null;
  22. }
  23. final HttpEntity entity =response.getEntity();
  24. if (entity != null) {
  25. InputStream inputStream =null;
  26. OutputStream outputStream =null;
  27. try {
  28. inputStream =entity.getContent();
  29. finalByteArrayOutputStream dataStream = new ByteArrayOutputStream();
  30. outputStream = newBufferedOutputStream(dataStream, IO_BUFFER_SIZE);
  31. copy(inputStream,outputStream);
  32. outputStream.flush();
  33. final byte[] data =dataStream.toByteArray();
  34. final Bitmap bitmap =BitmapFactory.decodeByteArray(data, 0, data.length);
  35. return bitmap;
  36. } finally {
  37. if (inputStream !=null) {
  38. inputStream.close();
  39. }
  40. if (outputStream !=null) {
  41. outputStream.close();
  42. }
  43. entity.consumeContent();
  44. }
  45. }
  46. } catch (IOException e) {
  47. getRequest.abort();
  48. Log.w(TAG, "I/O errorwhile retrieving bitmap from " + url, e);
  49. } catch (IllegalStateException e) {
  50. getRequest.abort();
  51. Log.w(TAG, "Incorrect URL:" + url);
  52. } catch (Exception e) {
  53. getRequest.abort();
  54. Log.w(TAG, "Error whileretrieving bitmap from " + url, e);
  55. } finally {
  56. if (client != null) {
  57. client.close();
  58. }
  59. }
  60. return null;
  61. }

这是两种做法,还有一些应用在下载的时候使用了线程池和消息队列MQ,对于图片下载的效率要更好一些。有兴趣的同学可以看下。

总结

对于远程图片等相对比较大的资源一定要在异步线程中去获取本地做缓存

posted @ 2012-04-16 11:43 小果子 阅读(1270) | 评论 (2)编辑 收藏
  Java 5的泛型语法已经有太多书讲了,这里不再打字贴书。GP一定有用,不然Java和C#不会约好了似的同时开始支持GP。但大家也清楚,GP和Ruby式的 动态OO语言属于不同的意识形态,如果是一人一票,我想大部分的平民程序员更热衷动态OO语言的平白自然。但如果不准备跳槽到支持JSR223的动态语 言,那还是看看GP吧。

   胡乱总结泛型的四点作用:
   第一是泛化,可以拿个T代表任意类型。 但GP是被C++严苛的静态性逼出来的,落到Java、C#这样的花语平原里----所有对象除几个原始类型外都派生于Object,再加上Java的反射功能,Java的Collection库没有范型一样过得好好的。

   第二是泛型 + 反射,原本因为Java的泛型拿不到T.class而觉得泛型没用,最近才刚刚学到通过反射的API来获取T的Class,后述。

   第三是收敛,就是增加了类型安全,减少了强制类型转换的代码。这点倒是Java Collection历来的弱项。

   第四是可以在编译期搞很多东西,比如MetaProgramming。但除非能完全封闭于框架内部,框架的使用者和扩展者都不用学习这些东西的用法,否则 那就是自绝于人民的票房毒药。C++的MetaProgramming好厉害吧,但对比一下Python拿Meta Programming生造一个Class出来的简便语法,就明白什么才是真正的叫好又叫座。

   所以,作为一个架构设计师,应该使用上述的第2,3项用法,在框架类里配合使用反射和泛型,使得框架的能力更强; 同时采用收敛特性,本着对人民负责的精神,用泛型使框架更加类型安全,更少强制类型转换。
   
   擦拭法避免了Java的流血分裂 :
    大家经常骂Java GP的擦拭法实现,但我觉得多亏于它的中庸特性---如果你用就是范型,不用就是普通Object,避免了Java阵营又要经历一场to be or not to be的分裂。 
    最大的例子莫过Java 5的Collection 框架, 比如有些同学坚持认为自己不会白痴到类型出错,而且难以忍受每个定义的地方都要带一个泛型定义List〈Book〉,不用强制类型转换所省下的代码还不够N处定义花的(对了,java里面还没有tyepdef.....),因此对范型十分不感冒,这时就要齐齐感谢这个搽拭法让你依然可以对一个泛型框架保持非泛型的用法了...

   通过反射获得 T.class:
   
    不知为何书上不怎么讲这个,是差沙告诉我才知道的,最经典的应用见Hibernate wiki的Generic Data Access Objects, 代码如下: 
abstract public class BaseHibernateEntityDao<T> extends HibernateDaoSupport {
 
private Class<T> entityClass;
 
public BaseHibernateEntityDao() {
        entityClass 
=(Class<T>) ((ParameterizedType) getClass()
                                .getGenericSuperclass()).getActualTypeArguments()[0];
    }
 
public T get(Serializable id) {
        T o 
= (T) getHibernateTemplate().get(entityClass, id);
}
}

  精华就是这句了:
Class<T> entityClass = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0]; 

  泛型之后,所有BaseHibernateEntityDao的子类只要定义了泛型,就无需再重载getEnttityClass(),get()函数和find()函数,销益挺明显的,所以SpringSide的Dao基类毫不犹豫就泛型了。

  不过擦拭法的大棒仍在,所以子类的泛型语法可不能乱写,最正确的用法只有:
    public class BookDao extends BaseHibernateEntityDao<Book>

转自:
http://www.blogjava.net/calvin/archive/2009/12/10/43830.html
posted @ 2012-04-14 21:47 小果子 阅读(520) | 评论 (0)编辑 收藏

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
NotificationManager 和Notification的使用总结(转)
文章分类:移动开发
这 几天一直在修改twigee的源代码,其中一个要加入的功能是常驻Notification栏,以前写的时候只能出现 在“通知”这一组中,想把它放在“正在运行”组中却不知道怎么放,查了下官方文档,找到了方法,在notification的flags字段中加一下 “FLAG_ONGOING_EVENT”就可以了。同时我也把Notification的使用方法给总结了一下。详见下文:
(1)、使用系统定义的Notification
以下是使用示例代码:
//创建一个NotificationManager的引用
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager)getSystemService(ns);
// 定义Notification的各种属性
int icon = R.drawable.icon; //通知图标
CharSequence tickerText = "Hello"; //状态栏显示的通知文本提示
long when = System.currentTimeMillis(); //通知产生的时间,会在通知信息里显示
//用上面的属性初始化 Nofification
Notification notification = new Notification(icon,tickerText,when);
/*
* 添加声音
* notification.defaults |=Notification.DEFAULT_SOUND;
* 或者使用以下几种方式
* notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3");
* notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");
* 如果想要让声音持续重复直到用户对通知做出反应,则可以在notification的flags字段增加"FLAG_INSISTENT"
* 如果notification的defaults字段包括了"DEFAULT_SOUND"属性,则这个属性将覆盖sound字段中定义的声音
*/
/*
* 添加振动
* notification.defaults |= Notification.DEFAULT_VIBRATE;
* 或者可以定义自己的振动模式:
* long[] vibrate = {0,100,200,300}; //0毫秒后开始振动,振动100毫秒后停止,再过200毫秒后再次振动300毫秒
* notification.vibrate = vibrate;
* long数组可以定义成想要的任何长度
* 如果notification的defaults字段包括了"DEFAULT_VIBRATE",则这个属性将覆盖vibrate字段中定义的振动
*/
/*
* 添加LED灯提醒
* notification.defaults |= Notification.DEFAULT_LIGHTS;
* 或者可以自己的LED提醒模式:
* notification.ledARGB = 0xff00ff00;
* notification.ledOnMS = 300; //亮的时间
* notification.ledOffMS = 1000; //灭的时间
* notification.flags |= Notification.FLAG_SHOW_LIGHTS;
*/
/*
* 更多的特征属性
* notification.flags |= FLAG_AUTO_CANCEL; //在通知栏上点击此通知后自动清除此通知
* notification.flags |= FLAG_INSISTENT; //重复发出声音,直到用户响应此通知
* notification.flags |= FLAG_ONGOING_EVENT; //将此通知放到通知栏的"Ongoing"即"正在运行"组中
* notification.flags |= FLAG_NO_CLEAR; //表明在点击了通知栏中的"清除通知"后,此通知不清除,
* //经常与FLAG_ONGOING_EVENT一起使用
* notification.number = 1; //number字段表示此通知代表的当前事件数量,它将覆盖在状态栏图标的顶部
* //如果要使用此字段,必须从1开始
* notification.iconLevel = ; //
*/
//设置通知的事件消息
Context context = getApplicationContext(); //上下文
CharSequence contentTitle = "My Notification"; //通知栏标题
CharSequence contentText = "Hello World!"; //通知栏内容
Intent notificationIntent = new Intent(this,Main.class); //点击该通知后要跳转的Activity
PendingIntent contentIntent = PendingIntent.getActivity(this,0,notificationIntent,0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
//把Notification传递给 NotificationManager
mNotificationManager.notify(0,notification);
如果想要更新一个通知,只需要在设置好notification之后,再次调用 setLatestEventInfo(),然后重新发送一次通知即可,即再次调用notify()。
(2)、使用自定义的 Notification
要 创建一个自定义的Notification,可以使用RemoteViews。要定义自己的扩展消息,首先 要初始化一个RemoteViews对象,然后将它传递给Notification的contentView字段,再把PendingIntent传递给 contentIntent字段。以下示例代码是完整步骤:
//1、创建一个自 定义的消息布局 view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<ImageView android:id="@+id/image" android:layout_width="wrap_content"
android:layout_height="fill_parent" android:layout_marginRight="10dp" />
<TextView android:id="@+id/text" android:layout_width="wrap_content"
android:layout_height="fill_parent" android:textColor="#000" />
</LinearLayout>
//2、 在程序代码中使用RemoteViews的方法来定义image和text。然后把RemoteViews对象传到contentView字段
RemoteViews contentView = new RemoteViews(getPackageName(),R.layout.view);
contentView.setImageViewResource(R.id.image,R.drawable.icon);
contentView.setTextViewText(R.id.text,”Hello,this message is in a custom expanded view”);
notification.contentView = contentView;
//3、 为Notification的contentIntent字段定义一个Intent(注意,使用自定义View不需要 setLatestEventInfo()方法)
Intent notificationIntent = new Intent(this,Main.class);
PendingIntent contentIntent = PendingIntent.getActivity(this,0,notificationIntent,0);
notification.contentIntent = contentIntent;
//4、发送通知
mNotificationManager.notify(2,notification);
// 以下是全部示例代码
//创建一个 NotificationManager的引用
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager)getSystemService(ns);
// 定义Notification的各种属性
int icon = R.drawable.icon; //通知图标
CharSequence tickerText = "Hello"; //状态栏显示的通知文本提示
long when = System.currentTimeMillis(); //通知产生的时间,会在通知信息里显示
//用上面的属性初始化 Nofification
Notification notification = new Notification(icon,tickerText,when);
RemoteViews contentView = new RemoteViews(getPackageName(),R.layout.view);
contentView.setImageViewResource(R.id.image, R.drawable.iconempty);
contentView.setTextViewText(R.id.text, "Hello,this is JC");
notification.contentView = contentView;
Intent notificationIntent = new Intent(this,Main.class);
PendingIntent contentIntent = PendingIntent.getActivity(this,0,notificationIntent,0);
notification.contentIntent = contentIntent;
//把Notification传递给NotificationManager
mNotificationManager.notify(0,notification);
posted @ 2012-04-05 02:34 小果子 阅读(584) | 评论 (0)编辑 收藏
     摘要: 关于android软键盘enter键的替换与事件监听 软件盘的界面替换只有一个属性android:imeOptions,这个属性的可以取的值有 normal,actionUnspecified,actionNone,actionGo,actionSearch,actionSend,actionNext,actionDone, 例如当值为actionNext时enter键外观变成一个向下箭头,而值为...  阅读全文
posted @ 2012-03-28 20:54 小果子 阅读(9850) | 评论 (1)编辑 收藏

今天学习android自定义组件:docs/guide/topics/ui/custom-components.html

其中有两个对布局界面影响很的方法,onDraw(),和onMeasure().

onDraw()比较好理解.onMeasure()就比较难理解一些,也更复杂些 ,引用文档中的说法就是:

onMeasure() is a little more involved.
其实还有另一个方面的原因就是我对这个单词measure不是很知道,然后果了下词典,就放了下心,确实是测量的意思.

实现onMeasure()方法基本需要完成下面三个方面的事情(最终结果是你自己写相应代码得出测量值并调用view的一个方法进行设置,告诉给你的view安排位置大小的父容器你要多大的空间.):

1.传递进来的参数,widthMeasureSpec,和heightMeasureSpec是你对你应该得出来的测量值的限制.

 

The overidden onMeasure() method is called with width and height measure specifications(widthMeasureSpec and heightMeasureSpec parameters,both are integer codes representing dimensions) which should be treated as requirements for the restrictions on the width and height measurements you should produce.

2. 你在onMeasure计算出来设置的width和height将被用来渲染组件.应当尽量在传递进来的width和height 声明之间.

虽然你也可以选择你设置的尺寸超过传递进来的声明.但是这样的话,父容器可以选择,如clipping,scrolling,或者抛出异常,或者(也许是用新的声明参数)再次调用onMeasure()

Your component's onMeasure() method should calculate a measurement width and height which will be required to render the component.it should try to stay within the specified passed in.although it can choose to exceed them(in this case,the parent can choose what to do,including clipping,scrolling,throwing an excption,or asking the onMeasure to try again,perhaps with different measurement specifications).

3.一但width和height计算好了,就应该调用View.setMeasuredDimension(int width,int height)方法,否则将导致抛出异常.
Once the width and height are calculated,the setMeasureDimension(int width,int height) method must be called with the calculated measurements.Failure to do this will result in an exceptiion being thrown
   

在Android提提供的一个自定义View示例中(在API demos 中的 view/LabelView)可以看到一个重写onMeasure()方法的

实例,也比较好理解.

01/**
02 * @see android.view.View#measure(int, int)
03 */
04@Override
05protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
06    setMeasuredDimension(measureWidth(widthMeasureSpec),
07            measureHeight(heightMeasureSpec));
08}
09 
10/**
11 * Determines the width of this view
12 * @param measureSpec A measureSpec packed into an int
13 * @return The width of the view, honoring constraints from measureSpec
14 */
15private int measureWidth(int measureSpec) {
16    int result = 0;
17    int specMode = MeasureSpec.getMode(measureSpec);
18    int specSize = MeasureSpec.getSize(measureSpec);
19 
20    if (specMode == MeasureSpec.EXACTLY) {
21        // We were told how big to be
22        result = specSize;
23    } else {
24        // Measure the text
25        result = (int) mTextPaint.measureText(mText) + getPaddingLeft()
26                + getPaddingRight();
27        if (specMode == MeasureSpec.AT_MOST) {
28            // Respect AT_MOST value if that was what is called for by measureSpec
29            result = Math.min(result, specSize);
30        }
31    }
32 
33    return result;
34}

 

直接看measureWidth()

首先看到的是参数,分别代表宽度和高度的MeasureSpec

android2.2文档中对于MeasureSpec中的说明是:

一个MeasureSpec封装了从父容器传递给子容器的布局需求.

每一个MeasureSpec代表了一个宽度,或者高度的说明.

一个MeasureSpec是一个大小跟模式的组合值.一共有三种模式.

A MeasureSpec encapsulates the layout requirements passed from parent to child Each MeasureSpec represents a requirement for either the width or the height.A MeasureSpec is compsized of a size and a mode.There are three possible modes:

 (1)UPSPECIFIED :父容器对于子容器没有任何限制,子容器想要多大就多大.
UNSPECIFIED The parent has not imposed any constraint on the child.It can be whatever size it wants

 (2) EXACTLY

 父容器已经为子容器设置了尺寸,子容器应当服从这些边界,不论子容器想要多大的空间.

EXACTLY The parent has determined and exact size for the child.The child is going to be given those bounds regardless of how big it wants to be.

(3) AT_MOST

 子容器可以是声明大小内的任意大小.

AT_MOST The child can be as large as it wants up to the specified size

MeasureSpec是View类下的静态公开类,MeasureSpec中的值作为一个整型是为了减少对象的分配开支.此类用于

将size和mode打包或者解包为一个整型.

MeasureSpecs are implemented as ints to reduce object allocation.This class is provided to pack and unpack the size,mode tuple into the int

我比较好奇的是怎么样将两个值打包到一个int中,又如何解包.

MeasureSpec类代码如下 :(注释已经被我删除了,因为在上面说明了.)

01public static class MeasureSpec {
02    private static final int MODE_SHIFT = 30;
03    private static final int MODE_MASK  = 0x3 << MODE_SHIFT;
04 
05    public static final int UNSPECIFIED = 0 << MODE_SHIFT;
06    public static final int EXACTLY     = 1 << MODE_SHIFT;
07    public static final int AT_MOST     = 2 << MODE_SHIFT;
08 
09    public static int makeMeasureSpec(int size, int mode) {
10        return size + mode;
11    }
12    public static int getMode(int measureSpec) {
13        return (measureSpec & MODE_MASK);
14    }
15    public static int getSize(int measureSpec) {
16        return (measureSpec & ~MODE_MASK);
17    }  }

我无聊的将他们的十进制值打印出来了:

mode_shift=30,mode_mask=-1073741824,UNSPECIFIED=0,EXACTLY=1073741824,AT_MOST=-2147483648

然后觉得也应该将他们的二进制值打印出来,如下:

mode_shift=11110, // 30

mode_mask=11000000000000000000000000000000,

UNSPECIFIED=0, 

EXACTLY=1000000000000000000000000000000, 

AT_MOST=10000000000000000000000000000000

 

1MODE_MASK  = 0x3 << MODE_SHIFT //也就是说MODE_MASK是由11左移30位得到的.因为Java用补码表示数值.最后得到的值最高位是1所以就是负数了
1 
对于上面的数值我们应该这样想,不要把0x3看成3而要看成二进制的11,

而把MODE_SHIFF就看成30.那为什么是二进制 的11呢?

呢,因为只有三各模式,如果有四种模式就是111了因为111三个位才可以有四种组合对吧.

我们这样来看,

UNSPECIFIED=00000000000000000000000000000000, 

      EXACTLY=01000000000000000000000000000000, 

    AT_MOST=10000000000000000000000000000000

也就是说,0,1,2

对应   00,01,10

当跟11想与时  00 &11 还是得到 00,11&01 -> 01,10&

我觉得到了这个份上相信,看我博客的也都理解了.

 return (measureSpec & ~MODE_MASK);应该是 return (measureSpec & (~MODE_MASK));

posted @ 2012-03-27 23:59 小果子 阅读(19770) | 评论 (4)编辑 收藏
仅列出标题
共58页: First 14 15 16 17 18 19 20 21 22 Last