我使用矩阵类来缩放我的图像。 但我不知道如何裁剪缩放的图像。
我想我应该在ZOOM模式下修复偏移。
请帮帮我
public class PP2cropActivity extends Activity implements OnTouchListener {
private static final String TAG = "Touch";
Button cropbtn;
float offx, offy;
float addX2, addY2;
float sx, sy, ex, ey;
float scale;
Matrix matrix = new Matrix();
Matrix savedMatrix = new Matrix();
static final int NONE = 0;
static final int DRAG = 1;
static final int ZOOM = 2;
int mode = NONE;
PointF start = new PointF();
PointF mid = new PointF();
PointF end = new PointF();
float oldDist = 1f;
Bitmap bPhoto;
String mImgpath;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent = getIntent();
mImgpath = intent.getStringExtra("mImgpath");
bPhoto = BitmapFactory.decodeFile(mImgpath);
bPhoto = Bitmap.createScaledBitmap(bPhoto, 400, 600, true);
cropbtn = (Button)findViewById(R.id.confirmbtn);
cropbtn.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
PP2mainActivity.mCropedImage = Bitmap.createBitmap(bPhoto, (int)addX2, (int)addY2, 100, 100);
Intent intent = new Intent(PP2cropActivity.this, PP2mainActivity.class);
startActivity(intent);
}
});
ImageView view = (ImageView) findViewById(R.id.matrixview);
view.setImageBitmap(bPhoto);
view.setOnTouchListener(this);
}
public boolean onTouch(View v, MotionEvent event) {
ImageView view = (ImageView) v;
// Dump touch event to log
// 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");
sx = event.getX();
sy = event.getY();
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:
if(mode ==DRAG) {
addX2 = -offx +10;
addY2 = -offy +10;
mode = NONE;
Log.d("point", "addX2 : " + addX2 + "addY2 : " + addY2);
} else if(mode ==ZOOM)
{
addX2 = (-offx+10)*scale;
addY2 = (-offy+10)*scale;
}
break;
case MotionEvent.ACTION_POINTER_UP:
mode = NONE;
Log.d(TAG, "mode=NONE");
break;
case MotionEvent.ACTION_MOVE:
if (mode == DRAG) {
// ...
matrix.set(savedMatrix);
ex = event.getX();
ey = event.getY();
offx +=(int)(ex-sx);
offy +=(int)(ey-sy);
sx = ex;
sy = ey;
Log.d("off", "offx : " + offx+ " , offy : " + offy);
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);
scale = newDist / oldDist;
matrix.postScale(scale, scale, mid.x, mid.y);
}
offx += (int)(ex-sx) /scale;
offy += (int)(ey-sy) /scale;
sx = ex;
sy = ey;
}
break;
}
view.setImageMatrix(matrix);
return true; // indicate event was handled
}
/** Determine the space between the first two fingers */
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);
}
/** Calculate the mid point of the first two fingers */
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);
}
}
答案 0 :(得分:0)
裁剪图片:
Bitmap originalBitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.test_image);
Bitmap croppedBitmap = Bitmap.createBitmap(originalBitmap, x, y, width, height);