implemanting在Android的写意作物 [英] implemanting freehand crop in android

查看:157
本文介绍了implemanting在Android的写意作物的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试推行使用Android的画布在写意的作物。我使用 drawPath 并将其存储在列表和绘制在画布上绘制路径确定,

像这样

但现在我想在中端领域,路径与此code中所有的像素,但我不就没怎么办呢......

 公共位图getBitmapWithTransparentBG(位图srcBitmap)
    {
        位图的结果= srcBitmap.copy(Bitmap.Config.ARGB_8888,真正的);
        INT nWidth = result.getWidth();
        INT nHeight参数= result.getHeight();
        对于(INT Y = 0; Y< nHeight参数; ++ Y)
        {
          为(中间体X = 0 X  - 其中; nWidth ++ x)的
          {
             的for(int i = 0; I< points.size();我++)
             {

             }
              result.setPixel(X,Y,Color.TRANSPARENT);
          }
        }
        返回结果;
    }
 

点是路径列表坐标听到的是$ C $下平局路径

 包com.org;

进口的java.util.ArrayList;
进口的java.util.List;

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

公共类SomeView扩展视图实现OnTouchListener {

    民营涂料粉刷;
    名单<点>分;
    INT DIST = 2;
    布尔flgPathDraw =真;

    点阵位图= BitmapFactory.de codeResource(getResources(),R.drawable.waterlilies);

    公共SomeView(上下文C){
        超级(C);
        setFocusable(真正的);
        setFocusableInTouchMode(真正的);

        油漆=新的油漆(Paint.ANTI_ALIAS_FLAG);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(2);
        paint.setColor(Color.WHITE);

        this.setOnTouchListener(本);
        分=新的ArrayList<点>();
    }
    公共SomeView(上下文的背景下,ATTRS的AttributeSet){
        超(背景下,ATTRS);
        setFocusable(真正的);
        setFocusableInTouchMode(真正的);

        油漆=新的油漆(Paint.ANTI_ALIAS_FLAG);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(2);
        paint.setColor(Color.WHITE);

        this.setOnTouchListener(本);
        分=新的ArrayList<点>();

    }

    公共无效的OnDraw(帆布油画)
    {
        canvas.drawBitmap(位图,0,0,NULL);

        路径path =新路径();
        布尔第一= TRUE;

        的for(int i = 0; I< points.size();我+ = 2)
        {
            点对点= points.get(我);
            如果(第一){
                第一= FALSE;
                path.moveTo(point.x,point.y);
            }否则如果(ⅰ&其中; points.size() -  1){
                点下= points.get第(i + 1);
                path.quadTo(point.x,point.y,next.x,next.y);
            } 其他 {
                path.lineTo(point.x,point.y);
            }
        }
        canvas.drawPath(路径,油漆);
    }

    公共布尔onTouch(查看视图,MotionEvent事件){
        //如果(event.getAction()!= MotionEvent.ACTION_DOWN)
        //返回super.onTouchEvent(事件);
        点对点=新的点();
        point.x =(int)的event.getX();
        point.y =(int)的event.getY();

        如果(flgPathDraw){
            points.add(点);
        }

        无效();
        Log.e(==&GT您好;,尺寸:+ points.size());

        返回true;
    }
    公共无效fillinPartofPath()
    {
        点对点=新的点();
        point.x = points.get(0).X;
        point.y = points.get(0).Y;

        points.add(点);
        无效();
    }
    公共无效resetView()
    {
        points.clear();
        paint.setColor(Color.WHITE);
        paint.setStyle(Style.STROKE);
        flgPathDraw = TRUE;
        无效();
    }
}

类Point {
    公众持股量DY;
    公众持股量DX;
    浮动的x,y;

    @覆盖
    公共字符串的toString(){
        返回X +,+ Y;
    }
}
 

解决方案

我想下面链接,您的具体解决方案,正是ü尝试?

安卓图片免费Croping

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

i m trying to implement freehand crop in android using canvas. i use drawPath and store it in List and draw it in canvas path drawing ok,

like this

but now i want to make all pixel in that path in side area with this code but i dont no how to do it..

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

points is list of path coordinate hear is code for draw path

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

解决方案

Hi i think below link for your exact solution, what u try?

Android: Free Croping of Image

Don't forget to put your vote and feedback here.

这篇关于implemanting在Android的写意作物的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆