Android Small Project Artboard

Source: Internet
Author: User
Tags gety

Welcome to my communication group: 386451316

These days really exhausted, and today climbed up to write a small project in fact there is no technology to write before writing a No this is much better program do not remember to put there is not to say

Start on the code to put

Or the last picture, or else no one is watching.

Layout code:

<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:orientation= "Vertical"
Tools:context= ". Mainactivity ">

<com.striver.drawingeboard.myview
Android:id= "@+id/draw"
Android:layout_width= "Fill_parent"
android:layout_height= "0DP"
android:layout_weight= "1"/>
<button
Android:id= "@+id/btn"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:text= "Clean canvas"/>

</LinearLayout>

Code files

Main activity

public class Mainactivity extends Activity {

Public Button btn;
Private MyView view;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
BTN = (Button) Findviewbyid (R.ID.BTN);
View = (MyView) Findviewbyid (R.id.draw);
Btn.setonclicklistener (New Onclicklistener () {
@Override
public void OnClick (View v) {
TODO auto-generated Method Stub
View.clear ();
}
});
}
}

Custom View class


/**
* Custom Picture version
* @author STRIVER_ZW
* Date 2014-12-26
*/
public class MyView extends Surfaceview implements callback,ontouchlistener{

Private Paint P = new paint ();
Private path PATH = new path ();//path

Public MyView (context context, AttributeSet Attrs) {
Super (context, attrs);
callback function
Getholder (). Addcallback (this);
Initializing the control
P.setcolor (Color.Blue);
P.settextsize (15);
P.setantialias (TRUE);
P.setstyle (Style.stroke);
Listening
Setontouchlistener (this);
}
public void Draw () {
Lock the canvas
Canvas canvas = Getholder (). Lockcanvas ();
Canvas color
Canvas.drawcolor (Color.White);
Canvas.drawpath (path, p);

Unlock Canvas
Getholder (). Unlockcanvasandpost (canvas);
}

Public MyView (Context context) {
Super (context);
TODO auto-generated Constructor stub
//}

Clear Canvas
public void Clear () {
Path.reset ();//Clear the path
Draw ();
}
@Override
public void surfacechanged (surfaceholder holder, int format, int width,
int height) {
Start calling the canvas
Draw ();

}

@Override
public void surfacecreated (Surfaceholder holder) {
TODO auto-generated Method Stub

}

@Override
public void surfacedestroyed (Surfaceholder holder) {
TODO auto-generated Method Stub

}
Move Event
@Override
public boolean OnTouch (View V, motionevent event) {
TODO auto-generated Method Stub
Switch (event.getaction ()) {
Case motionevent.action_down://Pressing Event
Path.moveto (Event.getx (), event.gety ());
Draw ();
Break

Case motionevent.action_move://Move Event
Path.lineto (Event.getx (), event.gety ());
Draw ();
Break
}
return true;
}

}

Android Small Project Artboard

Related Article

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.