First look at the effect:
In fact, the principle of drawing board is very simple, that is, first record down the screen point, and then each move to let these two times move the point line, the cycle, the image is composed of many lines.
Core code:
public class Mainactivity extends activity implements Onclicklistener,onseekbarchangelistener {private View Red_view, Green_view,blue_view; Control three areas of brush color private SeekBar SeekBar;
Controls the drag bar of brush thickness private ImageView IV; Private Bitmap Bitmap,backgroud; Drawing paper and background map private Canvas Canvas; Canvas private Paint Paint;
Brush @Override protected void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Red_view = Findviewbyid (R.id.red_view);
Green_view = Findviewbyid (R.id.green_view);
Blue_view = Findviewbyid (R.id.blue_view);
SeekBar = (SeekBar) Findviewbyid (R.id.seekbar);
IV = (ImageView) Findviewbyid (R.ID.IV);
Iv.setimageresource (r.drawable.bg);
Get the background figure Backgroud = Bitmapfactory.decoderesource (Getresources (), r.drawable.bg);
Get the same size as the background drawing of the paper bitmap = Bitmap.createbitmap (Backgroud.getwidth (), Backgroud.getheight (), Backgroud.getconfig ());
To fix the paper on the canvas Canvas = new canvas (bitmap);
Get the brush paint = new paint (); Paint.setcolor (Color.Black);/set brush color to black paint.setstyle (style.stroke);//Set Brush to hollow//Add processing rules Matrix Matrix = new Ma
Trix ();
According to the background picture Canvas.drawbitmap (Backgroud, Matrix, paint);
Red_view.setonclicklistener (this);
Green_view.setonclicklistener (this);
Blue_view.setonclicklistener (this);
Seekbar.setonseekbarchangelistener (this);
Iv.setontouchlistener (New Ontouchlistener () {float startx;
float Starty; @Override public boolean Ontouch (View V, motionevent event) {switch (event.getaction ()) {//Get the sitting when pressed
Standard case MotionEvent.ACTION_DOWN:startX = Event.getx ();
Starty = Event.gety ();
Break
Gets the coordinates of the move case MotionEvent.ACTION_MOVE:float stopx = Event.getx ();
float stopy = event.gety ();
Draw a line between the start and end coordinates canvas.drawline (STARTX, Starty, stopx, stopy, paint); Real-time update coordinates (by continuous drawing lines) StartX = Event.getx ();
Starty = Event.gety ();
Iv.setimagebitmap (bitmap);
Break
Case MotionEvent.ACTION_UP:break;
Default:break;
return true;
}
});
@Override public void OnClick (View v) {switch (V.getid ()) {case R.id.red_view://Set brush color to red when clicking on red area)
Paint.setcolor (color.red);
Break
Case R.id.green_view://When clicking on the green area, set the brush color to green paint.setcolor (Color.green);
Break
Case R.id.blue_view://When the blue area is clicked set the brush color to Blue paint.setcolor (Color.Blue);
Break
Default:break; }///Drag the value of the bar to change the call @Override public void onprogresschanged (SeekBar SeekBar, int progress, Boolean fromuser) {}///When drag is invoked @Override public void Onstarttrackingtouch (SeekBar SeekBar) {}//When dragging stops @Override public void Onstoptrackingtouch (SeekBar SeekBar) {int progress = SEEKBAR.GETPRogress ()///drag bar default maximum value is Paint.setstrokewidth (progress/10f);//Control Brush thickness range (0-10)}}
Try to find it very simple, and try it quickly!
Again to Friday, I hope everyone has a happy weekend, thank you all have been to the small series of support, small series will redouble their efforts to share more wonderful articles.