public class MainActivity extends AppCompatActivity implements View.OnClickListener { private ImageView mImage; /** * 修改颜色 */ private Button mButColor; /** * 修改线宽 */ private Button mButLine; private Bitmap bitmapCopy; private Canvas canvas; private Paint paint; /** * 画布Canvas 画笔paint 手势识别器 * 整体思路,因为我是图片作画,实际上是对图片进行修改,起到画图的效果 * 1.原图,白纸,画布,画笔 * 2.根据手势识别进行作画 * * @param savedInstanceState */ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); //加载原图 Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.aaa); //做一个图片的复制 bitmapCopy = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig()); //创建画布 canvas = new Canvas(bitmapCopy); //创建画笔 paint = new Paint(); //开始画画 canvas.drawBitmap(bitmap,new Matrix(),paint); //设置手势识别 mImage.setOnTouchListener(new View.OnTouchListener() { private float starty; private float startx; @Override public boolean onTouch(View view, MotionEvent motionEvent) { switch(motionEvent.getAction()){ case MotionEvent.ACTION_DOWN: startx = motionEvent.getX(); starty = motionEvent.getY(); break; case MotionEvent.ACTION_MOVE: float x = motionEvent.getX(); float y = motionEvent.getY(); canvas.drawLine(startx,starty,x,y,paint); startx=x; starty=y; mImage.setImageBitmap(bitmapCopy); break; default: break; } return true; } }); } private void initView() { mImage = (ImageView) findViewById(R.id.image); mButColor = (Button) findViewById(R.id.butColor); mButColor.setOnClickListener(this); mButLine = (Button) findViewById(R.id.butLine); mButLine.setOnClickListener(this); } //这里指示简单的修改了颜色和线宽 @Override public void onClick(View v) { switch (v.getId()) { case R.id.butColor: paint.setColor(Color.RED); break; case R.id.butLine: paint.setStrokeWidth(10); // paint.setTextSize(1500); break; } } }
//保存文件
File file=new File(path,"AA"); try { file.createNewFile(); FileOutputStream fos=new FileOutputStream(file); bitmapCopy.compress(Bitmap.CompressFormat.JPEG,50,fos); } catch (IOException e) { e.printStackTrace(); }