J'essaie de dessiner un marqueur sur la méthode du robinet unique dans Android. lorsque je dessine le marqueur, il dessine, mais il faudra plus de temps pour dessiner, c'est-à-dire 30 à 40 millisecondes parfois, cela prend 2 à 3 secondes. Voici mon code pour la classe dans laquelle j'ai la méthode 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);
// }
}
Vous devez initialiser tous les bitmaps dans le constructeur. Le décodage du bitmap prend beaucoup de temps. Vous pouvez utiliser un HashMap
(clé, valeur) pour les stocker. Ensuite, dans onDraw, obtenez le bitmap correspondant et dessinez-le directement.
Par exemple
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);
}
}
Voici ce que j'ai fait en fonction de votre code. Vous devez vérifier certains ID de ressource dupliqués.
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);
// }
}
L'idée d'optimiser votre code est de n'effectuer que les opérations nécessaires au dessin. Vous devez donc supprimer de votre méthode onDraw:
Par exemple :
En bref, minimisez les opérations pendant onDraw et vous obtiendrez un dessin très rapide, environ 60 FPS.
Vous devriez également envisager de supprimer ce commutateur (moche) et d'utiliser un hashmap pour associer les valeurs de count et le bitmap à dessiner. Un tableau serait même plus rapide et peut-être plus approprié ici.
Vous devez supprimer tous les appels BitmapFactory.decodeResource()
de votre méthode draw()
. Décodez le bitmap une seule fois et conservez-y une référence. Ensuite, appelez simplement canvas.drawBitmap()
dans votre méthode draw()
.