#android training# Graphics & Animation:Display Bitmaps Efficiently

本文介绍如何在Android应用中高效地加载和处理位图,避免内存溢出错误,保持UI响应性,并利用AsyncTask进行后台处理,确保流畅的用户体验。

#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.






内容概要:本文围绕可变桨叶四旋翼无人机的规范控制与点对点运动模拟展开,重点研究优化推力分配策略在翻转动作中的应用与性能比较。通过Matlab代码实现,构建了四旋翼动力学模型,并设计了多种控制算法以实现精确的姿态调整与轨迹跟踪。研究对比了不同推力分配方案在执行高机动性翻转动作时的稳定性、能耗效率与响应速度,旨在提升无人机在复杂飞行任务中的动态性能与控制精度。该仿真研究为无人机飞控系统的设计与优化提供了理论依据和技术支持。; 适合人群:具备一定自动控制理论基础和Matlab编程能力,从事无人机控制、飞行器动力学或机器人系统研究的科研人员及研究生。; 使用场景及目标:① 实现四旋翼无人机在三维空间中的精确点对点运动控制;② 对比分析不同推力分配策略在执行翻转等高难度动作时的控制效果与能耗表现,优化飞行性能;③ 为无人机自主飞行、特技飞行及复杂环境下的机动控制提供算法验证平台。; 阅读建议:此资源以Matlab仿真为核心,建议读者结合相关控制理论知识,深入理解代码实现细节,重点关注动力学建模、控制律设计与推力分配模块。在学习过程中,应动手调试参数,复现文中翻转动作的仿真结果,并尝试拓展至其他复杂飞行任务,以加深对无人机控制机理的理解。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值