在我的 fragment 中我可以缩放图像,现在我想做的是保存缩放后的图像
我该怎么做
PJ 1:左上角的图片是我从资源中选取的普通图片,中间的第二张图片是我要与第一张图片关联的图片。
PJ 2:缩放后我想在本地保存这个关联
类:
public class MaskFragment extends AbstractFragment{
@ViewById
ImageView imageToEdit;
Matrix matrix = new Matrix();
Matrix savedMatrix = new Matrix();
PointF startPoint = new PointF();
PointF midPoint = new PointF();
float oldDist = 1f;
static final int NONE = 0;
static final int DRAG = 1;
static final int ZOOM = 2;
int mode = NONE;
@AfterViews
void initialise() {
/** * set on touch listner on image */
imageToEdit.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
ImageView view = (ImageView) v;
System.out.println("matrix=" + savedMatrix.toString());
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
savedMatrix.set(matrix);
startPoint.set(event.getX(), event.getY());
mode = DRAG;
break;
case MotionEvent.ACTION_POINTER_DOWN:
oldDist = spacing(event);
if (oldDist > 10f) {
savedMatrix.set(matrix);
midPoint(midPoint, event);
mode = ZOOM;
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
mode = NONE;
break;
case MotionEvent.ACTION_MOVE:
if (mode == DRAG) {
matrix.set(savedMatrix);
matrix.postTranslate(
event.getX() - startPoint.x,
event.getY() - startPoint.y
);
} else if (mode == ZOOM) {
float newDist = spacing(event);
if (newDist > 10f) {
matrix.set(savedMatrix);
float scale = newDist / oldDist;
matrix.postScale(
scale, scale, midPoint.x,
midPoint.y
);
}
}
break;
}
view.setImageMatrix(matrix);
return true;
}
@SuppressLint("FloatMath")
private float spacing(MotionEvent event) {
float x = event.getX(0) - event.getX(1);
float y = event.getY(0) - event.getY(1);
return FloatMath.sqrt(x * x + y * y);
}
private void midPoint(PointF point, MotionEvent event) {
float x = event.getX(0) + event.getX(1);
float y = event.getY(0) + event.getY(1);
point.set(x / 2, y / 2);
}
});
}
xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:gesture-image="http://schemas.polites.com/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="0.9" >
<ImageView
android:id="@+id/imageToEdit"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="true"
android:scaleType="matrix"
android:src="@drawable/fake_user" />
<ImageView
android:id="@+id/mask"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="center"
android:layout_centerInParent="true"
android:src="@drawable/camera_native" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="0.1"
android:background="@android:color/black" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Valider"
android:textColor="@android:color/white"
android:textSize="25sp" />
</RelativeLayout>
如何将第二张图片保存到本地?
感谢您的回复。
最佳答案
查看 android api 文档中的位图和文件。不只是复制和粘贴,它会帮助你理解一切是如何工作的。
//use image from cache
yourImageView.setDrawingCacheEnabled(true);
yourImageView.buildDrawingCache(true); //this might hamper performance use hardware acc if available. see: http://developer.android.com/reference/android/view/View.html#buildDrawingCache(boolean)
//create the bitmaps
Bitmap zoomedBitmap = Bitmap.createScaledBitmap(yourImageView.getDrawingCache(true), outputSize, outputSize, true);
yourImageView.setDrawingCacheEnabled(false);
现在您有了缩放后的图像,只需将其保存到文件即可。有很多关于保存到文件的信息。简而言之:
File myFile = new File(thePathOfYourFile); //have a look at the android api docs for File for proper explanation
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(myFile);
zoomedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);
fileOutputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
关于android - 保存图像缩放后查看,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26054820/