#english# give your app an edge on the competition

These classed will help you create a beautiful visual experience.

Display Bitmaps Efficiently

keep UI components responsive

avoid exceeding application memory limit, otherwise, the dreaded exception:java.lang.OutofMemoryError:bitmap size exceeds VM budget.

1.Mobile devices typically have constrained system resources.

   Android devices can have as little as 16MB memory available to a single application.

  You can find the required minimum application memory for various screen size in CDD.

2.Bitmaps take a a lot of memory.

   2592*1936pixels ARGB_8888-->4*2592*1936:19MB

3.Often require several bitmaps to be loaded at once.

  ListView, GridView and ViewPager

  Include multiple bitmaps on-screen at once with many more potentially off-screen ready to show at the flick of a finger.

Loading Large Bitmaps Efficiently

This lesson walks you through decoding large bitmaps without exceeding the memory limit by

loading a smaller subsampled version in memory.

BitmapFactory.decode*

BitmapFactory.Options

        BitmapFactory.Options options = new BitmapFactory.Options();        options.inJustDecodeBounds = true;        BitmapFactory.decodeResource(getResources(),R.drawable.page_play,options);        int width = options.outWidth;        int height = options.outHeight;        String type = options.outMimeType;        Log.d("~~~",""+width+":"+height+":"+type);

a full image or a subsampled one?

1.Estimated memory usage of loading the full image

2.Dimensions of the target ImageView or UI component

3.Screen size and density of the current device

4.Amount of memory you are willing to commit to loading this image given any other memory requirements of the application


inSampleSize

if inSapleSize ==4, the the resolution 2048*1536 and a bitmap configuration of ARGB_8888

the original memory needed is 4*2048*1536/1024/1024 = 12MB

but the subsampled resolution will be approximately 512*384, 0.75MB.

public static int calculateInSampleSize(        BitmapFactory.Options options, int reqWidth, int reqHight    ){        final int height = options.outHeight;        final int width = options.outWidth;        int insampleSize = 1;        if(height>reqHight || width>reqWidth){            final int halfHeight = height/2;            final int halfwidth = width/2;            //Calculate the largest inSampleSize vaule that is a power of 2 and keeps            //both height and width larger than the requested height and width.            while ((halfHeight/insampleSize) >= reqHight &&                    (halfwidth/insampleSize) >= reqWidth ){                insampleSize *=2;            }        }        return insampleSize;    }

Processing Bitmaps Off the UI Thread

this lesson walks you through processing bitmaps in a background thread using AyncTask and shows you how to handle concurrency issues.


AsyncTask

The AsyncTask class provides an easy way to execute some work in a background thread and publish the results back on the UI thread.

AsyncTask比Handler更轻量级一些,适用于简单的异步处理。

可以直接继承AsyncTask,在类中实现异步操作,并提供反馈当前异步执行的程度。


要使用AsyncTask,需要提供三个泛型参数+重载方法。

三个泛型参数:Params, Progress and Result.

Params: 启动任务执行的输入参数,比如HTTP请求的URL;

Progress:后台任务执行的百分比;

Result:后台执行任务最终返回结果,比如String


至少要重写两个方法:

1. doInBackground(Params...):后台执行,进行耗时操作。在这里不能直接操作UI。

                                                   在执行过程中可以调用publicProgress(Progress...)来更新任务的进度。

2.onPostExeute(Result): 这里可以使用在doInBackground得到的处理结果操作UI。此方法在主线程执行


可能需要重新的方法:

1.onProgressUpdate(Process...) 此方法在主线程执行,用于显示任务执行的进度。

2.onPreExecute()当任务执行之前开始调用此方法,可以在这里显示进度对话框。

3.onCancelled() 取消时要做的操作。


Tips:

1.Task实例必须在UI Thread中创建;

2.execute方法必须在UI Thread中调用。

3.不要手动调用onPreExecute(), onPostExecute(Result), doInBackground(Params...), onProgressUpdate(Progress...)

4.该Task只能被执行一次,否则多次挑用时会出现异常。


Handle Concurrency

Problems:

1.Common view components such as ListView and GridView recycle child views as the user scrolls.

If each child view triggers an AsyncTask, there is no guarantee that when it completes, the socciated view

has not already been recyled.

2.there is no guarantee that the order in which asynchronous tasks are started is the order that they complete.

One of solutions: the ImageView stores a reference to the most recent AyncTask which can later be checkex

when the task complete.






更多相关文章

  1. android代码格式化方法小结
  2. Android生命周期实验
  3. Android的菜单栏Menu用法详解(超详细)
  4. 每日一道Android(安卓)面试题,面试途中不败题
  5. 数据存储之——Android内、外存储分区&常用存储目录详解(Android(
  6. Android进阶知识树——Android系统的启动过程
  7. IOS和Android(安卓)OpenGL游戏引擎的集成AdMob新版教程
  8. Android(安卓)程式开发:(一)详解Activity —— 1.1生命周期
  9. 2011Android技术面试整理附有详细答案(包括百度、新浪、中科软等

随机推荐

  1. golang的编译器是什么
  2. golang 如何判断文件是否存在
  3. golang 管道线程安全吗
  4. golang的zap怎么使用
  5. golang 如何模块化
  6. golang调试工具有哪些?
  7. golang的slice如何去重
  8. golang判断map中指定key是否存在
  9. golang指针传递和值传递的区别是什么?
  10. golang的hashmap怎么扩容