Android 在AsyncTask onPostExecute中使用canvas.drawBitmap
我使用异步任务在视图上绘制位图,但它什么也不绘制Android 在AsyncTask onPostExecute中使用canvas.drawBitmap,android,performance,android-asynctask,android-canvas,Android,Performance,Android Asynctask,Android Canvas,我使用异步任务在视图上绘制位图,但它什么也不绘制 这是异步任务代码 class BitmapWorker extends AsyncTask<String, Void, Void> { private Canvas canvas; private Rect rcText; private Paint paint; private Options options; private Options opt; public BitmapWo
这是异步任务代码
class BitmapWorker extends AsyncTask<String, Void, Void>
{
private Canvas canvas;
private Rect rcText;
private Paint paint;
private Options options;
private Options opt;
public BitmapWorker(Canvas canvas,Rect rcText,Paint paint)
{
this.canvas = canvas;
this.rcText = rcText;//the bitmap must draw on it's rect
this.paint = paint;
}
@Override
protected Void doInBackground(String... params)
{
options = new Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(m_AttachSource, options);
opt = new Options();
opt.inPurgeable = true;
opt.inSampleSize = calculateInSampleSize(options, INSAMPLESIZE_THIMBPIC_WIDTH, INSAMPLESIZE_THIMBPIC_HEIGHT);
LoadThumbPic(m_AttachSource, opt);
return null;
}
@Override
protected void onPostExecute(Void result)
{
super.onPostExecute(result);
Boolean hasBitmap = false;
while(!hasBitmap)
{
if(m_PictureMessageTumbPic.get() != null)
{
canvas.drawBitmap(m_PictureMessageTumbPic.get(), null, rcText, paint);
hasBitmap = true;
}
else
{
Options opt = new Options();
opt.inPurgeable = true;
opt.inSampleSize = calculateInSampleSize(options, INSAMPLESIZE_THIMBPIC_WIDTH, INSAMPLESIZE_THIMBPIC_HEIGHT);
LoadThumbPic(m_AttachSource, opt);
canvas.drawBitmap(m_PictureMessageTumbPic.get(), null, rcText, paint);
hasBitmap = true;
}
}
}
}
类BitmapWorker扩展异步任务
{
私人帆布;
私有矩形文本;
私人油漆;
私人期权;
私人选择权;
公共BitmapWorker(画布、矩形文本、油漆)
{
this.canvas=画布;
this.rcText=rcText;//位图必须在其矩形上绘制
这个。油漆=油漆;
}
@凌驾
受保护的Void doInBackground(字符串…参数)
{
选项=新选项();
options.inJustDecodeBounds=true;
解码文件(m_AttachSource,选项);
opt=新选项();
opt.inpurgable=true;
opt.inSampleSize=计算样本大小(选项,inSampleSize\u THIMBPIC\u WIDTH,inSampleSize\u THIMBPIC\u HEIGHT);
LoadThumbPic(m_AttachSource,opt);
返回null;
}
@凌驾
受保护的void onPostExecute(void结果)
{
super.onPostExecute(结果);
布尔hasbtmap=false;
而(!hasBitmap)
{
如果(m_PictureMessageTumbPic.get()!=null)
{
drawBitmap(m_PictureMessageTumbPic.get(),null,rcText,paint);
hasBitmap=true;
}
其他的
{
选项opt=新选项();
opt.inpurgable=true;
opt.inSampleSize=计算样本大小(选项,inSampleSize\u THIMBPIC\u WIDTH,inSampleSize\u THIMBPIC\u HEIGHT);
LoadThumbPic(m_AttachSource,opt);
drawBitmap(m_PictureMessageTumbPic.get(),null,rcText,paint);
hasBitmap=true;
}
}
}
}
tnx 4 adv.您说过要绘制视图,但从代码中可以看出,在绘制操作之后,您似乎没有该视图。因此,您必须修改
AsyncTask
以接受视图
,并在更新画布后调用其invalidate()
方法
记住,现代操作系统缓存图形元素以提高性能,因此您必须使用它提供的机制来通知它更新已就绪
试试这个(没有运行代码,可能会有愚蠢的错误):
类BitmapWorker扩展异步任务
{
私人帆布;
私有矩形文本;
私人油漆;
私人期权;
私人选择权;
私人视野;
公共BitmapWorker(画布、矩形文本、绘画、视图)
{
this.canvas=画布;
this.rcText=rcText;//位图必须在其矩形上绘制
这个。油漆=油漆;
this.view=视图;
}
@凌驾
受保护的Void doInBackground(字符串…参数)
{
选项=新选项();
options.inJustDecodeBounds=true;
解码文件(m_AttachSource,选项);
opt=新选项();
opt.inpurgable=true;
opt.inSampleSize=计算样本大小(选项,inSampleSize\u THIMBPIC\u WIDTH,inSampleSize\u THIMBPIC\u HEIGHT);
LoadThumbPic(m_AttachSource,opt);
返回null;
}
@凌驾
受保护的void onPostExecute(void结果)
{
super.onPostExecute(结果);
布尔hasbtmap=false;
而(!hasBitmap)
{
如果(m_PictureMessageTumbPic.get()!=null)
{
drawBitmap(m_PictureMessageTumbPic.get(),null,rcText,paint);
hasBitmap=true;
}
其他的
{
选项opt=新选项();
opt.inpurgable=true;
opt.inSampleSize=计算样本大小(选项,inSampleSize\u THIMBPIC\u WIDTH,inSampleSize\u THIMBPIC\u HEIGHT);
LoadThumbPic(m_AttachSource,opt);
drawBitmap(m_PictureMessageTumbPic.get(),null,rcText,paint);
hasBitmap=true;
}
}
如果(位图){
view.invalidate();
}
}
}
在您的onPostExecute
方法上执行此操作:
- 获取曲面保持器
- 从支架上初始化画布
- 让支架解锁画布并张贴画布
那么,当视图无效时,如何防止再次运行asyncTask呢?对不起。。。我完全理解你的意思,但我会更新我的答案,在几分钟内包含一个示例代码,说明要尝试什么。如果我使用invalidate,则视图已清除,并且必须再次使用canvas.DrawBtmap在其上绘制位图,是否正确?您应该使用该视图的onDraw()方法绘制位图。我假设它是一个自定义视图。是的,它是一个自定义视图,最后我更改了方法,在asyntask中加载位图并在主线程上绘制它,tnx提供帮助
class BitmapWorker extends AsyncTask<String, Void, Void>
{
private Canvas canvas;
private Rect rcText;
private Paint paint;
private Options options;
private Options opt;
private View view;
public BitmapWorker(Canvas canvas,Rect rcText,Paint paint, View view)
{
this.canvas = canvas;
this.rcText = rcText;//the bitmap must draw on it's rect
this.paint = paint;
this.view = view;
}
@Override
protected Void doInBackground(String... params)
{
options = new Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(m_AttachSource, options);
opt = new Options();
opt.inPurgeable = true;
opt.inSampleSize = calculateInSampleSize(options, INSAMPLESIZE_THIMBPIC_WIDTH, INSAMPLESIZE_THIMBPIC_HEIGHT);
LoadThumbPic(m_AttachSource, opt);
return null;
}
@Override
protected void onPostExecute(Void result)
{
super.onPostExecute(result);
Boolean hasBitmap = false;
while(!hasBitmap)
{
if(m_PictureMessageTumbPic.get() != null)
{
canvas.drawBitmap(m_PictureMessageTumbPic.get(), null, rcText, paint);
hasBitmap = true;
}
else
{
Options opt = new Options();
opt.inPurgeable = true;
opt.inSampleSize = calculateInSampleSize(options, INSAMPLESIZE_THIMBPIC_WIDTH, INSAMPLESIZE_THIMBPIC_HEIGHT);
LoadThumbPic(m_AttachSource, opt);
canvas.drawBitmap(m_PictureMessageTumbPic.get(), null, rcText, paint);
hasBitmap = true;
}
}
if(hasBitmap) {
view.invalidate();
}
}
}
protected void onPostExecute(Bitmap result) {
SurfaceHolder holder = getSurfaceHolder();
Canvas canvas = null;
try {
canvas = holder.lockCanvas();
if (canvas != null) {
canvas.drawBitmap(result, 50, 50, paint);
}
} finally {
if (canvas != null)
holder.unlockCanvasAndPost(canvas);
}
}