2012-04-25 192 views
0

我有下面的代碼,該比例的對象。現在我想保存縮放後的圖像到SD卡,請幫我有關,我已經搜查了許多,但沒有發現任何保存縮放圖像SD卡android

public boolean onTouch(View v, MotionEvent event) { 
     ImageView view = (ImageView) v; 

     // Dump touch event to log 
     dumpEvent(event); 

     // Handle touch events here... 
     switch (event.getAction() & MotionEvent.ACTION_MASK) { 
     case MotionEvent.ACTION_DOWN: 
     savedMatrix.set(matrix); 
     start.set(event.getX(), event.getY()); 
     Log.d(TAG, "mode=DRAG"); 
     mode = DRAG; 
     break; 
     case MotionEvent.ACTION_POINTER_DOWN: 
     oldDist = spacing(event); 
     Log.d(TAG, "oldDist=" + oldDist); 
     if (oldDist > 10f) { 
      savedMatrix.set(matrix); 
      midPoint(mid, event); 
      mode = ZOOM; 
      Log.d(TAG, "mode=ZOOM"); 
     } 
     break; 
     case MotionEvent.ACTION_UP: 
     case MotionEvent.ACTION_POINTER_UP: 
     mode = NONE; 
     Log.d(TAG, "mode=NONE"); 
     break; 
     case MotionEvent.ACTION_MOVE: 
     if (mode == DRAG) { 
      // ... 
      matrix.set(savedMatrix); 
      matrix.postTranslate(event.getX() - start.x, 
        event.getY() - start.y); 
     } 
     else if (mode == ZOOM) { 
      float newDist = spacing(event); 
      Log.d(TAG, "newDist=" + newDist); 
      if (newDist > 10f) { 
       matrix.set(savedMatrix); 
       float scale = newDist/oldDist; 
       matrix.postScale(scale, scale, mid.x, mid.y); 
      } 
     } 
     break; 
     } 

     view.setImageMatrix(matrix); 
     return true; // indicate event was handled 
    } 

/**顯示在logcat中查看事件,調試*/

private void dumpEvent(MotionEvent event) { 
     String names[] = { "DOWN", "UP", "MOVE", "CANCEL", "OUTSIDE", 
      "POINTER_DOWN", "POINTER_UP", "7?", "8?", "9?" }; 
     StringBuilder sb = new StringBuilder(); 
     int action = event.getAction(); 
     int actionCode = action & MotionEvent.ACTION_MASK; 
     sb.append("event ACTION_").append(names[actionCode]); 
     if (actionCode == MotionEvent.ACTION_POINTER_DOWN 
      || actionCode == MotionEvent.ACTION_POINTER_UP) { 
     sb.append("(pid ").append(
       action >> MotionEvent.ACTION_POINTER_ID_SHIFT); 
     sb.append(")"); 
     } 
     sb.append("["); 
     for (int i = 0; i < event.getPointerCount(); i++) { 
     sb.append("#").append(i); 
     sb.append("(pid ").append(event.getPointerId(i)); 
     sb.append(")=").append((int) event.getX(i)); 
     sb.append(",").append((int) event.getY(i)); 
     if (i + 1 < event.getPointerCount()) 
      sb.append(";"); 
     } 
     sb.append("]"); 
     Log.d(TAG, sb.toString()); 
    } 

任何幫助?在此先感謝

回答

1

你可以做這樣的事情:

public void saveAsJpeg(View view, File file) { 
    view.setDrawingCacheEnabled(true); 
    view.measure(
     MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), 
     MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED) 
    ); 

    view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight()); 
    view.setVisibility(View.VISIBLE); 

    Bitmap _b = Bitmap.createBitmap(
     view.getMeasuredWidth(), 
     view.getMeasuredHeight(), 
     Bitmap.Config.ARGB_8888 
    ); 

    Canvas _c = new Canvas(_b); 
    view.draw(_c); 

    OutputStream _out = null; 
    try { 
     _out = new FileOutputStream(file); 
     _b.compress(Bitmap.CompressFormat.JPEG, 100, _out);    
    } finally { 
     _out.close(); 
    } 
} 

以上是重一些工作的代碼我有它通過繪製離屏產生的位圖進行修改。我有而不是以上面顯示的形式對它進行測試,並且爲了您的需要,它可能會做比實際需要更多的工作。

它發生,我認爲以下,較短的版本,可能會爲你工作,但我還沒有在所有測試這樣的:

public void saveAsJpeg(View view, File file) { 
    view.setDrawingCacheEnabled(true); 
    Bitmap _b = view.getDrawingCache(); 
    OutputStream _out = null; 
    try { 
     _out = new FileOutputStream(file); 
     _b.compress(Bitmap.CompressFormat.JPEG, 100, _out);    
    } finally { 
     _out.close(); 
    } 
} 
0

你可以做到這一點,這樣

private void saveImage(ImageView view, File file) throws IOException{ 
     //Before saving image, you need to create a scaled bitmap object 
     //which reflects the actual scaling, rotation, zoom, etc. in itself 
      Bitmap src=((BitmapDrawable)view.getDrawable()).getBitmap(); 
      Bitmap bm= Bitmap.createBitmap(src, 0,0, src.getWidth(), src.getHeight(), matrix, true); 

     //Then save this bitmap in a particular file. 
      OutputStream out = null; 
      try { 
      out = new FileOutputStream(file); 
      bm.compress(Bitmap.CompressFormat.PNG, 100, out);    
      } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
      } finally { 
      out.close(); 
      } 
    } 

這是工作代碼!