android Thread 非线程更新UI界面(handler,runOnUiThread)解决图片加载慢的问题
2014-10-13 13:24
489 查看
项目背景:
将View存成bitmap,去色,作为button背景将View存成bitmap,去色,作为LinearLayout背景,选中时为蓝色背景,未选中为灰色背景
需要将一个LinearLayout(例子中为:LL_lineChart)中内容截图,存在本地,并将这张图片作为一个LinearLayout(例子中为:LL_clickfirst,LL_clickcontent)的背景。
加载过程会很慢,并且添加的processdialog会很卡,加载图片会占用UI线程,因此,需要将耗时操作添加到线程中。
private void ChartBitmap() { LL_lineChart.destroyDrawingCache(); LL_lineChart.setDrawingCacheEnabled(true); LL_lineChart.buildDrawingCache(); new Thread(new Runnable() { @Override public void run() { Bitmap catchbitmap = LL_lineChart.getDrawingCache(); Bitmap bitmap = Bitmap.createBitmap(catchbitmap, 40, 0, catchbitmap.getWidth() - 40, catchbitmap.getHeight() - 70); if (catchbitmap != null) { try { boolean sdCardExist = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED); // 判断sd卡是否存在 if (sdCardExist) // 如果SD卡存在,则获取跟目录 { sdDir = Environment.getExternalStorageDirectory();// 获取跟目录 } else { UIUtils.showToast(AppContentActivity.this, "SD卡内存不足", Toast.LENGTH_SHORT); return; } File myCaptureFile = new File(sdDir + "/appcontent" + selectTag + ".png"); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile)); BitmapUtil.getRecordImage(bitmap, Color.parseColor("#dddcdc"), Color.parseColor("#e6e6e6"), lineColors).compress(Bitmap.CompressFormat.JPEG, 80, bos);// 将LL_lineChart区域截图,并保存 bos.flush(); bos.close(); } catch (Exception e) { e.printStackTrace(); } final BitmapDrawable blueBitmap = new BitmapDrawable(BitmapUtil.getRecordImage(bitmap, Color.parseColor("#d2e5fb"), Color.parseColor("#e5f2fa"), lineColors)); AppContentActivity.this.runOnUiThread(new Runnable() { @Override public void run() { LL_clickfirst.setBackgroundDrawable(getGrayBitmap(beforeSelectTag)); LL_clickcontent.setBackgroundDrawable(blueBitmap); beforeSelectTag = selectTag; LL_clickfirst = LL_clickcontent; } }); } else { AppContentActivity.this.runOnUiThread(new Runnable() { @Override public void run() { UIUtils.showToast(AppContentActivity.this, "截图失败", Toast.LENGTH_SHORT); Log.i("CACHE_BITMAP", "DrawingCache=null"); } }); } AppContentActivity.this.runOnUiThread(new Runnable() { @Override public void run() { closeDialog(); } }); } }).start(); }
第一步:new Thread
第二步:将耗时操作放在线程中
第三步:将需要放在UI线程中的操作放在Activity.this.runOnUiThread中
Android异步处理系列文章索引参考文献:
/article/7601755.html
Android异步处理一:使用Thread+Handler实现非UI线程更新UI界面
相关文章推荐
- Android异步处理一:使用Thread+Handler实现非UI线程更新UI界面
- [转]Android异步处理一:使用Thread+Handler实现非UI线程更新UI界面
- Android异步处理:使用Thread+Handler实现非UI线程更新UI界面
- Android异步处理一:使用Thread+Handler实现非UI线程更新UI界面
- Android异步处理一:使用Thread+Handler实现非UI线程更新UI界面
- Android异步处理一:使用Thread+Handler实现非UI线程更新UI界面 .
- Android异步处理一:使用Thread+Handler实现非UI线程更新UI界面
- Android异步处理一:使用Thread+Handler实现非UI线程更新UI界面
- Android异步机制一:使用Thread+Handler实现非UI线程更新UI界面
- Android异步处理一:使用Thread+Handler实现非UI线程更新UI界面 .
- Android异步处理一:使用Thread+Handler实现非UI线程更新UI界面
- Android异步处理一:使用Thread+Handler实现非UI线程更新UI界面
- Android异步处理一:使用Thread+Handler实现非UI线程更新UI界面
- Android异步处理一:使用Thread+Handler实现非UI线程更新UI界面
- Android异步处理一:使用Thread+Handler实现非UI线程更新UI界面
- Android异步处理一:使用Thread+Handler实现非UI线程更新UI界面
- Android异步处理一:使用Thread+Handler实现非UI线程更新UI界面
- Android异步处理一:使用Thread+Handler实现非UI线程更新UI界面
- Android异步处理系列文章四篇之一使用Thread+Handler实现非UI线程更新UI界面
- Android异步处理一:使用Thread+Handler实现非UI线程更新UI界面