【问题标题】:Android draw area from Bitmap or Imageview with canvasAndroid 从 Bitmap 或 Imageview 用画布绘制区域
【发布时间】:2014-04-22 13:55:07
【问题描述】:

我正在尝试绘制位图,但仅使用原始位图或 Imageview 中的选定区域。要求是最终图片必须仅显示距离原始位图顶部 1/3。我附上草稿。我想我应该使用画布,但我不知道它是如何工作的。

提前致谢!!

【问题讨论】:

    标签: android canvas bitmap imageview draw


    【解决方案1】:
        Bitmap getTheReducedBitmap(Bitmap  fullLengthBitnap)
    {    
        Bitmap backDrop=Bitmap.createBitmap(fullLengthBitnap.getWidth(), fullLengthBitnap.getHeight()/3, Bitmap.Config.RGB_565);
        Canvas can = new Canvas(backDrop);
        can.drawBitmap(fullLengthBitnap, 0, 0, null);
        return backDrop;
    }
    

    【讨论】:

      【解决方案2】:

      This is the documentation for the method you should use.

      private void draw(Canvas c, Bitmap bmp){
      Rect r=new Rect(0,0,bmp.width,bmp.height/3);
      Rect drawR=new Rect(0,0,c.width,c.height/3);
      c.drawBitmap(bmp,r,drawR,null);
      }
      

      或作为一个班轮:

      c.drawBitmap(bmp,new Rect(0,0,bmp.width,bmp.height/3),new Rect(0,0,c.width,c.height/3),null);
      

      它允许您指定要在画布上的哪个位置绘制它,以及您希望片段的来源。

      @Eu.Dr.如果您想在其下方的画布上绘制任何其他内容,则答案将不起作用。

      【讨论】:

      • 感谢您的回答,但您的代码不起作用,因为您没有从原始位图复制,最终图片将在不需要的区域显示黑色,而不是删除间隙。
      • 取决于画布的大小。这是如果你想在上面画其他东西。在黑色空间中。
      • 我的意思是最终的 ImageView 显示 1/3pic 没有黑色空间,但我知道我没有更明确地定义这个目标
      【解决方案3】:
      val newBitmap=Bimtap
      .createBitmap(your_view.width,your_view.height,Bitmap.Config.ALPHA_8)
      //note: for tablet mode your_view.width,height will increase drastically so 
      //you might want 
      //to fix the size of drawing area for optimization
      //ALPHA_8 each pixel requires 1 byte of memory.
      //RGB_565 Each pixel is stored on 2 bytes
      //ARGB_8888 Each pixel is stored on 4 bytes
      //after this you can further apply the compress like
      [Refer more from google][1]
      
      
      
      
      
      val stream = ByteArrayOutputStream();
      newBitmap.compress(Bitmap.CompressFormat.PNG, 80, stream);
      val byteArray = stream.toByteArray(); // convert drawing photo to byte array
      // save it in your internal storage.
      val storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES 
      +"attendance_sheet.png");
      try{
       val fo = FileOutputStream(storageDir);
       fo.write(byteArray);
       fo.flush();
      fo.close();
      }catch(Exception ex){           
      }
      

      【讨论】:

      • 请不要只发布代码作为答案,而是说明您的代码的作用以及它如何解决问题的问题。带有解释的答案通常质量更高,更有可能吸引投票。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多