#android training# Graphics & Animation:Display Bitmaps Efficiently
来源:互联网 发布:mysql免费吗 编辑:程序博客网 时间:2024/05/19 11:48
#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.
- #android training# Graphics & Animation:Display Bitmaps Efficiently
- Android Training--Display Bitmaps Efficiently
- 高效率地显示位图(总课程绪论)Display Bitmaps Efficiently——翻译自developer.android.com Training
- Android-Displaying Bitmaps Efficiently
- Training - Multimedia & Graphics & Animation
- Training图像篇之一:Loading Large Bitmaps Efficiently
- Android Bitmap之Loading Large Bitmaps Efficiently
- android有效显示图片Displaying Bitmaps Efficiently
- [Developer Android] Loading Large Bitmaps Efficiently
- Displaying Bitmaps Efficiently -Loading Large Bitmaps Efficiently
- Displaying Bitmaps Efficiently
- Displaying Bitmaps Efficiently
- Displaying Bitmaps Efficiently
- Displaying Bitmaps Efficiently
- Displaying Bitmaps Efficiently
- Displaying Bitmaps Efficiently
- Loading Large Bitmaps Efficiently
- Loading Large Bitmaps Efficiently
- navicat+for+mysql+10.0.11简体中文破解版(有破解码)
- hdu 2049
- 寻找卓越的(Android)软件工程师( 译)
- redis 简介
- 将数据以表格的形式保存到pdf中
- #android training# Graphics & Animation:Display Bitmaps Efficiently
- 进一步了解安卓开发
- leetcode解题之416#Partition Equal Subset SumJava版. 对半分(0-1背包,动态规划)
- jquery是JavaScript的一个库,也就是说就算使用jQuery,也要遵守JavaScript的语法,为什么jQuery还有自己的语法?
- MySQL 5.7.17 绿色版 如何安装
- python2.7使用TimeDelta中total_seconds()方法的问题
- 踩过的坑
- MT7628如何控制GPIO
- JAVA代码自动生成.xls和.xlsx类型的excel文件的详细过程和代码