2014-10-16 2 views
0

터치 이벤트에 응답하는보기에서 드로잉하기 위해 안드로이드의 API 샘플을 사용했습니다. 기본적으로 내가하는 일은 화면을 터치 할 때마다 내가 가지고있는 것 위에 원을 그립니다 (내용이 보존 된 것처럼 보이게하는 투명한 배경을 가진 원을 그립니다)비트 맵을 파일로 저장하여 검은 색 이미지로 표시

이제 문제는 이미지로 저장하려고 할 때. 나는 JPG와 PNG를 모두 시험해 보았고 나는 검은 색 그림을 얻는다. 어떻게 든 그것은 모든 투명한 재료를 검은 색으로 보이게 만듭니다.

이 비트 맵을 이미지 (선호하는 JPG)로 저장할 수있는 방법이 있습니까? 이미지 자체를 보면 투명도가 전혀 없습니다.

@Override 
     protected void onSizeChanged(int w, int h, int oldw, int oldh) { 
      int curW = mBitmap != null ? mBitmap.getWidth() : 0; 
      int curH = mBitmap != null ? mBitmap.getHeight() : 0; 
      if (curW >= w && curH >= h) { 
       return; 
      } 

      if (curW < w) curW = w; 
      if (curH < h) curH = h; 

      Bitmap newBitmap = Bitmap.createBitmap(curW, curH, Bitmap.Config.ARGB_8888); 
      Canvas newCanvas = new Canvas(); 
      newCanvas.setBitmap(newBitmap); 
      if (mBitmap != null) { 
       newCanvas.drawBitmap(mBitmap, 0, 0, null); 
      } 
      mBitmap = newBitmap; 
      mCanvas = newCanvas; 

     } 

     @Override 
     protected void onDraw(Canvas canvas) { 
      if (mBitmap != null) { 
       canvas.drawBitmap(mBitmap, 0, 0, null); 
      } 
     } 

를 따를 때 나는 공공 다음과 같이 내가 저장

void paintmycolor(float x, float y, float major, float minor){ 
mPaint.setColor(myDrawColor); 
mPaint.setAlpha(Math.min((int) (pressure * 128), 255)); 
canvas.save(Canvas.MATRIX_SAVE_FLAG); 
RectF mReusableOvalRect = new RectF(); 
      canvas.rotate((float) (orientation * 180/Math.PI), x, y); 
      mReusableOvalRect.left = x - minor/2; 
      mReusableOvalRect.right = x + minor/2; 
      mReusableOvalRect.top = y - major/2; 
      mReusableOvalRect.bottom = y + major/2; 
      canvas.drawOval(mReusableOvalRect, paint); 
      canvas.restore(); 
} 

를 따를 때 내가보기 페인트 뷰를 초기화

을 요청한

코드를 추가 주셔서 감사합니다 파일 saveBitmap() throws IOException {

 File path=Environment.getExternalStoragePublicDirectory(android.os.Environment.DCIM); 
     File myDirectory = new File(path,mContext.getString(R.string.app_name)); 
     if(!myDirectory.exists()){ 
      myDirectory.mkdirs(); 
     } 
     File output; 
     do { 
      int rand = new Random().nextInt(); 
      output = new File(myDirectory,rand+".jpg"); 
     }while(output.exists()); 

     BufferedOutputStream ous = null; 
     try { 
      ous=new BufferedOutputStream(new FileOutputStream(output)); 
      mBitmap.compress(CompressFormat.JPEG, 100, ous); 
      ous.flush(); 
      ous.close(); 
      return output; 

     } finally { 
      if (ous!=null) { 
       try { 
        ous.close(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
        Log.e("closing", e.getMessage()); 
       } 
      } 
     } 
    } 
+0

당신이 당신의 원 사용자보기를 그리는 ... ?? 더 나은 이해는 몇 가지 코드를 보여 – koutuk

+0

대한 그리기 원을 공유하시기 바랍니다. 몇 가지 문제점 : 1) JPG는 투명도를 지원하지 않으므로 검정색 배경이됩니다. 그리고 서클이 검은 색이면 잘 찾지 못할 것입니다. 2) SurfaceView를 사용하고 있습니까? 아니면 캔버스를 사용하고 있습니까? SurfaceView는 실제로 검정색 이미지를 캔버스에 그립니다. 캔버스를 사용하여 비트 맵을 캡처하면 SurfaceView 레이어가 아직 렌더링되지 않아서 비트 맵이 검정색이됩니다. –

+0

관련성이있는 코드를 추가했습니다. 그것은 내가 확장하는 정상적인보기입니다 – Snake

답변

1

답을 찾았습니다. 비트 맵을 만들 때 흰색으로 표시하지만 실제로는 투명 배경입니다.

저장하는 비트 맵을 작성 : 나는 (당신이 아닌 흰색 배경 레이아웃을 사용하는 것이 될 수있다) 나는 그 흰색으로 색상을 변경하지 않고 투명성을 유지하기위한 해결책을 발견

+0

그래서 어떻게 문제를 해결 했습니까? –

+0

비트 맵을 만든 후에는 모두 흰색으로 지정하여 trasnprency가되지 않도록하십시오. – Snake

0

검은받을 이유

mBitmap.setHasAlpha(true); 

이 장치에있는 동안 이미지의 투명도를 유지하게되며, 검색하는 동안 그 투명성과 이미지에 발생합니다 : 메모리에, 그리고 전 은 여기에 다음과 같은 조건을 할당 절약 그것은 메모리에서 되돌아옵니다.

결국, PNG로 저장하는 것을 잊지 마세요 :

mBitmap.compress(Bitmap.CompressFormat.PNG, 80, ous); 
관련 문제