您的位置:首页 > 其它

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

2015-09-18 22:19 357 查看
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>


蒙版为图片:



背景为图片:



换背景:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: