How to develop custom cropping rectangle in android?

前端 未结 1 1189
逝去的感伤
逝去的感伤 2021-02-11 03:46

I am developing an application where i need to crop image . But this image croping should happen on my activity only so i can not use default image cropping concept of android w

1条回答
  •  日久生厌
    2021-02-11 04:19

    my mainactivity..

    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);
        }
    }
    }
    

    mainactvity layout

    
    
    
    
    
    
    
    
    
    

    cropactivity layout

     
     
    
    
    
    
    

    it may help you...

    0 讨论(0)
提交回复
热议问题