gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 18:54:27 29 4
gpt4 key购买 nike

我正在尝试在 android 中通过单击方法绘制标记。当我绘制标记时,它会绘制,但绘制需要更多时间,即 30-40 毫秒,有时需要 2-3 秒。这是我在其中具有绘制方法的类的代码。

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);
// }
}

最佳答案

您应该在构造函数中初始化所有位图。解码位图需要很长时间。您可以使用 HashMap (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);
}
}

这是我根据您的代码所做的。您必须检查一些重复的资源 ID。

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);
// }
}

关于android - 如何在 canvas android 的 onDraw() 方法中快速绘制位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19628193/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com