如何在android中开发自定义裁剪矩形?
我正在开发一个需要裁剪图像的应用程序。但这种图像裁剪应该只发生在我的活动上,这样我就不能使用安卓系统的默认图像裁剪概念,图像将出现在另一个用户可以裁剪的视图上 我需要的是创建自定义图像裁剪器矩形 我尝试了很久,但没有运气。如果有人能帮忙,我会很高兴的 谢谢我的主要活动如何在android中开发自定义裁剪矩形?,android,Android,我正在开发一个需要裁剪图像的应用程序。但这种图像裁剪应该只发生在我的活动上,这样我就不能使用安卓系统的默认图像裁剪概念,图像将出现在另一个用户可以裁剪的视图上 我需要的是创建自定义图像裁剪器矩形 我尝试了很久,但没有运气。如果有人能帮忙,我会很高兴的 谢谢我的主要活动 public class MainActivity extends Activity { private DragRectView view; private ImageView original; // private Im
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() ");
}
}
});
}
}
}
拖网角
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>
<?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>
Cropacity布局
<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>
<?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>
它可能会帮助您……您可以尝试使用此功能进行灵活的定制裁剪
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);
. 可以在stackoverflow上搜索更多的例子吗?我为我的一个项目做了这个和这个裁剪工具。你好,Rajesh,谢谢分享例子。你能告诉我你是如何在裁剪器中一幅又一幅地裁剪图像的吗?(裁剪工具)findViewById(R.id.crop_工具)).setmTargetImage(BitmapFactory.decodeResource(getResources(),R.drawable.arrahman));你好,Rajesh,谢谢下面为我编写的代码imageview.setImageBitmap(cropingTool.cropedBitmap);Hello@anand谢谢-你能用布局分享你的完整项目吗?所有的布局都很完美,但它没有在cropimageactivity类中显示裁剪图像。请帮我做这个。感谢
Bitmap Bitmap=Bitmap.createBitmap(bitmap1,view.getLeft(),view.getTop(),view.getWidth(),view.getHeight())
part帮助我解决了问题-根据提供的裁剪矩形显示图像。谢谢@Aba什么是视图?view.getLeft()@MSaudi在他的main活动中code,view=(DragRectView)findViewById(R.id.dragRect)代码>