0

我有以下代码,可以缩放对象。现在我想将缩放后的图像保存到 sdcard,请帮助我,我搜索了很多但没有找到

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());
   }

有什么帮助吗?提前致谢

4

2 回答 2

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();
    }
}
于 2012-04-28T15:21:16.413 回答
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();
          }
    }

这是工作代码!

于 2014-01-30T05:50:10.043 回答