第四十二天 BitmapView(蒙版,或模拟给图片打马赛克)
来源:互联网 发布:iis7怎么执行php 编辑:程序博客网 时间:2024/04/20 18:56
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
- 第四十二天 BitmapView(蒙版,或模拟给图片打马赛克)
- 用PHOTOSHOP给图片打马赛克
- JS使用canvas给图片打马赛克
- 图片打马赛克
- linux下使用open(),write(),给bmp图片打马赛克
- 利用标准I/O给图片打马赛克
- php 图片局部打马赛克
- php 图片局部打马赛克
- php 图片局部打马赛克
- html5 canvas 图片打马赛克 demo
- Android 对整张图片打马赛克
- /给图片打水印
- java给图片添加图片水印,文字水印,马赛克
- 视频如何打马赛克
- 如何打马赛克
- 如何给图片打水印
- 给上传图片打水印
- Java给图片打水印
- js 无法删除cookie的问题
- 第一个java程序:错误: 编码GBK的不可映射字符
- Windows下如何构建和发布Python模块
- eclipse 无法链接手机
- android BrocadcastReceiver
- 第四十二天 BitmapView(蒙版,或模拟给图片打马赛克)
- poj3255
- 网络配置
- access vba打开另一个数据库并关闭当前数据库的代码
- 因果图小例子
- 在XML数据库中高效的关键字搜索求SLCAs
- Bash shell 快捷键
- iOS数据请求之post异步同步
- 网络图可视化工具