The construction of Android's drawing application

Source: Internet
Author: User

1. Required CONTROLS:

(1) Paint: Brush, you can set the color of paint lines, thickness, sawtooth have no, etc., with the tool of drawing;

(2) Canvas: canvas (need to pass in the Bitmap object), the object can be used to achieve the graphic effect you want to draw, and finally implement the bitmap

such as: straight lines, rectangles, ellipses, etc.;

(3) Bitmap: Bitmap, the control to implement the canvas graphics;

(4) ImageView: The control that loads the bitmap;

(5) Touch screen listener: Realize Ontouchlistener to complete touch screen drawing response events and operations;

2, the construction process:

(1) First define the class to implement the touch screen listening interface (ontouchlistener)

1) to achieve the response according to the touch screen operation of the drawing, in the monitoring must be in advance to build a canvas, brush and other drawing- related controls. However, since the (Bitmap) bitmap size needs to be adapted to the event source object (i.e. , ImageView),

You should create the adaptive event source v in the first public boolean OnTouch (View V, motionevent event) method (Bitmap ) bitmap. Creating a bitmap can call the static creation method of the class:

Bitmap=bitmap.creatbitmap ( width , height , number of bitmap bits );

Create canvas again: New Canvas (bitmap)

With the brush (you can also set the corresponding property at this time);

2) The event object contains the coordinates of the touch screen and the events of the touch screen. Perform the corresponding action according to the touch screen event:

Example:motionevent.action_down(Press): Gets the first coordinate

Motionevent.action_up(Loosen): Gets the second coordinate and draws the desired graphic

(canvas.drawline (x1,y1,x2,y2,paint) line, rectangle, circle, etc.)

Motionevent.action_move(move): Gets the second coordinate, which is connected in two coordinates,

Then use the coordinates of the second time to cover the first coordinates, so that you can draw the graphics you want to draw;

3) because the canvas canvas The incoming bitmap, canvas draw operation is just a drawing in the bitmap. To imageview show it, it must be in every canvas draw operation (update of the Ascended image), ( imageview iv= (ImageView) v;

(2) Add a ImageViewin the Activity.xml interface, in Activity.java find it and set it to a touchscreen listener

The construction of Android's drawing application

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.