How to draw Bitmap fast in onDraw() method in canvas android

前端 未结 3 1548
夕颜
夕颜 2020-12-30 05:47

I am trying to draw a marker on single tap method in android. when i draw the marker it will draw but it will take more time to draw i.e 30-40 milliseconds some times it tak

3条回答
  •  無奈伤痛
    2020-12-30 06:15

    You should initialize all bitmaps in Constructor. Decoding bitmap takes a long time. You can use a HashMap (key, value) to store them. Then in onDraw, get the matched bitmap and draw it directly.

    For example

    public class MyView extends View{
    
        private HashMap mStore = new HashMap();
        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);
        }
    }
    

    Here is what I've done based on your code. You have to check some duplicated resource IDs.

    private ArrayList overlayItemList = new ArrayList();
    private HashMap mStore = new HashMap();
    
    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);
        // }
    }
    

提交回复
热议问题