我有以下代码,用于缩放对象。现在我想将缩放的图像保存到SD卡,请帮助我,因为我搜索了很多,但没有找到任何
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());
}
有任何帮助吗?提前致谢
答案 0 :(得分: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();
}
}
答案 1 :(得分: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();
}
}
这是工作代码!