我正在开发一个我需要裁剪图像的应用程序。但是这个图像裁剪应该只在我的活动上进行,所以我不能使用android的默认图像裁剪概念,其中图像将进入用户可以裁剪的另一个视图。
我需要的是创建自定义图像裁剪器矩形。
我从很长时间尝试这个但没有运气。如果有任何身体可以提供帮助,我们将感
谢谢
答案 0 :(得分:8)
我的主要活动..
public class MainActivity extends Activity {
private DragRectView view;
private ImageView original;
// private ImageView croppedImage;
/**
*
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
view = (DragRectView) findViewById(R.id.dragRect);
original = (ImageView) findViewById(R.id.image);
// croppedImage = (ImageView) findViewById(R.id.crop_image);
if (null != view) {
view.setOnUpCallback(new DragRectView.OnUpCallback() {
@Override
public void onRectFinished(final Rect rect) {
Toast.makeText(
getApplicationContext(),
"Rect is (" + rect.left + ", " + rect.top + ", "
+ rect.right + ", " + rect.bottom + ")",
Toast.LENGTH_LONG).show();
Bitmap bitmap1 = Bitmap.createScaledBitmap(
((BitmapDrawable) original.getDrawable())
.getBitmap(), original.getWidth(), original
.getHeight(), false);
System.out.println(rect.height() + " "
+ bitmap1.getHeight() + " " + rect.width()
+ " " + bitmap1.getWidth());
if (rect.height() <= bitmap1.getHeight()
&& rect.width() <= bitmap1.getWidth()) {
Bitmap bitmap = Bitmap.createBitmap(bitmap1,
view.getLeft(), view.getTop(), view.getWidth(),
view.getHeight());
System.out
.println("MainActivity.onCreate(...).new OnUpCallback() {...}.onRectFinished() if true");
Intent intent = new Intent(MainActivity.this,
CropImageActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("cropimage", bitmap);
startActivity(intent);
System.out.println("MainActivity.onCreate() ");
}
}
});
}
}
}
... cropImageAcivity
public class CropImageActivity extends Activity {
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.crop_image);
imageView = (ImageView) findViewById(R.id.crop_image);
Intent intent = getIntent();
if (intent != null) {
Bundle extras = intent.getExtras();
if (extras != null) {
System.out.println("CropImageActivity.onCreate()");
Bitmap bitmap = extras.getParcelable("cropimage");
imageView.setImageBitmap(bitmap);
}
}
}
}
... DragRectangle
public class DragRectView extends View {
private Paint mRectPaint;
private int mStartX = 0;
private int mStartY = 0;
private int mEndX = 0;
private int mEndY = 0;
private boolean mDrawRect = false;
private TextPaint mTextPaint = null;
private OnUpCallback mCallback = null;
public interface OnUpCallback {
void onRectFinished(Rect rect);
}
public DragRectView(final Context context) {
super(context);
init();
}
public DragRectView(final Context context, final AttributeSet attrs) {
super(context, attrs);
init();
}
public DragRectView(final Context context, final AttributeSet attrs,
final int defStyle) {
super(context, attrs, defStyle);
init();
}
/**
* Sets callback for up
*
* @param callback
* {@link OnUpCallback}
*/
public void setOnUpCallback(OnUpCallback callback) {
mCallback = callback;
}
/**
* Inits internal data
*/
private void init() {
mRectPaint = new Paint();
mRectPaint.setColor(Color.GREEN);
mRectPaint.setStyle(Paint.Style.STROKE);
mRectPaint.setStrokeWidth(5);
mTextPaint = new TextPaint();
mTextPaint.setColor(Color.MAGENTA);
mTextPaint.setTextSize(20);
}
@Override
public boolean onTouchEvent(final MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mDrawRect = false;
mStartX = (int) event.getX();
mStartY = (int) event.getY();
invalidate();
break;
case MotionEvent.ACTION_MOVE:
final int x = (int) event.getX();
final int y = (int) event.getY();
if (!mDrawRect || Math.abs(x - mEndX) > 5
|| Math.abs(y - mEndY) > 5) {
mEndX = x;
mEndY = y;
invalidate();
}
mDrawRect = true;
break;
case MotionEvent.ACTION_UP:
if (mCallback != null) {
mCallback.onRectFinished(new Rect(Math.min(mStartX, mEndX),
Math.min(mStartY, mEndY), Math.max(mEndX, mStartX),
Math.max(mEndY, mStartX)));
}
invalidate();
break;
default:
break;
}
return true;
}
@Override
protected void onDraw(final Canvas canvas) {
super.onDraw(canvas);
if (mDrawRect) {
canvas.drawRect(Math.min(mStartX, mEndX), Math.min(mStartY, mEndY),
Math.max(mEndX, mStartX), Math.max(mEndY, mStartY),
mRectPaint);
canvas.drawText(
" (" + Math.abs(mStartX - mEndX) + ", "
+ Math.abs(mStartY - mEndY) + ")",
Math.max(mEndX, mStartX), Math.max(mEndY, mStartY),
mTextPaint);
}
}
}
主要布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/background_dark"
android:orientation="vertical"
android:weightSum="2" >
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:src="@drawable/image" />
<com.afbb.imagecrop.DragRectView
android:id="@+id/dragRect"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!--
<ImageView
android:id="@+id/crop_image"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:scaleType="fitXY"
android:src="@drawable/image" />
-->
</RelativeLayout>
cropactivity layout
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/crop_image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/ic_launcher" />
</FrameLayout>
它可能对你有帮助......
答案 1 :(得分:0)
你可以试试这个灵活和自定义的裁剪......
Intent intent = new Intent("com.android.camera.action.CROP");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
intent.setType("image/*");
intent.putExtra("aspectX", 0);
intent.putExtra("aspectY", 0);
intent.putExtra("outputY", 600);
intent.putExtra("outputX", 600);
intent.putExtra("scale", "true);