canvas新建画布,并将绘画结果保存到本地。

其实画图就是在画布上画出图形。废话不多说,直接上代码
bitmap = Bitmap.createBitmap(600, 600, Config.ARGB_8888);
//用createBitmap方法实例化一个新bitmap,bitmap并没有指定的图片
Canvas canvas = new Canvas(bitmap);//将bitmap作为画布
canvas.drawColor(Color.WHITE);//设置画布的背景颜色
Paint paint = new Paint();//新建一个画笔
paint.setAntiAlias(true);//抗锯齿效果
paint.setStyle(Style.STROKE);//空心圆效果
paint.setColor(Color.RED);
canvas.drawCircle(200, 120, 80, paint);
canvas.drawCircle(240, 80, 20, paint);
canvas.drawCircle(160, 80, 20, paint);
canvas.drawLines(new float[]{
200,100, 190,120,
200,100,210,120,
190,120,210,120}, paint);//三角形,坐标(200,100),( 190,120),(210,120)两两连线画图
// RectF rectF=new RectF(80, 100, 120, 30);
canvas.drawLine(160, 180, 240, 180, paint);//直线

// canvas.drawBitmap(bitmap,100, 100,paint);
photo.setImageBitmap(bitmap);
save();
接下来画好了圆,三角形鼻子,直线嘴巴,就可以保存图片了。
public void save(){
FileOutputStream fos=null;
try {
File file = new File(“/sdcard/pictures”);
if (!file.exists()) {
file.mkdir();//不存在该文件夹就重新创建
}
fos=new FileOutputStream(new File(file,System.currentTimeMillis()+”.jpg”));//jpg后缀
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        Log.i("tips", "fail to save ");
    }finally{
        if (fos!=null) {
            try {
                fos.close();
                fos.flush();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            Log.i("tips", "success to save ");
        }
    }

}

其实代码就这么多了。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xwjy123

谢谢您的帮助

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值