Android implements graffiti, saves graffiti images, and clears the screen

Source: Internet
Author: User

The code for customizing the view class is as follows:
[Html]
Package com. xy. tuya;
 
Import android. annotation. SuppressLint;
Import android. content. Context;
Import android. graphics. Bitmap;
Import android. graphics. BitmapFactory;
Import android. graphics. Canvas;
Import android. graphics. Color;
Import android. graphics. Paint;
Import android. graphics. Paint. Style;
Import android. OS. Handler;
Import android. OS. Message;
Import android. util. AttributeSet;
Import android. util. Log;
Import android. view. MotionEvent;
Import android. view. View;
 
Public class MyView extends View {
Private Paint paint = null;
Private Bitmap originalBitmap = null;
Private Bitmap new1Bitmap = null;
Private Bitmap new2Bitmap = null;
Private float clickX = 0, clickY = 0;
Private float startX = 0, startY = 0;
Private boolean isMove = true;
Private boolean isClear = false;
Private int color = Color. GREEN;
Private float strokeWidth = 2.0f;
 
Public MyView (Context context, AttributeSet attrs ){
Super (context, attrs );
OriginalBitmap = BitmapFactory. decodeResource (getResources (),
R. drawable. a1). copy (Bitmap. Config. ARGB_8888, true );
New1Bitmap = Bitmap. createBitmap (originalBitmap );
SetDrawingCacheEnabled (true );
Log. I ("RG", "new1Bitmap --- >>>" + new1Bitmap );
}
 
Public void clear (){
IsClear = true;
New2Bitmap = Bitmap. createBitmap (originalBitmap );
Invalidate ();
}
 
Bitmap saveImage = null;
 
Public Bitmap saveImage (){
If (saveImage = null ){
Return null;
}
Return saveImage;
}
 
Public void setImge (){
SaveImage = null;
}
 
Public void setstyle (float strokeWidth ){
This. strokeWidth = strokeWidth;
}
 
Handler handler1;
 
@ SuppressLint ("DrawAllocation ")
@ Override
Protected void onDraw (Canvas canvas ){
Super. onDraw (canvas );
Canvas. drawBitmap (HandWriting (new1Bitmap), 0, 0, null );
Handler1 = new Handler (){
 
@ Override
Public void handleMessage (Message msg ){
 
Log. I ("RG ","--------------------");
Int what = msg. what;
If (what = 2 ){
SaveImage = Bitmap. createBitmap (HandWriting (new1Bitmap ));
Message msg1 = new Message ();
Msg1 = Message. obtain (MainActivity. hh, 3 );
MainActivity. hh. sendMessage (msg1 );
}
Super. handleMessage (msg );
}
 
};
 
}
 
@ SuppressLint ("HandlerLeak ")
Handler handler;
 
Public Bitmap HandWriting (Bitmap originalBitmap ){
Handler = new Handler (){
 
@ Override
Public void handleMessage (Message msg ){
 
Int what = msg. what;
If (what = 1 ){
StartX = clickX;
StartY = clickY;
}
Super. handleMessage (msg );
}
 
};
Canvas canvas = null;
 
If (isClear ){
Canvas = new Canvas (new2Bitmap );
Log. I ("RG", "canvas ");
} Else {
Canvas = new Canvas (originalBitmap );
}
Paint = new Paint ();
Paint. setStyle (Style. STROKE );
Paint. setAntiAlias (true );
Paint. setColor (color );
Paint. setStrokeWidth (strokeWidth );
Log. I ("RG", "startX -- >>>" + startX );
Log. I ("RG", "startY -- >>>" + startY );
If (isMove ){
Canvas. drawLine (startX, startY, clickX, clickY, paint );
}
 
If (isClear ){
Return new2Bitmap;
}
 
Return originalBitmap;
}
 
@ Override
Public boolean onTouchEvent (MotionEvent event ){
Message msg = new Message ();
Msg = Message. obtain (handler, 1 );
Handler. sendMessage (msg );
ClickX = event. getX ();
ClickY = event. getY ();
If (event. getAction () = MotionEvent. ACTION_DOWN ){
 
IsMove = false;
Invalidate ();
Return true;
} Else if (event. getAction () = MotionEvent. ACTION_MOVE ){
 
IsMove = true;
Invalidate ();
Return true;
}
 
Return super. onTouchEvent (event );
}
 
}

The mainactiviry class code is as follows:
[Html]
Package com. xy. tuya;
 
Import android. opengl. Visibility;
Import android. OS. Bundle;
Import android. OS. Handler;
Import android. OS. Message;
Import android. app. Activity;
Import android. graphics. Bitmap;
Import android. graphics. drawable. Drawable;
Import android. util. Log;
Import android. view. Menu;
Import android. view. MenuItem;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
Import android. widget. ImageView;
Import android. support. v4.app. NavUtils;
 
Public class MainActivity extends Activity {
/** Called when the activity is first created .*/
Private MyView handWrite = null;
Private Button clear = null;
Private Button save = null;
Private ImageView saveImage = null;
 
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
 
HandWrite = (MyView) findViewById (R. id. handwriteview );
Save = (Button) findViewById (R. id. save );
SaveImage = (ImageView) findViewById (R. id. saveimage );
Clear = (Button) findViewById (R. id. clear );
Clear. setOnClickListener (new clearListener ());
Save. setOnClickListener (new saveListener ());
Handerl ();
}
 
Private class clearListener implements OnClickListener {
 
Public void onClick (View v ){
HandWrite. clear ();
}
}
 
Static Handler hh;
 
Public void handerl (){
Hh = new Handler (){
 
@ Override
Public void handleMessage (Message msg ){
Int what = msg. what;
If (what = 3 ){
SaveImage. setVisibility (View. VISIBLE );
SaveImage. setImageBitmap (handWrite. saveImage ());
HandWrite. setImge ();
}
Super. handleMessage (msg );
}
 
};
}
 
Private class saveListener implements OnClickListener {
 
@ Override
Public void onClick (View v ){
 
Message msg = new Message ();
Msg = Message. obtain (handWrite. handler1, 2 );
HandWrite. handler1.sendMessage (msg );
 
If (handWrite. saveImage ()! = Null ){
Log. I ("RG", "111111111111111111111 ");
 
} Else {
SaveImage. setVisibility (View. GONE );
}
}
 
}
}

The xml layout code is as follows:
[Html]
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "vertical">
 
<LinearLayout
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content">
 
<Com. zte. tuya. MyView
Android: id = "@ + id/handwriteview"
Android: layout_width = "fill_parent"
Android: layout_height = "300dp"/>
</LinearLayout>
 
<LinearLayout
Android: layout_width = "wrap_content"
Android: layout_height = "40dip"
Android: orientation = "horizontal">
 
<Button
Android: id = "@ + id/clear"
Android: layout_width = "50dp"
Android: layout_height = "wrap_content"
Android: text = "clear screen"/>
 
<Button
Android: id = "@ + id/save"
Android: layout_width = "50dp"
Android: layout_height = "wrap_content"
Android: text = "save"/>
</LinearLayout>
 
<ImageView
Android: id = "@ + id/saveimage"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"/>
 
</LinearLayout>
 
The figure below is as follows:


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.