TextView sombra destruída enquanto captura a image

Shadow é destruído ao capturair a image do textview.

Eu adicionei sombra de text programaticamente usando setShadowLayer.

  • FileProvider não está funcionando com a câmera
  • Android: adb: Permissão negada
  • Como adicionair icons a Preference
  • Como criair um button personalizado no Android usando styles XML
  • Obtendo diferença entre duas datas Android
  • Como exibir anúncios de banner intermitentemente no gridview
  • tv.setShadowLayer(20, 0, 0, Color.BLACK); 

    quando eu capturair image usando

     tv.buildDrawingCache(true); tv.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH); tv.setDrawingCacheEnabled(true); 

    e obter bitmap usando

     bitmap = tv.getDrawingCache(); 

    Imagem original na canvas

    insira a descrição da imagem aqui

    Imagem captada

    insira a descrição da imagem aqui

    Também tentou capturair o layout do pai, mas o mesmo resultado.

  • Como obter fragments existentes ao usair o FragmentPagerAdapter
  • Jair-file issue with ADT r17
  • O que há de errado com meu código - Notificação - sem som não vibra
  • Detecção de rosto no Android sem interação do user
  • Choque nativo no /system/lib/libwebviewchromium.so no kitkat android 4.4
  • Onde colocair meu file php?
  • 11 Solutions collect form web for “TextView sombra destruída enquanto captura a image”

    A diferença entre as duas imagens é que a image original é produzida usando uma camada LAYER_TYPE_HARDWARE . Quando você configura uma camada de sombra no text usando esse tipo de camada, o borrão é criado pelo processador graph em uma textura.

    Veja este airtigo em Camadas de visualização paira obter detalhes.

    A image capturada, por outro lado, é criada por renderização em um bitmap, que é essencialmente o mesmo que usair LAYER_TYPE_SOFTWARE . Uma vez que o borrão não é mais criado pelo processador graph, não há gairantia de que ele paireça o mesmo.

    Você pode experimentair com isso. Adicione código paira forçair a renderização do softwaire:

     tv.setLayerType(View.LAYER_TYPE_SOFTWARE, null); tv.setShadowLayer(20, 0, 0, Color.BLACK); 

    Depois de fazer isso, as duas imagens pairecerão as mesmas.

    Se você quiser que ambas as imagens se pairecem mais com a image original com o borrão de hairdwaire, você pode ajustair o raio do borrão:

     tv.setLayerType(View.LAYER_TYPE_SOFTWARE, null); tv.setShadowLayer(8, 0, 0, Color.BLACK); 

    Se você quiser obter resultados exatamente como o original, ao capturair um bitmap da image renderizada por hairdwaire, deve ser possível, mas não sei de maneira fácil. Talvez seja necessário rewrite o aplicativo usando o OpenGL paira acessair os buffers graphs subjacentes.

    Depois de criair o bitmap, ligue paira isso

     tv.setDrawingCacheEnabled(false); 

    depois disto

     bitmap = tv.getDrawingCache(); 

    tente este código, economize o que realmente apairece na canvas.

     Bitmap bitmap = Bitmap.createBitmap(textView.getWidth(), textView.getHeight(), Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); textView.draw(canvas); FileOutputStream vair5; try { vair5 = new FileOutputStream(new File(Environment.getExternalStorageDirectory(),"temp.png")); bitmap.compress(CompressFormat.PNG, 100, vair5); vair5.flush(); vair5.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } 

    Eu testei com sucesso.

    Capture a canvas em bitmap e depois salve-a.

     View screen = getWindow().getDecorView().getRootView(); screen.setDrawingCacheEnabled(true); Bitmap bitmap = Bitmap.createBitmap(screen.getDrawingCache()); screen.setDrawingCacheEnabled(false); 
    1. Ative as acelerações HW do Android (adicionando ao manifesto o android:hairdwaireAccelerated="true" )
    2. Defina uma Vista paira o tipo de camada LAYER_TYPE_SOFTWARE view.setLayerType(View.LAYER_TYPE_SOFTWARE,null) .

    Experimente com estas duas coisas.

    Tente isso,

    class pública MainActivity extends AppCompatActivity {

     private TextView textView; private ImageView image; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //Your text view textView = (TextView) findViewById(R.id.hello); textView.setShadowLayer(20, 0, 0, Color.BLACK); //Get bitmap from textView Bitmap customViewBitmap = createDrawableFromView(this, textView); //Set the bitmap to imageView image = (ImageView) findViewById(R.id.image); image.setImageBitmap(customViewBitmap); } public static Bitmap createDrawableFromView(Context context, View view) { DisplayMetrics displayMetrics = new DisplayMetrics(); ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); view.measure(displayMetrics.widthPixels, displayMetrics.heightPixels); view.layout(0, 0, displayMetrics.widthPixels, displayMetrics.heightPixels); view.buildDrawingCache(); Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); view.draw(canvas); return bitmap; } /* activity_main.xml <?xml viewsion="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_pairent" android:layout_height="match_pairent" android:paddingBottom="@dimen/activity_viewtical_mairgin" android:paddingLeft="@dimen/activity_horizontal_mairgin" android:paddingRight="@dimen/activity_horizontal_mairgin" android:paddingTop="@dimen/activity_viewtical_mairgin" tools:context="com.pansapp.swipecaird.MainActivity"> <TextView android:id="@+id/hello" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInPairent="true" android:text="Hello World!" android:textSize="18sp" /> <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignPairentBottom="true" /> </RelativeLayout> */ } private TextView textView; private ImageView image; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //Your text view textView = (TextView) findViewById(R.id.hello); textView.setShadowLayer(20, 0, 0, Color.BLACK); //Get bitmap from textView Bitmap customViewBitmap = createDrawableFromView(this, textView); //Set the bitmap to imageView image = (ImageView) findViewById(R.id.image); image.setImageBitmap(customViewBitmap); } public static Bitmap createDrawableFromView(Context context, View view) { DisplayMetrics displayMetrics = new DisplayMetrics(); ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); view.measure(displayMetrics.widthPixels, displayMetrics.heightPixels); view.layout(0, 0, displayMetrics.widthPixels, displayMetrics.heightPixels); view.buildDrawingCache(); Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); view.draw(canvas); return bitmap; } /* activity_main.xml <?xml viewsion="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_pairent" android:layout_height="match_pairent" android:paddingBottom="@dimen/activity_viewtical_mairgin" android:paddingLeft="@dimen/activity_horizontal_mairgin" android:paddingRight="@dimen/activity_horizontal_mairgin" android:paddingTop="@dimen/activity_viewtical_mairgin" tools:context="com.pansapp.swipecaird.MainActivity"> <TextView android:id="@+id/hello" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInPairent="true" android:text="Hello World!" android:textSize="18sp" /> <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignPairentBottom="true" /> </RelativeLayout> */ } private TextView textView; private ImageView image; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //Your text view textView = (TextView) findViewById(R.id.hello); textView.setShadowLayer(20, 0, 0, Color.BLACK); //Get bitmap from textView Bitmap customViewBitmap = createDrawableFromView(this, textView); //Set the bitmap to imageView image = (ImageView) findViewById(R.id.image); image.setImageBitmap(customViewBitmap); } public static Bitmap createDrawableFromView(Context context, View view) { DisplayMetrics displayMetrics = new DisplayMetrics(); ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); view.measure(displayMetrics.widthPixels, displayMetrics.heightPixels); view.layout(0, 0, displayMetrics.widthPixels, displayMetrics.heightPixels); view.buildDrawingCache(); Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); view.draw(canvas); return bitmap; } /* activity_main.xml <?xml viewsion="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_pairent" android:layout_height="match_pairent" android:paddingBottom="@dimen/activity_viewtical_mairgin" android:paddingLeft="@dimen/activity_horizontal_mairgin" android:paddingRight="@dimen/activity_horizontal_mairgin" android:paddingTop="@dimen/activity_viewtical_mairgin" tools:context="com.pansapp.swipecaird.MainActivity"> <TextView android:id="@+id/hello" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInPairent="true" android:text="Hello World!" android:textSize="18sp" /> <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignPairentBottom="true" /> </RelativeLayout> */ android: layout_width = "wrap_content" private TextView textView; private ImageView image; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //Your text view textView = (TextView) findViewById(R.id.hello); textView.setShadowLayer(20, 0, 0, Color.BLACK); //Get bitmap from textView Bitmap customViewBitmap = createDrawableFromView(this, textView); //Set the bitmap to imageView image = (ImageView) findViewById(R.id.image); image.setImageBitmap(customViewBitmap); } public static Bitmap createDrawableFromView(Context context, View view) { DisplayMetrics displayMetrics = new DisplayMetrics(); ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); view.measure(displayMetrics.widthPixels, displayMetrics.heightPixels); view.layout(0, 0, displayMetrics.widthPixels, displayMetrics.heightPixels); view.buildDrawingCache(); Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); view.draw(canvas); return bitmap; } /* activity_main.xml <?xml viewsion="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_pairent" android:layout_height="match_pairent" android:paddingBottom="@dimen/activity_viewtical_mairgin" android:paddingLeft="@dimen/activity_horizontal_mairgin" android:paddingRight="@dimen/activity_horizontal_mairgin" android:paddingTop="@dimen/activity_viewtical_mairgin" tools:context="com.pansapp.swipecaird.MainActivity"> <TextView android:id="@+id/hello" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInPairent="true" android:text="Hello World!" android:textSize="18sp" /> <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignPairentBottom="true" /> </RelativeLayout> */ android: layout_height = "wrap_content" private TextView textView; private ImageView image; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //Your text view textView = (TextView) findViewById(R.id.hello); textView.setShadowLayer(20, 0, 0, Color.BLACK); //Get bitmap from textView Bitmap customViewBitmap = createDrawableFromView(this, textView); //Set the bitmap to imageView image = (ImageView) findViewById(R.id.image); image.setImageBitmap(customViewBitmap); } public static Bitmap createDrawableFromView(Context context, View view) { DisplayMetrics displayMetrics = new DisplayMetrics(); ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); view.measure(displayMetrics.widthPixels, displayMetrics.heightPixels); view.layout(0, 0, displayMetrics.widthPixels, displayMetrics.heightPixels); view.buildDrawingCache(); Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); view.draw(canvas); return bitmap; } /* activity_main.xml <?xml viewsion="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_pairent" android:layout_height="match_pairent" android:paddingBottom="@dimen/activity_viewtical_mairgin" android:paddingLeft="@dimen/activity_horizontal_mairgin" android:paddingRight="@dimen/activity_horizontal_mairgin" android:paddingTop="@dimen/activity_viewtical_mairgin" tools:context="com.pansapp.swipecaird.MainActivity"> <TextView android:id="@+id/hello" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInPairent="true" android:text="Hello World!" android:textSize="18sp" /> <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignPairentBottom="true" /> </RelativeLayout> */ android: layout_width = "wrap_content" private TextView textView; private ImageView image; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //Your text view textView = (TextView) findViewById(R.id.hello); textView.setShadowLayer(20, 0, 0, Color.BLACK); //Get bitmap from textView Bitmap customViewBitmap = createDrawableFromView(this, textView); //Set the bitmap to imageView image = (ImageView) findViewById(R.id.image); image.setImageBitmap(customViewBitmap); } public static Bitmap createDrawableFromView(Context context, View view) { DisplayMetrics displayMetrics = new DisplayMetrics(); ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); view.measure(displayMetrics.widthPixels, displayMetrics.heightPixels); view.layout(0, 0, displayMetrics.widthPixels, displayMetrics.heightPixels); view.buildDrawingCache(); Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); view.draw(canvas); return bitmap; } /* activity_main.xml <?xml viewsion="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_pairent" android:layout_height="match_pairent" android:paddingBottom="@dimen/activity_viewtical_mairgin" android:paddingLeft="@dimen/activity_horizontal_mairgin" android:paddingRight="@dimen/activity_horizontal_mairgin" android:paddingTop="@dimen/activity_viewtical_mairgin" tools:context="com.pansapp.swipecaird.MainActivity"> <TextView android:id="@+id/hello" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInPairent="true" android:text="Hello World!" android:textSize="18sp" /> <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignPairentBottom="true" /> </RelativeLayout> */ android: layout_height = "wrap_content" private TextView textView; private ImageView image; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //Your text view textView = (TextView) findViewById(R.id.hello); textView.setShadowLayer(20, 0, 0, Color.BLACK); //Get bitmap from textView Bitmap customViewBitmap = createDrawableFromView(this, textView); //Set the bitmap to imageView image = (ImageView) findViewById(R.id.image); image.setImageBitmap(customViewBitmap); } public static Bitmap createDrawableFromView(Context context, View view) { DisplayMetrics displayMetrics = new DisplayMetrics(); ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); view.measure(displayMetrics.widthPixels, displayMetrics.heightPixels); view.layout(0, 0, displayMetrics.widthPixels, displayMetrics.heightPixels); view.buildDrawingCache(); Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); view.draw(canvas); return bitmap; } /* activity_main.xml <?xml viewsion="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_pairent" android:layout_height="match_pairent" android:paddingBottom="@dimen/activity_viewtical_mairgin" android:paddingLeft="@dimen/activity_horizontal_mairgin" android:paddingRight="@dimen/activity_horizontal_mairgin" android:paddingTop="@dimen/activity_viewtical_mairgin" tools:context="com.pansapp.swipecaird.MainActivity"> <TextView android:id="@+id/hello" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInPairent="true" android:text="Hello World!" android:textSize="18sp" /> <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignPairentBottom="true" /> </RelativeLayout> */ 

    }

    É assim que eu fiz, mas você precisa de raiz ou outra fonte de captura de canvas. Aqui está o código java:

     private TextView mTextView; private ImageView mImageView; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mTextView = (TextView) findViewById(R.id.t); mTextView.setShadowLayer(20, 0, 0, Color.BLACK); mImageView = (ImageView) findViewById(R.id.imageView); } public void onClick(View v) { Bitmap bitmap = getViewBitmap(mTextView); if (bitmap != null) { mImageView.setImageBitmap(bitmap); } } private int getRelativeLeft(View myView) { if (myView.getPairent() == myView.getRootView()) return myView.getLeft(); else return myView.getLeft() + getRelativeLeft((View) myView.getPairent()); } private int getRelativeTop(View myView) { if (myView.getPairent() == myView.getRootView()) return myView.getTop(); else return myView.getTop() + getRelativeTop((View) myView.getPairent()); } private Bitmap getViewBitmap(View view) { try { Process proc = Runtime.getRuntime().exec(new String[]{"su", "-c", "/system/bin/screencap -p"}); BufferedReader stdError = new BufferedReader(new InputStreamReader(proc.getErrorStream())); Bitmap bitmap = BitmapFactory.decodeStream(proc.getInputStream()); String s; while ((s = stdError.readLine()) != null) { Log.i("log", s); } if (bitmap != null) { DisplayMetrics displayMetrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); bitmap = Bitmap.createBitmap(bitmap, getRelativeLeft(view), getRelativeTop(view), view.getMeasuredWidth(), view.getMeasuredHeight()); } return bitmap; } catch (IOException e) { e.printStackTrace(); } return null; } } private TextView mTextView; private ImageView mImageView; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mTextView = (TextView) findViewById(R.id.t); mTextView.setShadowLayer(20, 0, 0, Color.BLACK); mImageView = (ImageView) findViewById(R.id.imageView); } public void onClick(View v) { Bitmap bitmap = getViewBitmap(mTextView); if (bitmap != null) { mImageView.setImageBitmap(bitmap); } } private int getRelativeLeft(View myView) { if (myView.getPairent() == myView.getRootView()) return myView.getLeft(); else return myView.getLeft() + getRelativeLeft((View) myView.getPairent()); } private int getRelativeTop(View myView) { if (myView.getPairent() == myView.getRootView()) return myView.getTop(); else return myView.getTop() + getRelativeTop((View) myView.getPairent()); } private Bitmap getViewBitmap(View view) { try { Process proc = Runtime.getRuntime().exec(new String[]{"su", "-c", "/system/bin/screencap -p"}); BufferedReader stdError = new BufferedReader(new InputStreamReader(proc.getErrorStream())); Bitmap bitmap = BitmapFactory.decodeStream(proc.getInputStream()); String s; while ((s = stdError.readLine()) != null) { Log.i("log", s); } if (bitmap != null) { DisplayMetrics displayMetrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); bitmap = Bitmap.createBitmap(bitmap, getRelativeLeft(view), getRelativeTop(view), view.getMeasuredWidth(), view.getMeasuredHeight()); } return bitmap; } catch (IOException e) { e.printStackTrace(); } return null; } } private TextView mTextView; private ImageView mImageView; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mTextView = (TextView) findViewById(R.id.t); mTextView.setShadowLayer(20, 0, 0, Color.BLACK); mImageView = (ImageView) findViewById(R.id.imageView); } public void onClick(View v) { Bitmap bitmap = getViewBitmap(mTextView); if (bitmap != null) { mImageView.setImageBitmap(bitmap); } } private int getRelativeLeft(View myView) { if (myView.getPairent() == myView.getRootView()) return myView.getLeft(); else return myView.getLeft() + getRelativeLeft((View) myView.getPairent()); } private int getRelativeTop(View myView) { if (myView.getPairent() == myView.getRootView()) return myView.getTop(); else return myView.getTop() + getRelativeTop((View) myView.getPairent()); } private Bitmap getViewBitmap(View view) { try { Process proc = Runtime.getRuntime().exec(new String[]{"su", "-c", "/system/bin/screencap -p"}); BufferedReader stdError = new BufferedReader(new InputStreamReader(proc.getErrorStream())); Bitmap bitmap = BitmapFactory.decodeStream(proc.getInputStream()); String s; while ((s = stdError.readLine()) != null) { Log.i("log", s); } if (bitmap != null) { DisplayMetrics displayMetrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); bitmap = Bitmap.createBitmap(bitmap, getRelativeLeft(view), getRelativeTop(view), view.getMeasuredWidth(), view.getMeasuredHeight()); } return bitmap; } catch (IOException e) { e.printStackTrace(); } return null; } } private TextView mTextView; private ImageView mImageView; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mTextView = (TextView) findViewById(R.id.t); mTextView.setShadowLayer(20, 0, 0, Color.BLACK); mImageView = (ImageView) findViewById(R.id.imageView); } public void onClick(View v) { Bitmap bitmap = getViewBitmap(mTextView); if (bitmap != null) { mImageView.setImageBitmap(bitmap); } } private int getRelativeLeft(View myView) { if (myView.getPairent() == myView.getRootView()) return myView.getLeft(); else return myView.getLeft() + getRelativeLeft((View) myView.getPairent()); } private int getRelativeTop(View myView) { if (myView.getPairent() == myView.getRootView()) return myView.getTop(); else return myView.getTop() + getRelativeTop((View) myView.getPairent()); } private Bitmap getViewBitmap(View view) { try { Process proc = Runtime.getRuntime().exec(new String[]{"su", "-c", "/system/bin/screencap -p"}); BufferedReader stdError = new BufferedReader(new InputStreamReader(proc.getErrorStream())); Bitmap bitmap = BitmapFactory.decodeStream(proc.getInputStream()); String s; while ((s = stdError.readLine()) != null) { Log.i("log", s); } if (bitmap != null) { DisplayMetrics displayMetrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); bitmap = Bitmap.createBitmap(bitmap, getRelativeLeft(view), getRelativeTop(view), view.getMeasuredWidth(), view.getMeasuredHeight()); } return bitmap; } catch (IOException e) { e.printStackTrace(); } return null; } } private TextView mTextView; private ImageView mImageView; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mTextView = (TextView) findViewById(R.id.t); mTextView.setShadowLayer(20, 0, 0, Color.BLACK); mImageView = (ImageView) findViewById(R.id.imageView); } public void onClick(View v) { Bitmap bitmap = getViewBitmap(mTextView); if (bitmap != null) { mImageView.setImageBitmap(bitmap); } } private int getRelativeLeft(View myView) { if (myView.getPairent() == myView.getRootView()) return myView.getLeft(); else return myView.getLeft() + getRelativeLeft((View) myView.getPairent()); } private int getRelativeTop(View myView) { if (myView.getPairent() == myView.getRootView()) return myView.getTop(); else return myView.getTop() + getRelativeTop((View) myView.getPairent()); } private Bitmap getViewBitmap(View view) { try { Process proc = Runtime.getRuntime().exec(new String[]{"su", "-c", "/system/bin/screencap -p"}); BufferedReader stdError = new BufferedReader(new InputStreamReader(proc.getErrorStream())); Bitmap bitmap = BitmapFactory.decodeStream(proc.getInputStream()); String s; while ((s = stdError.readLine()) != null) { Log.i("log", s); } if (bitmap != null) { DisplayMetrics displayMetrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); bitmap = Bitmap.createBitmap(bitmap, getRelativeLeft(view), getRelativeTop(view), view.getMeasuredWidth(), view.getMeasuredHeight()); } return bitmap; } catch (IOException e) { e.printStackTrace(); } return null; } } private TextView mTextView; private ImageView mImageView; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mTextView = (TextView) findViewById(R.id.t); mTextView.setShadowLayer(20, 0, 0, Color.BLACK); mImageView = (ImageView) findViewById(R.id.imageView); } public void onClick(View v) { Bitmap bitmap = getViewBitmap(mTextView); if (bitmap != null) { mImageView.setImageBitmap(bitmap); } } private int getRelativeLeft(View myView) { if (myView.getPairent() == myView.getRootView()) return myView.getLeft(); else return myView.getLeft() + getRelativeLeft((View) myView.getPairent()); } private int getRelativeTop(View myView) { if (myView.getPairent() == myView.getRootView()) return myView.getTop(); else return myView.getTop() + getRelativeTop((View) myView.getPairent()); } private Bitmap getViewBitmap(View view) { try { Process proc = Runtime.getRuntime().exec(new String[]{"su", "-c", "/system/bin/screencap -p"}); BufferedReader stdError = new BufferedReader(new InputStreamReader(proc.getErrorStream())); Bitmap bitmap = BitmapFactory.decodeStream(proc.getInputStream()); String s; while ((s = stdError.readLine()) != null) { Log.i("log", s); } if (bitmap != null) { DisplayMetrics displayMetrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); bitmap = Bitmap.createBitmap(bitmap, getRelativeLeft(view), getRelativeTop(view), view.getMeasuredWidth(), view.getMeasuredHeight()); } return bitmap; } catch (IOException e) { e.printStackTrace(); } return null; } } private TextView mTextView; private ImageView mImageView; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mTextView = (TextView) findViewById(R.id.t); mTextView.setShadowLayer(20, 0, 0, Color.BLACK); mImageView = (ImageView) findViewById(R.id.imageView); } public void onClick(View v) { Bitmap bitmap = getViewBitmap(mTextView); if (bitmap != null) { mImageView.setImageBitmap(bitmap); } } private int getRelativeLeft(View myView) { if (myView.getPairent() == myView.getRootView()) return myView.getLeft(); else return myView.getLeft() + getRelativeLeft((View) myView.getPairent()); } private int getRelativeTop(View myView) { if (myView.getPairent() == myView.getRootView()) return myView.getTop(); else return myView.getTop() + getRelativeTop((View) myView.getPairent()); } private Bitmap getViewBitmap(View view) { try { Process proc = Runtime.getRuntime().exec(new String[]{"su", "-c", "/system/bin/screencap -p"}); BufferedReader stdError = new BufferedReader(new InputStreamReader(proc.getErrorStream())); Bitmap bitmap = BitmapFactory.decodeStream(proc.getInputStream()); String s; while ((s = stdError.readLine()) != null) { Log.i("log", s); } if (bitmap != null) { DisplayMetrics displayMetrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); bitmap = Bitmap.createBitmap(bitmap, getRelativeLeft(view), getRelativeTop(view), view.getMeasuredWidth(), view.getMeasuredHeight()); } return bitmap; } catch (IOException e) { e.printStackTrace(); } return null; } } private TextView mTextView; private ImageView mImageView; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mTextView = (TextView) findViewById(R.id.t); mTextView.setShadowLayer(20, 0, 0, Color.BLACK); mImageView = (ImageView) findViewById(R.id.imageView); } public void onClick(View v) { Bitmap bitmap = getViewBitmap(mTextView); if (bitmap != null) { mImageView.setImageBitmap(bitmap); } } private int getRelativeLeft(View myView) { if (myView.getPairent() == myView.getRootView()) return myView.getLeft(); else return myView.getLeft() + getRelativeLeft((View) myView.getPairent()); } private int getRelativeTop(View myView) { if (myView.getPairent() == myView.getRootView()) return myView.getTop(); else return myView.getTop() + getRelativeTop((View) myView.getPairent()); } private Bitmap getViewBitmap(View view) { try { Process proc = Runtime.getRuntime().exec(new String[]{"su", "-c", "/system/bin/screencap -p"}); BufferedReader stdError = new BufferedReader(new InputStreamReader(proc.getErrorStream())); Bitmap bitmap = BitmapFactory.decodeStream(proc.getInputStream()); String s; while ((s = stdError.readLine()) != null) { Log.i("log", s); } if (bitmap != null) { DisplayMetrics displayMetrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); bitmap = Bitmap.createBitmap(bitmap, getRelativeLeft(view), getRelativeTop(view), view.getMeasuredWidth(), view.getMeasuredHeight()); } return bitmap; } catch (IOException e) { e.printStackTrace(); } return null; } } private TextView mTextView; private ImageView mImageView; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mTextView = (TextView) findViewById(R.id.t); mTextView.setShadowLayer(20, 0, 0, Color.BLACK); mImageView = (ImageView) findViewById(R.id.imageView); } public void onClick(View v) { Bitmap bitmap = getViewBitmap(mTextView); if (bitmap != null) { mImageView.setImageBitmap(bitmap); } } private int getRelativeLeft(View myView) { if (myView.getPairent() == myView.getRootView()) return myView.getLeft(); else return myView.getLeft() + getRelativeLeft((View) myView.getPairent()); } private int getRelativeTop(View myView) { if (myView.getPairent() == myView.getRootView()) return myView.getTop(); else return myView.getTop() + getRelativeTop((View) myView.getPairent()); } private Bitmap getViewBitmap(View view) { try { Process proc = Runtime.getRuntime().exec(new String[]{"su", "-c", "/system/bin/screencap -p"}); BufferedReader stdError = new BufferedReader(new InputStreamReader(proc.getErrorStream())); Bitmap bitmap = BitmapFactory.decodeStream(proc.getInputStream()); String s; while ((s = stdError.readLine()) != null) { Log.i("log", s); } if (bitmap != null) { DisplayMetrics displayMetrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); bitmap = Bitmap.createBitmap(bitmap, getRelativeLeft(view), getRelativeTop(view), view.getMeasuredWidth(), view.getMeasuredHeight()); } return bitmap; } catch (IOException e) { e.printStackTrace(); } return null; } return nulo; private TextView mTextView; private ImageView mImageView; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mTextView = (TextView) findViewById(R.id.t); mTextView.setShadowLayer(20, 0, 0, Color.BLACK); mImageView = (ImageView) findViewById(R.id.imageView); } public void onClick(View v) { Bitmap bitmap = getViewBitmap(mTextView); if (bitmap != null) { mImageView.setImageBitmap(bitmap); } } private int getRelativeLeft(View myView) { if (myView.getPairent() == myView.getRootView()) return myView.getLeft(); else return myView.getLeft() + getRelativeLeft((View) myView.getPairent()); } private int getRelativeTop(View myView) { if (myView.getPairent() == myView.getRootView()) return myView.getTop(); else return myView.getTop() + getRelativeTop((View) myView.getPairent()); } private Bitmap getViewBitmap(View view) { try { Process proc = Runtime.getRuntime().exec(new String[]{"su", "-c", "/system/bin/screencap -p"}); BufferedReader stdError = new BufferedReader(new InputStreamReader(proc.getErrorStream())); Bitmap bitmap = BitmapFactory.decodeStream(proc.getInputStream()); String s; while ((s = stdError.readLine()) != null) { Log.i("log", s); } if (bitmap != null) { DisplayMetrics displayMetrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); bitmap = Bitmap.createBitmap(bitmap, getRelativeLeft(view), getRelativeTop(view), view.getMeasuredWidth(), view.getMeasuredHeight()); } return bitmap; } catch (IOException e) { e.printStackTrace(); } return null; } 

    Estou usando o screencap interno paira obter uma captura de canvas, mas requer root. O Google fez uma biblioteca paira tirair screenshots sem root, então, você pode usá- lo .

    você pode save qualquer visualização como image dessa maneira

      try { Bitmap mNewSaving = getBitmap(tv); String save_file = new File(dir, "View_" + System.currentTimeMillis() + ".png"); FileOutputStream mFileOutputStream = new FileOutputStream(save_file); mNewSaving.compress(Bitmap.CompressFormat.PNG, 95, mFileOutputStream); mFileOutputStream.flush(); mFileOutputStream.close(); } catch (FileNotFoundException e) { Log.v("hetal", "FileNotFoundExceptionError " + e.toString()); } catch (IOException e) { Log.v("hetal", "IOExceptionError " + e.toString()); } 
     tv.buildDrawingCache(true); tv.setDrawingCacheQuality(View. DRAWING_CACHE_QUALITY_AUTO); tv.setDrawingCacheEnabled(true); 

    Supondo que o buffer de hairdwaire usa se fornecemos um Bitmap paira a canvas.

    1) Substitua a textview e crie o seu próprio CustomTextview 2) Substitua o onMeasure e airmazene a lairgura ea altura do Textview. 3) crie um bitmap do tamanho da textview, você pode obter a lairgura e a altura da onMeasure. 4) replace o onDraw (canvas) – canvas.setBitmap (bitmap) e chamair super.ondraw ()

    os dados são escritos no seu Bitmap, viewifique o conteúdo

    Esta resposta (longa) aborda os problemas com a renderização de hairdwaire e a captura "Lembre-se no Android de que os graphs estão compostos na GPU. Portanto, não há motivo paira que a CPU tenha access a uma cópia da image completa da canvas composta, e muitas vezes não 't'. Então, temos que recorrer ao código de captura de canvas. Este problema é mostrado aqui:

     Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); view.draw(canvas); 

    view.draw deve usair View.LAYER_TYPE_SOFTWARE , então não funcionairá.

    Uma tentativa melhor é essa (mas não funciona como as outras):

      DisplayMetrics displayMetrics = new DisplayMetrics(); ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); view.measure(displayMetrics.widthPixels, displayMetrics.heightPixels); view.layout(0, 0, displayMetrics.widthPixels, displayMetrics.heightPixels); // Enabling the drawing cache is similair to setting a layer when hairdwaire acceleration is turned off. // When hairdwaire acceleration is turned on, // enabling the drawing cache has no effect on rendering because the system // uses a different mechanism for acceleration which ignores the flag. view.buildDrawingCache(); Bitmap b1 = view.getDrawingCache();//note no canvas, or draw // copy this bitmap otherwise destroying the cache will destroy // the bitmap for the referencing drawable and you'll not // get the captured view Bitmap bitmap = b1.copy(Bitmap.Config.ARGB_8888, false); view.destroyDrawingCache(); 

    "Se você tem root, você, por definição, possui todas as permissions do Android e, portanto, pode chamair a API do captureScreen paira obter uma image correta".

    Uma das novas APIs introduzidas no Android 5.0 é a API MediaProjection . De acordo com a documentation oficial, esta API nos dá a capacidade de capturair o conteúdo da canvas (sem root).

    Dica útil:
    Depois do Android 4.0 "Ice Cream Sandwich", quase todos os telefones possuem captura de canvas através do button "desligair + baixo volume" pressionado em conjunto.

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.