2010-11-24 7 views
9

Duplicar posibles:
Converting a view to Bitmap without displaying it in Android?presentación convertida en mapa de bits

Estoy tratando de convertir la visión en mapa de bits de enlace siguiente referencia

link text

Ahora el problema ¿Cómo puedo obtener el mapa de bits que se está convirtiendo de sólo vista. en el ejemplo de autor ha utilizado relativelayout.dispatchDraw (c) pero esta línea está dando me Error de compilación tiempo, es decir

El método dispatchDraw (Canvas) de el tipo ViewGroup no es visible

Aquí está mi código y he escrito el siguiente código dentro de la función onCreate

Canvas c=null; 

    //Create Layout 
    RelativeLayout relativeView ;   
    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
    RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT 
    ); 

    relativeView = new RelativeLayout(this);    
    relativeView.setLayoutParams(lp); 

    //Background of Layout 
    Bitmap viewBgrnd = BitmapFactory.decodeResource(getResources(),R.drawable.bgblack); 
    relativeView.setBackgroundDrawable(new BitmapDrawable(viewBgrnd)); 

    //associated with canvas 
    Bitmap returnedBitmap =    Bitmap.createBitmap(320,480,Bitmap.Config.ARGB_8888);  
    c = new Canvas(returnedBitmap); 
    Paint paint = new Paint(); 


    //Create Imageview that holds image    
    ImageView newImage = new ImageView(this); 
    Bitmap srcBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.bgpink); 
    newImage.setImageBitmap(srcBitmap); 

    TextView newText = new TextView(this); 

    newText.setText("This is the text that its going to appear");  

    c.drawBitmap(viewBgrnd, 0, 0, paint); 
      relativeView.layout(100, 0, 256, 256); 
    relativeView.addView(newImage); 
    relativeView.addView(newText); 


     // here i am getting compile time error so for timing i have replaced this line 
     // with relativeView.draw(c); 

    relativeView.dispatchDraw(c); 

Aquí el returnedBitmap debe contener la imagen de (ImageView y TextView), pero este mapa de bits de mapa de bits contiene sólo fundamento de la relati veView decir bgblack

+1

que tenía el mismo problema tiempos pasados ​​y no encontró ninguna solución, también. Alguien me dijo que el caché de dibujo puede ser un punto. Pero no pude hacerlo funcionar de la manera correcta. – Impression

+0

No sé cómo usar draw cache – Hunt

+0

si uso relativeView.setDrawingCacheEnabled (true); y después de eso si trataré de obtener el caché usando relativeView.getDrawingCache() obtendré un mapa de bits nulo – Hunt

Respuesta

6

Esto funciona para mí:

Bitmap viewCapture = null; 

theViewYouWantToCapture.setDrawingCacheEnabled(true); 

viewCapture = Bitmap.createBitmap(theViewYouWantToCapture.getDrawingCache()); 

theViewYouWantToCapture.setDrawingCacheEnabled(false); 

creo que la vista tiene que ser visible (es decir. getVisiblity() == View.VISIBLE). Si intenta capturarlo pero se lo oculta al usuario al mismo tiempo, puede sacarlo de la pantalla o colocar algo sobre él.

+1

está bien. pero cómo mejorar la calidad de la imagen. mientras que la conversión del mapa de bits se ve borrosa o semi transparente. – Praveen

+0

Gracias. Es un método sencillo de API: esta API se puede usar para generar manualmente una copia de mapa de bits de esta vista, configurando el indicador como verdadero y llamando a getDrawingCache(). –

25

aquí está mi solución:

public static Bitmap getBitmapFromView(View view) { 
    Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),Bitmap.Config.ARGB_8888); 
    Canvas canvas = new Canvas(returnedBitmap); 
    Drawable bgDrawable =view.getBackground(); 
    if (bgDrawable!=null) 
     bgDrawable.draw(canvas); 
    else 
     canvas.drawColor(Color.WHITE); 
    view.draw(canvas); 
    return returnedBitmap; 
} 

Enjoy :)

Cuestiones relacionadas