第四十二天 BitmapView(蒙版,或模拟给图片打马赛克)
2015-09-18 22:19
357 查看
BitmapView
MainActivity
values目录下:myview_attr.xml
layout布局:
蒙版为图片:
背景为图片:
换背景:
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>
蒙版为图片:
背景为图片:
换背景:
相关文章推荐
- 增加用户体验的细节--三个按钮的交互设计思维
- 优秀APP启动页的设计思维
- 从url到页面显示发生了什么
- Android性能优化之内存篇
- Android -Camer使用!
- 结构体变量的初始化
- 零基础掌握交互设计的重点笔记
- gdb调试命令及方法
- "无法删除数据库,因为该数据库当前正在使用"问题解决
- 绿色版MySql的安装和配置root密码
- 线段树入门理解
- ios开发之AppDelegate
- Web网页性能管理详解
- codeforces #319 div 2 E C. Points on Plane (分块)
- 为什么mysql不能远程访问?
- Tcp/ip三次握手,4次分手.
- spring 配置Spring数据源
- objective-c
- Mac os x 下配置Intellij IDEA + Tomcat 出现权限问题的解决办法
- 杭电1003 Max Sum