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