8

我正在尝试使用画布在 android 中实现徒手裁剪。我使用drawPath它并将其存储在列表中并在画布路径中绘制它,确定,

像这样

在此处输入图像描述

但现在我想用这段代码在侧面区域的那个路径中制作所有像素,但我不知道该怎么做..

public  Bitmap getBitmapWithTransparentBG(Bitmap srcBitmap) 
    {
        Bitmap result = srcBitmap.copy(Bitmap.Config.ARGB_8888, true);
        int nWidth = result.getWidth();
        int nHeight = result.getHeight();
        for (int y = 0; y < nHeight; ++y)
        {
          for (int x = 0; x < nWidth; ++x) 
          {
             for (int i = 0; i < points.size() ; i++) 
             {

             }
              result.setPixel(x, y, Color.TRANSPARENT);
          }
        }
        return result;
    }

点是路径坐标列表听到是绘制路径的代码

package com.org;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

public class SomeView extends View implements OnTouchListener {

    private Paint paint;
    List<Point> points;
    int DIST = 2;
    boolean flgPathDraw = true;

    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.waterlilies);

    public SomeView(Context c  ) {
        super(c);
        setFocusable(true);
        setFocusableInTouchMode(true);

        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(2);
        paint.setColor(Color.WHITE);

        this.setOnTouchListener(this);
        points = new ArrayList<Point>();
    }
    public SomeView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setFocusable(true);
        setFocusableInTouchMode(true);

        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(2);
        paint.setColor(Color.WHITE);

        this.setOnTouchListener(this);
        points = new ArrayList<Point>();

    }

    public void onDraw(Canvas canvas) 
    {
        canvas.drawBitmap(bitmap, 0, 0, null);

        Path path = new Path();
        boolean first = true;

        for (int i = 0; i < points.size(); i += 2) 
        {
            Point point = points.get(i);
            if (first) {
                first = false;
                path.moveTo(point.x, point.y);
            } else if (i < points.size() - 1) {
                Point next = points.get(i + 1);
                path.quadTo(point.x, point.y, next.x, next.y);
            } else {
                path.lineTo(point.x, point.y);
            }
        }
        canvas.drawPath(path, paint);
    }

    public boolean onTouch(View view, MotionEvent event) {
        // if(event.getAction() != MotionEvent.ACTION_DOWN)
        // return super.onTouchEvent(event);
        Point point = new Point();
        point.x = (int) event.getX();
        point.y = (int) event.getY();

        if (flgPathDraw) {
            points.add(point);
        }

        invalidate();
        Log.e("Hi  ==>", "Size: " + points.size());

        return true;
    }
    public void fillinPartofPath()
    {
        Point point = new Point();
        point.x = points.get(0).x;
        point.y = points.get(0).y;

        points.add(point);
        invalidate();
    }
    public void resetView()
    {
        points.clear();
        paint.setColor(Color.WHITE);
        paint.setStyle(Style.STROKE);
        flgPathDraw=true;
        invalidate();
    }
}

class Point {
    public float dy;
    public float dx;
    float x, y;

    @Override
    public String toString() {
        return x + ", " + y;
    }
}
4

3 回答 3

3

嗨,我认为下面的链接是您的确切解决方案,您尝试什么?

Android:免费裁剪图像

不要忘记在这里投票和反馈。

于 2014-01-03T11:37:11.553 回答
1

让我们看一个更复杂的例子:

例子

红点是您要测试的点。您必须找到穿过红点 y 坐标的边缘。在此示例中,4 条边穿过 y 坐标(蓝色点)。

现在测试在要检查的点的左侧和右侧有多少交叉点。如果两侧有奇数个交叉点,则该点位于形状内部。

更新:您可以在此处找到该算法的更详细说明

于 2012-12-16T11:33:46.560 回答
0

您可以使用Canvas.clipPath仅绘制裁剪区域。但请注意,此方法不适用于硬件加速,因此您必须将其关闭并使用软件渲染。

于 2012-12-16T11:43:04.613 回答