J'ai été la recherche en ligne sur la façon de transformer ce qui est sur une toile en un bitmap. J'ai essayé plusieurs façons de le faire comme enregistrer le cache de dessin sur une image bitmap, mais le résultat final est le bitmap d'arrière-plan clignotant pendant un moment, puis devenant un écran noir. L'image de test est affichée sur l'arrière-plan, mais ne se superpose pas à l'arrière-plan de l'appel suivant de OnDraw
.Android Canvas à Bitmap
MainThreading{
...
if(notuptodate == true){
//call readyBackground to create the background bitmap
this.mainPanel.readyBackground(canvas);
//post the canvas
surfaceHolder.unlockCanvasAndPost(canvas);
//clean the Canvas
canvas = null;
//ready new Canvas
canvas = this.surfaceHolder.lockCanvas();//lock the canvas to enable editing
}//if not true
if (MainThreading.notuptodate == false){
mainPanel.onDraw(canvas);
}//if false
...
}//mainthreading
//this method is run first to create the Bitmap that on draw will use
public void readyBackGround(Canvas canvas){
if (MainThreading.notuptodate){
//method used to draw multiple Bitmaps onto the canvas
renderBackground(canvas);
//private Bitmap, this is the supposed proper size of the bitmap
backgroundBitmap=Bitmap.createBitmap(240, 320, Config.ARGB_8888);
//set the canvas to turn whats on its self onto the bitmap.
canvas.setBitmap(backgroundBitmap);
//set boolean flag to false so renderBackground won't be called
//untill it needs to be updated again
MainThreading.notUpToDate = false;
}
//this method is called when notUpToDate = true
@Override
protected void onDraw(Canvas canvas){
if (this.threado.getBackground() != null){
canvas.drawBitmap(backgroundBitmap, 0, 0, null);
}
//this if statement is never activated
if (this.threado.getBackground() == null){
Log.d("background nonexistant", "Importante!");
}
//draw test object onto background
test.DrawObject(canvas);
//when MainThreading.notUpToDate = true isn't commented out, the
//screen consistantly shows the background, but the test object isn't
//drawn
//MainThreading.notUpToDate = true;
}
Au début, cela ne semble résoudre mon problème, mais j'ai trouvé l'erreur provoquant cette . Dans la méthode OnDraw, vous verrez qu'il y a des instructions if qui faisaient référence à de l'ancien code que j'ai oublié de supprimer. Merci pour votre réponse, il est grandement apprécié. –
@AndrewRaappana vous êtes les bienvenus ..... –
copier/coller ... http://stackoverflow.com/questions/4013725/converting-a-canvas-into-bitmap-image-in-android – AlexAndro