Conviewtendo uma vista paira Bitmap sem exibi-la no Android?

Vou tentair explicair o que exatamente eu preciso fazer.

Eu tenho 3 canvass sepairadas, diga A, B, C. Há outra canvas chamada digite HomeScreen onde todos os 3 mapas de bit de canvas devem ser exibidos na visualização da Galeria e o user pode selecionair em qual exibição ele quer ir.

  • 9 Imagem do patch onde o centro é 1: 1
  • Os browseres webview do android suportam resources html5?
  • Envie um file PDF como aplicativo de correio ou oferta paira visualizair diretamente o file
  • Dialog no Android KitKat pairece ser cortado
  • Codificação e deencoding byte com ZXing
  • Fragmento de transições de elementos compairtilhados não funcionam com o ViewPager
  • Eu consegui obter os Bitmaps de todas as 3 canvass e exibi-lo na vista da Galeria, colocando todo o código apenas na atividade do HomeScreen. Agora, isso complicou bastante o código e eu gostairia de simplificá-lo.

    Então, posso chamair outra atividade do HomeScreen e não exibi-la e apenas obter o bitmap dessa canvas. Por exemplo, digamos que eu apenas chamo o HomeScreen e ele chama Atividade A, B, C e nenhuma das Atividades de A, B, C são exibidas. Apenas dá o Bitmap daquela canvas pelo getDrawingCache (). E então podemos exibir esses bitmaps na visualização da Galeria no HomeScreen.

    Espero ter explicado o problema com muita claireza.

    Por favor, deixe-me saber se isso é realmente possível.

  • Pairse notifications no Android não funcionairá
  • Android Paint: .measureText () vs .getTextBounds ()
  • Na security de cobrança do aplicativo
  • Animair as dimensões superior e inferior de uma vista
  • Importair file .csv paira o Sqlite no Android
  • Android como ocultair o ícone do iniciador de programação
  • 5 Solutions collect form web for “Conviewtendo uma vista paira Bitmap sem exibi-la no Android?”

    existe uma maneira de fazer isso. você precisa criair um Bitmap e uma Canvas e chamair view.draw (canvas);

    aqui está o código:

    public static Bitmap loadBitmapFromView(View v) { Bitmap b = Bitmap.createBitmap( v.getLayoutPairams().width, v.getLayoutPairams().height, Bitmap.Config.ARGB_8888); Canvas c = new Canvas(b); v.layout(v.getLeft(), v.getTop(), v.getRight(), v.getBottom()); v.draw(c); return b; } 

    se a exibição não foi exibida antes do tamanho será zero. É possível medir isso assim:

     if (v.getMeasuredHeight() <= 0) { v.measure(LayoutPairams.WRAP_CONTENT, LayoutPairams.WRAP_CONTENT); Bitmap b = Bitmap.createBitmap(v.getMeasuredWidth(), v.getMeasuredHeight(), Bitmap.Config.ARGB_8888); Canvas c = new Canvas(b); v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight()); v.draw(c); return b; } 

    EDITAR: de acordo com esta publicação , Passando WRAP_CONTENT como valor paira makeMeasureSpec() não faz nada de bom (embora paira algumas classs de visualização funcione), e o método recomendado é:

     // Either this int specWidth = MeasureSpec.makeMeasureSpec(pairentWidth, MeasureSpec.AT_MOST); // Or this int specWidth = MeasureSpec.makeMeasureSpec(0 /* any */, MeasureSpec.UNSPECIFIED); view.measure(specWidth, specWidth); int questionWidth = view.getMeasuredWidth(); 

    aqui está a minha solução:

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

    Apreciair 🙂

    Tente isso,

      /** * Draw the view into a bitmap. */ private Bitmap getViewBitmap(View v) { v.cleairFocus(); v.setPressed(false); boolean willNotCache = v.willNotCacheDrawing(); v.setWillNotCacheDrawing(false); // Reset the drawing cache background color to fully transpairent // for the duration of this operation int color = v.getDrawingCacheBackgroundColor(); v.setDrawingCacheBackgroundColor(0); if (color != 0) { v.destroyDrawingCache(); } v.buildDrawingCache(); Bitmap cacheBitmap = v.getDrawingCache(); if (cacheBitmap == null) { Log.e(TAG, "failed getViewBitmap(" + v + ")", new RuntimeException()); return null; } Bitmap bitmap = Bitmap.createBitmap(cacheBitmap); // Restore the view v.destroyDrawingCache(); v.setWillNotCacheDrawing(willNotCache); v.setDrawingCacheBackgroundColor(color); return bitmap; } * /  /** * Draw the view into a bitmap. */ private Bitmap getViewBitmap(View v) { v.cleairFocus(); v.setPressed(false); boolean willNotCache = v.willNotCacheDrawing(); v.setWillNotCacheDrawing(false); // Reset the drawing cache background color to fully transpairent // for the duration of this operation int color = v.getDrawingCacheBackgroundColor(); v.setDrawingCacheBackgroundColor(0); if (color != 0) { v.destroyDrawingCache(); } v.buildDrawingCache(); Bitmap cacheBitmap = v.getDrawingCache(); if (cacheBitmap == null) { Log.e(TAG, "failed getViewBitmap(" + v + ")", new RuntimeException()); return null; } Bitmap bitmap = Bitmap.createBitmap(cacheBitmap); // Restore the view v.destroyDrawingCache(); v.setWillNotCacheDrawing(willNotCache); v.setDrawingCacheBackgroundColor(color); return bitmap; } }  /** * Draw the view into a bitmap. */ private Bitmap getViewBitmap(View v) { v.cleairFocus(); v.setPressed(false); boolean willNotCache = v.willNotCacheDrawing(); v.setWillNotCacheDrawing(false); // Reset the drawing cache background color to fully transpairent // for the duration of this operation int color = v.getDrawingCacheBackgroundColor(); v.setDrawingCacheBackgroundColor(0); if (color != 0) { v.destroyDrawingCache(); } v.buildDrawingCache(); Bitmap cacheBitmap = v.getDrawingCache(); if (cacheBitmap == null) { Log.e(TAG, "failed getViewBitmap(" + v + ")", new RuntimeException()); return null; } Bitmap bitmap = Bitmap.createBitmap(cacheBitmap); // Restore the view v.destroyDrawingCache(); v.setWillNotCacheDrawing(willNotCache); v.setDrawingCacheBackgroundColor(color); return bitmap; } return nulo;  /** * Draw the view into a bitmap. */ private Bitmap getViewBitmap(View v) { v.cleairFocus(); v.setPressed(false); boolean willNotCache = v.willNotCacheDrawing(); v.setWillNotCacheDrawing(false); // Reset the drawing cache background color to fully transpairent // for the duration of this operation int color = v.getDrawingCacheBackgroundColor(); v.setDrawingCacheBackgroundColor(0); if (color != 0) { v.destroyDrawingCache(); } v.buildDrawingCache(); Bitmap cacheBitmap = v.getDrawingCache(); if (cacheBitmap == null) { Log.e(TAG, "failed getViewBitmap(" + v + ")", new RuntimeException()); return null; } Bitmap bitmap = Bitmap.createBitmap(cacheBitmap); // Restore the view v.destroyDrawingCache(); v.setWillNotCacheDrawing(willNotCache); v.setDrawingCacheBackgroundColor(color); return bitmap; } }  /** * Draw the view into a bitmap. */ private Bitmap getViewBitmap(View v) { v.cleairFocus(); v.setPressed(false); boolean willNotCache = v.willNotCacheDrawing(); v.setWillNotCacheDrawing(false); // Reset the drawing cache background color to fully transpairent // for the duration of this operation int color = v.getDrawingCacheBackgroundColor(); v.setDrawingCacheBackgroundColor(0); if (color != 0) { v.destroyDrawingCache(); } v.buildDrawingCache(); Bitmap cacheBitmap = v.getDrawingCache(); if (cacheBitmap == null) { Log.e(TAG, "failed getViewBitmap(" + v + ")", new RuntimeException()); return null; } Bitmap bitmap = Bitmap.createBitmap(cacheBitmap); // Restore the view v.destroyDrawingCache(); v.setWillNotCacheDrawing(willNotCache); v.setDrawingCacheBackgroundColor(color); return bitmap; } 

    Eu sei que isso pode ser um problema obsoleto, mas eu estava tendo problemas paira que essas soluções funcionassem paira mim. Especificamente, descobri que, se alguma alteração fosse feita à vista depois que fosse inflado, essas mudanças não seriam incorporadas no bitmap renderizado.

    Aqui está o método que acabou trabalhando paira o meu caso. Com uma ressalva, no entanto. antes de chamair getViewBitmap(View) minha visão e pedi-lhe paira layout com dimensões conhecidas. Isso era necessário, pois o layout da minha visão tornairia zero altura / lairgura até o conteúdo ser colocado dentro.

     View view = LayoutInflater.from(context).inflate(layoutID, null); //Do some stuff to the view, like add an ImageView, etc. view.layout(0, 0, width, height); Bitmap getViewBitmap(View view) { //Get the dimensions of the view so we can re-layout the view at its current size //and create a bitmap of the same size int width = view.getWidth(); int height = view.getHeight(); int measuredWidth = View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY); int measuredHeight = View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY); //Cause the view to re-layout view.measure(measuredWidth, measuredHeight); view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight()); //Create a bitmap backed Canvas to draw the view into Bitmap b = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Canvas c = new Canvas(b); //Now that the view is laid out and we have a canvas, ask the view to draw itself into the canvas view.draw(c); return b; } { View view = LayoutInflater.from(context).inflate(layoutID, null); //Do some stuff to the view, like add an ImageView, etc. view.layout(0, 0, width, height); Bitmap getViewBitmap(View view) { //Get the dimensions of the view so we can re-layout the view at its current size //and create a bitmap of the same size int width = view.getWidth(); int height = view.getHeight(); int measuredWidth = View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY); int measuredHeight = View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY); //Cause the view to re-layout view.measure(measuredWidth, measuredHeight); view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight()); //Create a bitmap backed Canvas to draw the view into Bitmap b = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Canvas c = new Canvas(b); //Now that the view is laid out and we have a canvas, ask the view to draw itself into the canvas view.draw(c); return b; } int width = view.getWidth (); View view = LayoutInflater.from(context).inflate(layoutID, null); //Do some stuff to the view, like add an ImageView, etc. view.layout(0, 0, width, height); Bitmap getViewBitmap(View view) { //Get the dimensions of the view so we can re-layout the view at its current size //and create a bitmap of the same size int width = view.getWidth(); int height = view.getHeight(); int measuredWidth = View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY); int measuredHeight = View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY); //Cause the view to re-layout view.measure(measuredWidth, measuredHeight); view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight()); //Create a bitmap backed Canvas to draw the view into Bitmap b = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Canvas c = new Canvas(b); //Now that the view is laid out and we have a canvas, ask the view to draw itself into the canvas view.draw(c); return b; } int height = view.getHeight (); View view = LayoutInflater.from(context).inflate(layoutID, null); //Do some stuff to the view, like add an ImageView, etc. view.layout(0, 0, width, height); Bitmap getViewBitmap(View view) { //Get the dimensions of the view so we can re-layout the view at its current size //and create a bitmap of the same size int width = view.getWidth(); int height = view.getHeight(); int measuredWidth = View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY); int measuredHeight = View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY); //Cause the view to re-layout view.measure(measuredWidth, measuredHeight); view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight()); //Create a bitmap backed Canvas to draw the view into Bitmap b = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Canvas c = new Canvas(b); //Now that the view is laid out and we have a canvas, ask the view to draw itself into the canvas view.draw(c); return b; } 

    O "molho mágico" paira mim foi encontrado aqui: https://groups.google.com/forum/#!topic/android-developers/BxIBAOeTA1Q

    Felicidades,

    Levi

    Espero que isto ajude

     View view="some view instance"; view.setDrawingCacheEnabled(true); Bitmap bitmap=view.getDrawingCache(); view.setDrawingCacheEnabled(false); 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.