#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 SDK Manager 更新失败的解决方法
  3. Android三种方法设置ImageView的图片
  4. android音乐播放器常见操作
  5. Android下如何防止横竖屏切换的时候进度条从头开始
  6. 全志A64 Android7.1屏蔽使用按键进入安全模式的方法

随机推荐

  1. Android(安卓)Studio使用aidl实现进程间
  2. Android中的Rect类各参数的意义
  3. 有趣的动画视图集合:Android(安卓)View An
  4. Android(安卓)sdk tool地址及相关工具
  5. Android(安卓)ListView上下滑动与item左
  6. android传感器Gsensor和Psensor的使用举
  7. android quicksearchbox修改默认搜索引擎
  8. Android开发心得整理
  9. android系统属性读写操作SystemPropertie
  10. 【JAVA】给大家推荐一道有意思的java测试