图片剪裁之图形圆形和方形选择框

来源:互联网 发布:php增删改查源码下载 编辑:程序博客网 时间:2024/06/05 21:58

通过hongyang大神的博客http://blog.csdn.net/lmj623565791/article/details/39761281学习了自定义选择框

下面是我的学习之后的成果

public class ClipImageBorderView extends View {    //选择框距离左右的间距默认为dp    private int marginAbout=40;    //选择框距离上下的间距根据需要选择框宽度动态计算    private int marginTopAndButtom;    //选择框的颜色默认为白色    private int color= Color.parseColor("#FFFFFF");    //选择框边框的宽度    private int mBorderWidth =3;    //选择框的宽度    private int mwidth;    //圆形选择框框的时候底部背景画笔    private Paint mPaint;    //圆形选择框透明圆画笔    private Paint mBorderPaint;    private Xfermode cur_xfermode;    private Rect r;    private RectF rf;    public ClipImageBorderView(Context context) {       this(context,null);    }    public ClipImageBorderView(Context context, AttributeSet attrs) {        this(context, attrs,0);    }    public ClipImageBorderView(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);        //计算出选择框左右间距和选择框边框的px值        mwidth=dip2px(context,mwidth);        mBorderWidth=dip2px(context,mBorderWidth);        //实例化画笔        mPaint=new Paint();        //减轻除锯齿效果        mPaint.setAntiAlias(true);        //实例化圆形选择框画笔        mBorderPaint=new Paint();        //抗锯齿效果        mBorderPaint.setAntiAlias(true);        //设值画笔填充样式        mBorderPaint.setStyle(Paint.Style.STROKE);        //设置圆形选择框边框颜色        mBorderPaint.setColor(color);        //设置圆形选择框边框宽度        mBorderPaint.setStrokeWidth(mBorderWidth);        ///设置混合模式 (只在源图像和目标图像相交的地方绘制目标图像)        cur_xfermode=new PorterDuffXfermode(PorterDuff.Mode.DST_OUT);    }    @Override    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);        //创建一个全屏正方形框        if(rf==null||rf.isEmpty()){            r=new Rect(0,0,getWidth(),getHeight());            rf=new RectF(r);        }        //为圆形选择框创建新的图层//还原所有 Canvas.ALL_SAVE_FLAG        int sc=canvas.saveLayer(rf,null,Canvas.ALL_SAVE_FLAG);        //计算出正方形选择框的宽和高        mwidth=getWidth()-marginAbout*2;        //计算出正方形选择框距离上下的间距        marginTopAndButtom=(getHeight()-mwidth)/2;        //设置画笔颜色        mPaint.setColor(Color.parseColor("#aa000000"));        //设置画笔填充样式为全部填充        mPaint.setStyle(Paint.Style.FILL);        //画一个正方形        canvas.drawRect(r,mPaint);        //为画笔添加混合样式        mPaint.setXfermode(cur_xfermode);        canvas.drawCircle(getWidth()/2,getHeight()/2,mwidth/2,mPaint);        canvas.drawCircle(getWidth()/2,getHeight()/2,mwidth/2,mBorderPaint);        // 还原混合模式        mPaint.setXfermode(null);        //还原画布        canvas.restoreToCount(sc);
//       正方形选择框//        //画笔填充颜色设置选择框其他区域半透明//        mPaint.setColor(Color.parseColor("#aa000000"));//        //设置画笔填充样式(设置画笔的样式,为FILL,FILL_OR_STROKE,或STROKE Style.FILL: 实心 STROKE:空心 FILL_OR_STROKE:同时实心与空心)//        mPaint.setStyle(Paint.Style.FILL);//        //绘制选择框左边的区域//        canvas.drawRect(0,0,marginAbout,getHeight(),mPaint);//        //绘制选择框右边的区域//        canvas.drawRect(getWidth()-marginAbout,0,getWidth(),getHeight(),mPaint);//        //绘制选择框顶部的区域//        canvas.drawRect(marginAbout,0,getWidth()-marginAbout,marginTopAndButtom,mPaint);//        //绘制选择框底部的区域//        canvas.drawRect(marginAbout,getHeight()-marginTopAndButtom,getWidth()-marginAbout,getHeight(),mPaint);//        //设置画笔颜色//        mPaint.setColor(color);//        //设置画笔填充样式//        mPaint.setStyle(Paint.Style.STROKE);//        // 当画笔样式为STROKE或FILL_OR_STROKE时,设置笔刷的粗细度//        mPaint.setStrokeWidth(mBorderWidth);//        //绘制选择框//        canvas.drawRect(marginAbout,marginTopAndButtom,getWidth()-marginAbout,getHeight()-marginTopAndButtom,mPaint);    }    /**     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)     */    public int dip2px(Context context, float dpValue) {        final float scale = context.getResources().getDisplayMetrics().density;        return (int) (dpValue * scale + 0.5f);    }}
应为主页xml只是调用这个自定义的View顺便给一个背景图片所以就不放上去了



0 0
原创粉丝点击