第四十二天 BitmapView(蒙版,或模拟给图片打马赛克)

BitmapView

public class BitmapView extends View {
    private int width;
    private int height;
    private Bitmap mBitmap;
    private Paint mPaintCircel;
    private Paint mPaintRect;
    private Canvas canvasBit;
    private Bitmap mBitmapBackground;
    private Bitmap back;
    private Path path;
    private float x;
    private float y;
    private float old_x;
    private float old_y;
    public BitmapView(Context context) {
        super(context);
    }

    public BitmapView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mPaintRect=new Paint();
        //切换背景图片
        final TypedArray array=context.obtainStyledAttributes(attrs,R.styleable.BitmapView);
        BitmapDrawable drawable= (BitmapDrawable) array.getDrawable(R.styleable.BitmapView_mybackground);
        Log.d("drawable","得到背景图片");
        if (drawable!=null){
            Log.d("drawable","drawable:"+drawable.getIntrinsicWidth());
            mBitmapBackground=drawable.getBitmap();
        }else{
            mBitmapBackground= BitmapFactory.decodeResource(getResources(), R.mipmap.photo);
        }
        int paintWidth=array.getDimensionPixelOffset(R.styleable.BitmapView_mypaintwidth,50);
        mPaintRect.setStrokeWidth(paintWidth);
        mPaintCircel=new Paint();
        mPaintCircel.setColor(Color.CYAN);//圆形设置为青色
        mPaintRect.setColor(Color.YELLOW);//矩形设置为黄色
        PorterDuffXfermode mode=new PorterDuffXfermode(PorterDuff.Mode.XOR);//src_over蓝色被覆盖  DST_OVER:黄色被覆盖XOR
        mPaintRect.setXfermode(mode);
        mPaintRect.setStrokeJoin(Paint.Join.ROUND);//设置绘制中间使用的样式:笔头为圆滑的
        mPaintRect.setStrokeCap(Paint.Cap.ROUND);
        mPaintRect.setStyle(Paint.Style.FILL_AND_STROKE);
        mPaintRect.setPathEffect(new CornerPathEffect(360));
        mPaintRect.setAntiAlias(true);
       // mBitmapBackground= BitmapFactory.decodeResource(getResources(), R.mipmap.photo);
        path=new Path();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        width=getDefaultSize(getSuggestedMinimumWidth(),widthMeasureSpec);
        height=getDefaultSize(getSuggestedMinimumHeight(),heightMeasureSpec);
        setMeasuredDimension(width,height);
        mBitmap=Bitmap.createBitmap(width,height, Bitmap.Config.ARGB_8888);
        canvasBit=new Canvas(mBitmap);//必须画在Bitmap上
        back=Bitmap.createBitmap(width,height, Bitmap.Config.ARGB_8888);
        Canvas canvas=new Canvas(back);
        canvas.drawBitmap(mBitmapBackground,
                new Rect(0,0,mBitmapBackground.getWidth(),mBitmapBackground.getHeight()),
                new Rect(0,0,width,height),null);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        //背景为图片,蒙版为自定义矩形
        canvas.drawBitmap(mBitmapBackground,
                new Rect(0,0,mBitmapBackground.getWidth(),mBitmapBackground.getHeight()),
                new Rect(0,0,width,height),null);//背景
        canvasBit.drawRect(0,0,width,height,mPaintCircel);//蒙版


        //背景为自定义矩形,蒙版为图片
//        canvas.drawRect(0, 0, width, height, mPaintCircel);
//        canvasBit.drawBitmap(mBitmapBackground,
//                new Rect(0,0,mBitmapBackground.getWidth(),mBitmapBackground.getHeight()),
//                new Rect(0,0,width,height),null);
        canvasBit.drawPath(path, mPaintRect);//路径,圆
        canvas.drawBitmap(mBitmap,0,0,null);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                x=event.getX();
                y=event.getY();
                path.moveTo(x,y);
                invalidate();
                old_x=x;
                old_y=y;
                return true;
            case MotionEvent.ACTION_MOVE:
                x=event.getX();
                y=event.getY();
                path.moveTo(old_x,old_y);
              //  path.lineTo(x,y);//划线
                path.quadTo((x+old_x)/2,(y+old_y)/2,x,y);//划贝塞尔曲线
                invalidate();
                old_x=x;
                old_y=y;
                return true;
        }
        return super.onTouchEvent(event);
    }
}

MainActivity

public class MainActivity extends Activity {
    private Button mButtonBitmap;
    private BitmapView mBitmapView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bitmapview);
        //生成图片,保存当前状态
        mBitmapView= (BitmapView) findViewById(R.id.bitmapview);
        mButtonBitmap= (Button) findViewById(R.id.button_bitmapview);
        mButtonBitmap.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mBitmapView.setDrawingCacheEnabled(true);
                Bitmap bitmap=mBitmapView.getDrawingCache(true);
                File file=new File(Environment.getExternalStorageDirectory(),System.currentTimeMillis()+".jpg");
                if(!file.exists()){
                    try {
                        file.createNewFile();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                try {
                    bitmap.compress(Bitmap.CompressFormat.JPEG,100,new FileOutputStream(file));
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}

values目录下:myview_attr.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="BitmapView">
        <attr name="mybackground" format="reference"></attr>
        <attr name="mypaintwidth" format="dimension"></attr>
    </declare-styleable>
</resources>

layout布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:myview="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:id="@+id/button_bitmapview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="生成图片"/>
    <com.example.administrator.mypathdemo.view.BitmapView
        android:id="@+id/bitmapview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        myview:mybackground="@mipmap/view1"
        myview:mypaintwidth="20dp"/>
</LinearLayout>

蒙版为图片:

这里写图片描述

背景为图片:

这里写图片描述

换背景:

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值