Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/189.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在canvas android的onDraw()方法中快速绘制位图_Android_Dictionary_Canvas_Bitmap - Fatal编程技术网

如何在canvas android的onDraw()方法中快速绘制位图

如何在canvas android的onDraw()方法中快速绘制位图,android,dictionary,canvas,bitmap,Android,Dictionary,Canvas,Bitmap,我正在尝试在android中的单点击方法上画一个标记。当我绘制标记时,它将绘制,但绘制需要更多时间,即30-40毫秒,有时需要2-3秒。这是我的类代码,我在其中有draw方法 public class MyItemizedOverlay extends ItemizedOverlay<OverlayItem> { private ArrayList<OverlayItem> overlayItemList = new ArrayList<OverlayIt

我正在尝试在android中的单点击方法上画一个标记。当我绘制标记时,它将绘制,但绘制需要更多时间,即30-40毫秒,有时需要2-3秒。这是我的类代码,我在其中有draw方法

public class MyItemizedOverlay extends ItemizedOverlay<OverlayItem> {

    private ArrayList<OverlayItem> overlayItemList = new ArrayList<OverlayItem>();

    public MyItemizedOverlay(Drawable pDefaultMarker,
            ResourceProxy pResourceProxy) {
        super(pDefaultMarker, pResourceProxy);
    }

    @Override
    public void draw(Canvas canvas, MapView mapView, boolean arg2) {
        super.draw(canvas, mapView, arg2);

        // ---translate the GeoPoint to screen pixels---
        Point screenPts = new Point();
        mapView.getProjection().toPixels(p, screenPts);

        // ---add the marker---
        Bitmap bmp = BitmapFactory.decodeResource(getResources(),
                R.drawable.pin_annotation_darkblue);
        Bitmap bmp1 = BitmapFactory.decodeResource(getResources(),
                R.drawable.pin_annotation_green);
        Bitmap bmp2 = BitmapFactory.decodeResource(getResources(),
                R.drawable.pin_annotation_bue);
        Bitmap bmp3 = BitmapFactory.decodeResource(getResources(),
                R.drawable.pin_annotation_light);
        Bitmap bmp4 = BitmapFactory.decodeResource(getResources(),
                R.drawable.pin_annotation_light);
        Bitmap bmp5 = BitmapFactory.decodeResource(getResources(),
                R.drawable.pin_annotation_light);
        Bitmap bmp6 = BitmapFactory.decodeResource(getResources(),
                R.drawable.pin_annotation_light);
        if (count == 1) {
            int caller = getIntent().getIntExtra("button", 0);
            switch (caller) {
            case R.id.btMap:
                canvas.drawBitmap(bmp, screenPts.x, screenPts.y - 50, null);
                bmp.recycle();
                break;
            case R.id.imageButton1:
                canvas.drawBitmap(bmp1, screenPts.x, screenPts.y - 50, null);
                bmp1.recycle();
                break;
            case R.id.imageButton2:
                canvas.drawBitmap(bmp2, screenPts.x, screenPts.y - 50, null);
                bmp2.recycle();
                break;
            case R.id.imageButton3:
                canvas.drawBitmap(bmp3, screenPts.x, screenPts.y - 50, null);
                bmp3.recycle();
                break;
            case R.id.imageButton4:
                canvas.drawBitmap(bmp4, screenPts.x, screenPts.y - 50, null);
                bmp4.recycle();
                break;
            case R.id.imageButton5:
                canvas.drawBitmap(bmp5, screenPts.x, screenPts.y - 50, null);
                bmp5.recycle();
                break;
            case R.id.imageButton6:
                canvas.drawBitmap(bmp6, screenPts.x, screenPts.y - 50, null);
                bmp6.recycle();
                break;
            }
        }
        // Bitmap bmp = BitmapFactory.decodeResource(getResources(),
        // R.drawable.pin_annotation_green);
        // if (count == 1) {
        // canvas.drawBitmap(bmp, screenPts.x, screenPts.y - 50, null);
        // }
}
公共类MyItemizeOverlay扩展了ItemizeOverlay{
private ArrayList overlayItemList=新建ArrayList();
公共MyItemizedOverlay(可抽出式pDefaultMarker,
资源代理(资源代理){
super(pDefaultMarker,尿前造影);
}
@凌驾
公共空白绘制(画布、地图视图、地图视图、布尔值arg2){
super.draw(画布、地图视图、arg2);
//---将地质点转换为屏幕像素---
点屏幕PTS=新点();
getProjection().toPixels(p,screenPts);
//---添加标记---
位图bmp=BitmapFactory.decodeResource(getResources(),
R.可绘制的插针(黑色);
位图bmp1=BitmapFactory.decodeResource(getResources(),
R.可拉拔。插脚(绿色);
位图bmp2=BitmapFactory.decodeResource(getResources(),
R.可拉拔的销(注);
位图bmp3=BitmapFactory.decodeResource(getResources(),
R.可拉拔的插脚(灯);
位图bmp4=BitmapFactory.decodeResource(getResources(),
R.可拉拔的插脚(灯);
位图bmp5=BitmapFactory.decodeResource(getResources(),
R.可拉拔的插脚(灯);
位图bmp6=BitmapFactory.decodeResource(getResources(),
R.可拉拔的插脚(灯);
如果(计数=1){
int caller=getIntent().getIntExtra(“按钮”,0);
交换机(调用者){
案例R.id.btMap:
drawBitmap(bmp,screenPts.x,screenPts.y-50,空);
bmp.recycle();
打破
案例R.id.imageButton1:
drawBitmap(bmp1,screenPts.x,screenPts.y-50,空);
bmp1.recycle();
打破
案例R.id.imageButton2:
drawBitmap(bmp2,screenPts.x,screenPts.y-50,空);
bmp2.recycle();
打破
案例R.id.imageButton3:
drawBitmap(bmp3,screenPts.x,screenPts.y-50,空);
bmp3.recycle();
打破
案例R.id.imageButton4:
drawBitmap(bmp4,screenPts.x,screenPts.y-50,空);
bmp4.recycle();
打破
案例R.id.imageButton5:
drawBitmap(bmp5,screenPts.x,screenPts.y-50,空);
bmp5.recycle();
打破
案例R.id.imageButton6:
drawBitmap(bmp6,screenPts.x,screenPts.y-50,空);
bmp6.recycle();
打破
}
}
//位图bmp=BitmapFactory.decodeResource(getResources(),
//R.可拉拔。插脚(绿色);
//如果(计数=1){
//drawBitmap(bmp,screenPts.x,screenPts.y-50,空);
// }
}

您应该从
draw()
方法中删除所有
BitmapFactory.decodeResource()
调用。只解码位图一次并保留对它的引用。然后只需在
draw()中调用
canvas.drawBitmap()
方法。

优化代码的想法是只执行绘图所需的操作。因此,您应该从onDraw方法中删除:

  • 任何实例:它们需要很长时间,onDraw经常被调用,您不想创建这么多新对象。在onLayout期间存储ScreenPT,并始终重复使用相同的点
  • BitmapFactory.decodeResource:这需要相当长的时间。请先解码位图,存储它们,并仅在onDraw期间绘制它们
  • 当不再需要位图时回收位图,而不是每次绘制位图时
例如:

  • 在onResume期间解码位图
  • 在暂停期间回收它们
  • 解码应在异步任务内进行。异步任务结束后,升起一个标志,向onDraw指示图像已准备就绪,可以绘制
  • 在后台解码图像非常重要,因为这需要很长时间。不要在主UI线程中执行此操作。否则您的应用程序将看起来没有响应
  • 计算您的屏幕PTS内部在线布局,并始终重复使用相同的点
  • 在onDraw期间也不要调用getIntent
简单地说,最小化onDraw期间的操作,您将实现非常快的绘图,大约60 FPS


你也应该考虑删除这个(丑陋)开关,并使用一个哈希映射将计数和位图的值关联到绘图。一个数组甚至会更快,也许更适合这里。

< p>你应该初始化构造函数中的所有位图。解码位图需要很长时间。你可以使用<代码> HashMap < /Cord>(key,value)。然后在onDraw中,获取匹配的位图并直接绘制

比如说

public class MyView extends View{

    private HashMap<String, Bitmap> mStore = new HashMap<String, Bitmap>();
    public MyView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub

        init();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub

        int caller = getIntent().getIntExtra("button", 0);
        Bitmap bmp = null;
        switch (caller) {
        case R.id.btMap:
            bmp = mStore.get(R.id.btMap);
            canvas.drawBitmap(bmp, screenPts.x, screenPts.y - 50, null);
            bmp.recycle();
            bmp = null;
            break;
        case R.id.imageButton1:
            bmp = mStore.get(R.id.imageButton1);
            canvas.drawBitmap(bmp, screenPts.x, screenPts.y - 50, null);
            bmp1.recycle();
            bmp1 = null;
            break;
        }

        super.onDraw(canvas);
    }

    public void init() {
        Bitmap bmp = BitmapFactory.decodeResource(getResources(),
                R.drawable.pin_annotation_darkblue);
        mStore.put(R.id.btMap, bmp);

        bmp = BitmapFactory.decodeResource(getResources(),
                R.drawable.pin_annotation_green);
        mStore.put(R.id.imageButton1, bmp);
    }
}
公共类MyView扩展了视图{
private HashMap mStore=new HashMap();
公共MyView(上下文){
超级(上下文);
//TODO自动生成的构造函数存根
init();
}
@凌驾
受保护的void onDraw(画布){
//TODO自动生成的方法存根
int caller=getIntent().getIntExtra(“按钮”,0);
位图bmp=null;
交换机(调用者){
案例R.id.btMap:
bmp=mStore.get(R.id.btMap);
drawBitmap(bmp,screenPts.x,screenPts.y-50,空);
bmp.recycle();
bmp=null;
打破
案例R.id.imageButton1:
bmp=mStore.get(R.id.imageButton1);
drawBitmap(bmp,screenPts.x,screenPts.y-50,
private ArrayList<OverlayItem> overlayItemList = new ArrayList<OverlayItem>();
private HashMap<String, Bitmap> mStore = new HashMap<String, Bitmap>();

public MyItemizedOverlay(Drawable pDefaultMarker,
        ResourceProxy pResourceProxy) {
    super(pDefaultMarker, pResourceProxy);

    Bitmap bmp = BitmapFactory.decodeResource(getResources(),
            R.drawable.pin_annotation_darkblue);
    mStore.put(R.id.btMap, bmp);
    bmp = BitmapFactory.decodeResource(getResources(),
            R.drawable.pin_annotation_green);
    mStore.put(R.id.imageButton1, bmp);
    bmp = BitmapFactory.decodeResource(getResources(),
            R.drawable.pin_annotation_bue);
    mStore.put(R.id.imageButton2, bmp);
    bmp = BitmapFactory.decodeResource(getResources(),
            R.drawable.pin_annotation_light); 
    mStore.put(R.id.imageButton3, bmp);
    bmp = BitmapFactory.decodeResource(getResources(),
            R.drawable.pin_annotation_light); // check it
    mStore.put(R.id.imageButton4, bmp);
    bmp = BitmapFactory.decodeResource(getResources(),
            R.drawable.pin_annotation_light); // check it
    mStore.put(R.id.imageButton5, bmp);
    bmp = BitmapFactory.decodeResource(getResources(),
            R.drawable.pin_annotation_light); // check it
    mStore.put(R.id.imageButton6, bmp);

}

@Override
public void draw(Canvas canvas, MapView mapView, boolean arg2) {
    super.draw(canvas, mapView, arg2);

    // ---translate the GeoPoint to screen pixels---
    Point screenPts = new Point();
    mapView.getProjection().toPixels(p, screenPts);

    // ---add the marker---
    if (count == 1) {
        int caller = getIntent().getIntExtra("button", 0);
        Bitmap bmp = null;

        switch (caller) {
        case R.id.btMap:
            bmp = mStore.get(R.id.btMap);
            canvas.drawBitmap(bmp, screenPts.x, screenPts.y - 50, null);
            bmp.recycle();
            break;
        case R.id.imageButton1:
            bmp = mStore.get(R.id.imageButton1);
            canvas.drawBitmap(bmp1, screenPts.x, screenPts.y - 50, null);
            bmp.recycle();
            break;
        case R.id.imageButton2:
            bmp = mStore.get(R.id.imageButton2);
            canvas.drawBitmap(bmp, screenPts.x, screenPts.y - 50, null);
            bmp.recycle();
            break;
        case R.id.imageButton3:
            bmp = mStore.get(R.id.imageButton3);
            canvas.drawBitmap(bmp, screenPts.x, screenPts.y - 50, null);
            bmp.recycle();
            break;
        case R.id.imageButton4:
            bmp = mStore.get(R.id.imageButton4);
            canvas.drawBitmap(bmp, screenPts.x, screenPts.y - 50, null);
            bmp.recycle();
            break;
        case R.id.imageButton5:
            bmp = mStore.get(R.id.imageButton5);
            canvas.drawBitmap(bmp, screenPts.x, screenPts.y - 50, null);
            bmp.recycle();
            break;
        case R.id.imageButton6:
            bmp = mStore.get(R.id.imageButton6);
            canvas.drawBitmap(bmp, screenPts.x, screenPts.y - 50, null);
            bmp.recycle();
            break;
        }
    }
    // Bitmap bmp = BitmapFactory.decodeResource(getResources(),
    // R.drawable.pin_annotation_green);
    // if (count == 1) {
    // canvas.drawBitmap(bmp, screenPts.x, screenPts.y - 50, null);
    // }
}