18

我只想分享我写的这段代码。我尝试搜索自定义裁剪活动,但尽管存在自定义裁剪或徒手裁剪活动问题,但其中大多数会导致默认的“com.android.camera.action.CROP”。反正我自己做了一个,希望对大家有帮助。

public class CropView extends ImageView {

    Paint paint = new Paint();
    private int initial_size = 300;
    private static Point leftTop, rightBottom, center, previous;

    private static final int DRAG= 0;
    private static final int LEFT= 1;
    private static final int TOP= 2;
    private static final int RIGHT= 3;
    private static final int BOTTOM= 4;

    private int imageScaledWidth,imageScaledHeight;
    // Adding parent class constructors   
    public CropView(Context context) {
        super(context);
        initCropView();
    }

    public CropView(Context context, AttributeSet attrs) {
        super(context, attrs, 0);
        initCropView();
    }

    public CropView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initCropView();
    }

    @Override
    protected void onDraw(Canvas canvas)
    {
        super.onDraw(canvas);
        if(leftTop.equals(0, 0))
            resetPoints();
        canvas.drawRect(leftTop.x, leftTop.y, rightBottom.x, rightBottom.y, paint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int eventaction = event.getAction();
        switch (eventaction) { 
            case MotionEvent.ACTION_DOWN:
                previous.set((int)event.getX(), (int)event.getY());
                break; 
            case MotionEvent.ACTION_MOVE: 
                if(isActionInsideRectangle(event.getX(), event.getY())) {
                    adjustRectangle((int)event.getX(), (int)event.getY());
                    invalidate(); // redraw rectangle
                    previous.set((int)event.getX(), (int)event.getY());
                }
                break; 
            case MotionEvent.ACTION_UP: 
                previous = new Point();
                break;
        }         
        return true;
    }

    private void initCropView() {
        paint.setColor(Color.YELLOW);
        paint.setStyle(Style.STROKE);
        paint.setStrokeWidth(5);  
        leftTop = new Point();
        rightBottom = new Point();
        center = new Point();
        previous = new Point();
    }

    public void resetPoints() {
        center.set(getWidth()/2, getHeight()/2);
        leftTop.set((getWidth()-initial_size)/2,(getHeight()-initial_size)/2);
        rightBottom.set(leftTop.x+initial_size, leftTop.y+initial_size);
    }

    private static boolean isActionInsideRectangle(float x, float y) {
        int buffer = 10;
        return (x>=(leftTop.x-buffer)&&x<=(rightBottom.x+buffer)&& y>=(leftTop.y-buffer)&&y<=(rightBottom.y+buffer))?true:false;
    }

    private boolean isInImageRange(PointF point) {
        // Get image matrix values and place them in an array
        float[] f = new float[9];
        getImageMatrix().getValues(f);

        // Calculate the scaled dimensions
        imageScaledWidth = Math.round(getDrawable().getIntrinsicWidth() * f[Matrix.MSCALE_X]);
        imageScaledHeight = Math.round(getDrawable().getIntrinsicHeight() * f[Matrix.MSCALE_Y]);

        return (point.x>=(center.x-(imageScaledWidth/2))&&point.x<=(center.x+(imageScaledWidth/2))&&point.y>=(center.y-(imageScaledHeight/2))&&point.y<=(center.y+(imageScaledHeight/2)))?true:false;
    }

    private void adjustRectangle(int x, int y) {
        int movement;
        switch(getAffectedSide(x,y)) {
            case LEFT:
                movement = x-leftTop.x;
                if(isInImageRange(new PointF(leftTop.x+movement,leftTop.y+movement)))
                    leftTop.set(leftTop.x+movement,leftTop.y+movement);
                break;
            case TOP:
                movement = y-leftTop.y;
                if(isInImageRange(new PointF(leftTop.x+movement,leftTop.y+movement)))
                    leftTop.set(leftTop.x+movement,leftTop.y+movement);
                break;
            case RIGHT:
                movement = x-rightBottom.x;
                if(isInImageRange(new PointF(rightBottom.x+movement,rightBottom.y+movement)))
                    rightBottom.set(rightBottom.x+movement,rightBottom.y+movement);
                break;
            case BOTTOM:
                movement = y-rightBottom.y;
                if(isInImageRange(new PointF(rightBottom.x+movement,rightBottom.y+movement)))
                    rightBottom.set(rightBottom.x+movement,rightBottom.y+movement);
                break;      
            case DRAG:
                movement = x-previous.x;
                int movementY = y-previous.y;
                if(isInImageRange(new PointF(leftTop.x+movement,leftTop.y+movementY)) && isInImageRange(new PointF(rightBottom.x+movement,rightBottom.y+movementY))) {
                    leftTop.set(leftTop.x+movement,leftTop.y+movementY);
                    rightBottom.set(rightBottom.x+movement,rightBottom.y+movementY);
                }
                break;
        }
    }

    private static int getAffectedSide(float x, float y) {
        int buffer = 10;
        if(x>=(leftTop.x-buffer)&&x<=(leftTop.x+buffer))
            return LEFT;
        else if(y>=(leftTop.y-buffer)&&y<=(leftTop.y+buffer))
            return TOP;
        else if(x>=(rightBottom.x-buffer)&&x<=(rightBottom.x+buffer))
            return RIGHT;
        else if(y>=(rightBottom.y-buffer)&&y<=(rightBottom.y+buffer))
            return BOTTOM;
        else
            return DRAG;
    }

    public byte[] getCroppedImage() {
        BitmapDrawable drawable = (BitmapDrawable)getDrawable();
        float x = leftTop.x-center.x+(drawable.getBitmap().getWidth()/2);
        float y = leftTop.y-center.y+(drawable.getBitmap().getHeight()/2);
        Bitmap cropped = Bitmap.createBitmap(drawable.getBitmap(),(int)x,(int)y,(int)rightBottom.x-(int)leftTop.x,(int)rightBottom.y-(int)leftTop.y);
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        cropped.compress(Bitmap.CompressFormat.PNG, 100, stream);
        return stream.toByteArray();
    }
}

我所做的是,我扩展了 ImageView 并添加了裁剪功能。它很容易使用。保存类后,只需像这样在布局中使用它。

    <"your package name".CropView
        android:id="@+id/image_preview"
        android:layout_width="fill_parent"
        android:layout_height="match_parent" />

就是这样!希望能帮助到你!如果您遇到任何问题,请随时询问:)

4

3 回答 3

2

你应该试试:

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    setContentView(R.layout.crop_layout);


    myCropView = new CropView(this);

    Uri imageUri = getIntent().getExtras().getParcelable("path");

    b = (BitmapDrawable) BitmapDrawable.createFromPath(imageUri.getPath()); 

    myCropView.setImageURI(imageUri);
}

(取自对您问题的编辑。)

于 2013-05-24T11:00:25.247 回答
1

谢谢thehippo ...但我已经解决了通过布局找到视图的问题

Uri imageUri = getIntent().getExtras().getParcelable("path");
    b = (BitmapDrawable) BitmapDrawable.createFromPath(getRealPathFromURI(imageUri));
    myCropView = (CropView) findViewById(R.id.image_preview);
    myCropView.setBackground(b);

但现在我无法处理触摸事件。即使我触摸屏幕,矩形也会保持静止......

编辑:好的,我已经成功了。但是现在,矩形只在较小的区域内移动,而不是在整个图像中移动。我想这里有问题

private boolean isInImageRange(PointF point) {
    // Get image matrix values and place them in an array
    float[] f = new float[9];
    getImageMatrix().getValues(f);

    // Calculate the scaled dimensions
    imageScaledWidth = Math.round(getBackground().getIntrinsicWidth() * f[Matrix.MSCALE_X]);
    imageScaledHeight = Math.round(getBackground().getIntrinsicHeight() * f[Matrix.MSCALE_Y]);

    return (point.x>=(center.x-(imageScaledWidth/2))&&point.x<=(center.x+(imageScaledWidth/2))&&point.y>=(center.y-(imageScaledHeight/2))&&point.y<=(center.y+(imageScaledHeight/2)))?true:false;
}

我做了一些改变以使代码工作:getBackground() 而不是 getDrawable

编辑 2:好的,我明白了,我做错了。你的代码很好。为了设置图像,我使用了 view.seBackground()...而不是 view.setImageDrawable()。现在一切正常。也许我只会检查是否有可能创建一个更大的区域来触发矩形的缩放

于 2013-05-24T10:10:36.033 回答
1

我找到了一个支持这个的库:来自https://android-arsenal.com/details/1/2366的 SimpleCropView 。一般来说,我不会推荐它,它的性能与原生安卓裁剪应用程序相差甚远。

我试过使用它,我的想法是:

  • 在您的应用程序中实现非常简单,我花了大约 5 分钟的时间让裁剪和旋转功能与我现有的应用程序一起使用

  • 裁剪区域的重新调整大小非常缓慢,我不希望我的用户看到它。

更新:事实上,我在 Github 上找到了一个非常好的解决方案,如 jdamcd/android-crop:https ://github.com/jdamcd/android-crop 摘要:

  • 在您的应用中使用非常简单

  • 快,因为它使用来自本机画廊应用程序的代码

  • 可定制,如果你想花一些时间玩它。默认情况下,它会为您提供一个 Activity,您可以在其中进行裁剪。如果你想把它整合到你自己的活动中,这将需要一点时间。(对于我的项目,我本来希望整合它,并且将来会这样做,但现在一个单独的活动就足够了)。

希望这能提供一些见解!

于 2015-12-07T15:09:22.970 回答