Android仿桌面版QQ截图后编辑绘制椭圆工具

转载请注明:(http://blog.csdn.net/u012854870/article/details/73251151)

需求:Android截屏后对图片进行编辑,添加随手指移动缩放大小的椭圆,类似桌面版QQ截图后编辑绘制椭圆工具
先上效果图:
这里写图片描述
实现代码

public class DrawView extends View {
    private float startX;
    private float startY;
    //定义、并创建画笔
    private Paint p = new Paint();
    private ArrayList<RectF> rectFs = new ArrayList<>();
    private RectF rectF;

    public DrawView(Context context) {
        super(context);
    }

    public DrawView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public DrawView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //设置画笔的颜色
        p.setColor(Color.RED);
        //设置空心
        p.setStyle(Paint.Style.STROKE);
        //设置线宽
        p.setStrokeWidth(4);
        //绘制椭圆
        for (RectF rectF : rectFs) {
            canvas.drawOval(rectF, p);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                startX = event.getX();
                startY = event.getY();
                rectF = new RectF(startX, startY, startX, startY);
                rectFs.add(rectF);
                invalidate();
                break;
            case MotionEvent.ACTION_MOVE:
                float currentX = event.getX();
                float currentY = event.getY();
                if (currentX < startX) {
                    if (event.getY() < startY) {
                        rectF.set(currentX, currentY, startX, startY);
                    } else {
                        rectF.set(currentX, startY, startX, currentY);
                    }
                } else {
                    if (event.getY() < startY) {
                        rectF.set(startX, currentY, currentX, startY);
                    } else {
                        rectF.set(startX, startY, currentX, currentY);
                    }
                }
                invalidate();
                break;
            default:
                break;
        }
        return true;
    }
}
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页