【问题标题】:capture bitmap from view android [duplicate]从视图android捕获位图[重复]
【发布时间】:2014-04-01 12:36:33
【问题描述】:

我已经发布了同样的问题,但它离我的问题很近,这就是我第二次发布它的原因

您好,我想从 RelativeLayout 捕获图像,因为我使用了下面的代码

   captureRelativeLayout.setDrawingCacheEnabled(true);
   bitmap = captureRelativeLayout.getDrawingCache(true).copy(
                Config.ARGB_8888, false);

问题是,当我开始活动并从那个视图获取图像时,它会正常工作,但如果我第二次使用它,图像不会被刷新,这意味着每次我得到之前的位图。

现在,如果我关闭我的活动并再次启动它,那么我将获得更新的图像,但第二次不会 :(有关更多信息,请查看 can't share image properly android

【问题讨论】:

  • 是的,我也试过了 :( 但还是有问题
  • 我可以建议的是:...不时用不同的名称保存位图。文件名基于当前时间(如果精确到秒可能就足够了,我认为。如果不是,也使用毫秒)。
  • @ArtooDetoo 或时间+日期 :)
  • 是的,我的意思是:yyyyMMdd_HHmmss,确切地说 - 加上您的应用名称;)
  • 我尝试过使用 100000 的随机数,但在每个新图像中它都存储了以前的图像,主要问题是,获取视图并不刷新

标签: android


【解决方案1】:

Android KTX 中有 Kotlin 扩展功能:

val config: Bitmap.Config = Bitmap.Config.ARGB_8888
val bitmap = canvasView.drawToBitmap(config)

【讨论】:

  • canvasView 兄弟在哪里?
【解决方案2】:

由于 DrawingCache 在 kotlin 中已被弃用:

fun View.createBitmap(): Bitmap {
    val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
    Canvas(bitmap).apply {
        background?.draw(this) ?: this.drawColor(Color.WHITE)
        draw(this)
    }
    return bitmap
}

【讨论】:

    【解决方案3】:

    view.setDrawingCacheEnabled(true);

    view.buildDrawingCache();

    位图位图 = view.getDrawingCache();

    “位图”是最终的位图..

    【讨论】:

      【解决方案4】:

      这里有两种将视图转换为位图的方法:

      使用绘图缓存:

      RelativeLayout view = (RelativeLayout)findViewById(R.id.relativelayout);
      view.setDrawingCacheEnabled(true);
      view.buildDrawingCache();
      Bitmap bitmap = view.getDrawingCache();
      view.setDrawingCacheEnabled(false);
      

      当视图非常大时(例如,ScrollView 中的TextView 远离可见屏幕),我在使用绘图缓存方法时遇到了一些问题。那样的话,使用下一种方法会更好。

      使用画布:

      RelativeLayout view = (RelativeLayout)findViewById(R.id.relativelayout);
      Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
      Canvas canvas = new Canvas(bitmap);
      Drawable bgDrawable = view.getBackground();
      if (bgDrawable != null) {
          bgDrawable.draw(canvas);
      } else {
          canvas.drawColor(Color.WHITE);
      }
      view.draw(canvas);
      

      【讨论】:

        【解决方案5】:

        我终于从view.getDrawingCache() only works once得到了解决方案

        我只是忘了放

             captureRelativeLayout.setDrawingCacheEnabled(false);
        

        我已经通过下面的代码完成了,

            try {
                if (bitmap != null) {
                    bitmap.recycle();
                    bitmap = null;
                }
                Intent sharingIntent = new Intent(Intent.ACTION_SEND);
                captureRelativeLayout.setDrawingCacheEnabled(true);
                File sdcard = Environment.getExternalStorageDirectory();
                File f = new File(sdcard, "temp.jpg");
                FileOutputStream out = null;
                out = new FileOutputStream(f);
                captureRelativeLayout.setDrawingCacheEnabled(true);
                bitmap = captureRelativeLayout.getDrawingCache(true).copy(
                        Config.ARGB_8888, false);
                bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
                out.close();
                captureRelativeLayout.setDrawingCacheEnabled(false);
                sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(f)); // imageUri
                sharingIntent.setType("image/jpg");
        
                sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(f)); // imageUri
                startActivity(Intent.createChooser(sharingIntent, "Share Image"));
            } catch (Exception e) {
                e.printStackTrace();
            }
        

        【讨论】:

          【解决方案6】:

          你应该在复制后销毁绘图缓存,以便下次调用getDrawingCache()时会重新构建缓存。

          代码如下所示:

          captureRelativeLayout.setDrawingCacheEnabled(true);
          bitmap = captureRelativeLayout.getDrawingCache(true).copy(Config.ARGB_8888, false);
          captureRelativeLayout.destroyDrawingCache();
          

          如果您不想启用该标志,也可以这样:

          captureRelativeLayout.buildDrawingCache(true);
          bitmap = captureRelativeLayout.getDrawingCache(true).copy(Config.ARGB_8888, false);
          captureRelativeLayout.destroyDrawingCache();
          

          参考:https://groups.google.com/d/msg/android-developers/IkRXuMtOA5w/zlP6SKlfX-0J

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-12-29
            • 1970-01-01
            • 2019-04-06
            相关资源
            最近更新 更多