<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />public class MainActivity extends Activity {float blurRadius = 5f;//在使用光之前的模糊不清的面具的宽度。private DrawView drawView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);drawView = new DrawView(this);setContentView(drawView);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {new MenuInflater(this).inflate(R.menu.main, menu);return super.onCreateOptionsMenu(menu);}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {switch (item.getItemId()) {case R.id.red:drawView.getPaint().setColor(Color.RED);break;case R.id.blue:drawView.getPaint().setColor(Color.BLUE);break;case R.id.green:drawView.getPaint().setColor(Color.GREEN);break;case R.id.width_3:drawView.getPaint().setStrokeWidth(dp2px(3));break;case R.id.width_6:drawView.getPaint().setStrokeWidth(dp2px(6));break;case R.id.emboss://浮雕效果drawView.getPaint().setMaskFilter(new EmbossMaskFilter(new float[] { 0.5f, 1f, 1.5f }, 0.6f, 5f, blurRadius));//float[] direction 用来指示光源照的方向;float ambient 光的强度系数;float specular 镜子的高亮系数;float blurRadius 在使用光之前的模糊不清的面具的宽度break;case R.id.blur://模糊效果blurRadius = drawView.getPaint().getStrokeWidth();drawView.getPaint().setMaskFilter(new BlurMaskFilter(8, BlurMaskFilter.Blur.NORMAL));break;case R.id.save:Bitmap bitmap = drawView.getCacheBitmap();if (bitmap != null) saveBitmap2Pic(bitmap);else Toast.makeText(this, "保存失败", Toast.LENGTH_SHORT).show();break;default:break;}return true;}/*** 根据手机的分辨率从 dp 的单位 转成为 px(像素)*/public int dp2px(float dpValue) {float scale = getResources().getDisplayMetrics().density;return (int) (dpValue * scale + 0.5f);}/** 保存bitmap为图片 */public void saveBitmap2Pic(Bitmap bitmap) {File file = new File(Environment.getExternalStorageDirectory(), new SimpleDateFormat("yyyy.MM.dd HH-mm-ss", Locale.getDefault()).format(new Date()) + ".png");try {FileOutputStream out = new FileOutputStream(file);bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);out.flush();out.close();} catch (Exception e) {Toast.makeText(this, "保存出现异常", Toast.LENGTH_SHORT).show();e.printStackTrace();}}}
public class DrawView extends View {private float preX;//前一个点的(x,y)坐标private float preY;private Path path;private Bitmap cacheBitmap = null; //最核心的地方。定义一个内存中的图片,将图片作为缓冲区。大小为屏幕宽高的大小。private Canvas cacheCanvas = null; //定义cacheBitmap的画布private Paint cachePaint = null;//定义cacheBitmap的画笔public Bitmap getCacheBitmap() {return cacheBitmap;}public Paint getPaint() {return cachePaint;}public DrawView(Context context) {this(context, null);}public DrawView(Context context, AttributeSet attrs) {super(context, attrs);cacheBitmap = Bitmap.createBitmap(getResources().getDisplayMetrics().widthPixels, getResources().getDisplayMetrics().heightPixels, Config.ARGB_8888);cacheCanvas = new Canvas(cacheBitmap);path = new Path();cachePaint = new Paint();cachePaint.setColor(Color.BLACK);cachePaint.setStyle(Paint.Style.STROKE);cachePaint.setStrokeWidth(1 * context.getResources().getDisplayMetrics().density + 0.5f);//1dpcachePaint.setAntiAlias(true);cachePaint.setDither(true);}@SuppressLint("ClickableViewAccessibility")//意思是:有可能会和点击事件发生冲突,如果你在touch中返回了true,那么就不会响应onClick事件了@Overridepublic boolean onTouchEvent(MotionEvent event) {float x = event.getX();float y = event.getY();switch (event.getAction()) {case MotionEvent.ACTION_DOWN:path.moveTo(x, y);preX = x;preY = y;break;case MotionEvent.ACTION_MOVE:path.quadTo(preX, preY, x, y);//平滑的"贝塞尔曲线"(Bezier curve)。其中,x1,y1为控制点的坐标值,x2,y2为终点的坐标值;//path.lineTo(preX, preY);//由于onTouchEvent事件回调非常频繁,所以用lineTo方法也是可以的preX = x;preY = y;break;case MotionEvent.ACTION_UP:cacheCanvas.drawPath(path, cachePaint);//当手指移开时,把曲线画到cacheCanvas中,在onDraw中又把cacheCanvas画到我们的自定义View中。path.reset();break;default:break;}invalidate();return true;}@SuppressLint("DrawAllocation")@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);canvas.drawColor(Color.WHITE);canvas.drawBitmap(cacheBitmap, 0, 0, cachePaint);canvas.drawPath(path, cachePaint);}}
<!-- <menu>标签是根元素,他没有属性,在<menu>里面可嵌套<item>和<group>子元素 --><menu xmlns:android="http://schemas.android.com/apk/res/android" ><!-- <item>标签表示具体的菜单项,<item>元素中也可嵌套<menu>形成子菜单 --><item android:title="选择颜色"><menu><!-- <group>标签表示一个菜单组,相同的菜单组可以一起设置其属性,checkableBehavior:选择行为:单选、多选 --><group android:checkableBehavior="single" ><itemandroid:id="@+id/red"android:title="红色"/><itemandroid:id="@+id/green"android:title="绿色"/><itemandroid:id="@+id/blue"android:title="蓝色"/></group></menu></item><itemandroid:id="@+id/width_3"android:title="3dp"/><itemandroid:id="@+id/width_6"android:title="6dp"/><itemandroid:id="@+id/blur"android:title="模糊效果"/><itemandroid:id="@+id/emboss"android:title="浮雕效果"/><itemandroid:id="@+id/save"android:title="保存为图片"/></menu>