设计布局文件
activiy_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <com.example.ex_4_5.HandWrite android:id="@+id/handwriteview" android:layout_height="380dp" android:layout_width="380dp" tools:ignore="MissingConstraints" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" android:gravity="center_horizontal"> <Button android:layout_width="200dp" android:layout_height="wrap_content" android:id="@+id/clear" android:text="清屏"/> </LinearLayout> </LinearLayout>
设计主控文件MainActivity.java
package com.example.ex_4_5; import android.app.Activity; import android.view.View.OnClickListener; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity { private HandWrite handWrite = null; private Button clear = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); handWrite = (HandWrite) findViewById(R.id.handwriteview); clear = (Button) findViewById(R.id.clear); clear.setOnClickListener(new mClick()); } private class mClick implements OnClickListener { @Override public void onClick(View v) { handWrite.clear(); } } } 记录滑动痕迹HandWrite.java
package com.example.ex_4_5; 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.support.annotation.Nullable; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; public class HandWrite extends View { Paint paint = null; Bitmap originalBitmap = null; Bitmap new1_Bitmap = null; Bitmap new2_Bitmap = null; float startX = 0, startY = 0; float clickX = 0, clickY = 0; boolean isMove = true; boolean isClear = false; int color = Color.GREEN; float strokeWidth = 2.0f; public HandWrite(Context context, @Nullable AttributeSet attrs) { super(context, attrs); originalBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.cy).copy(Bitmap.Config.ARGB_8888, true); new1_Bitmap = Bitmap.createBitmap(originalBitmap); } public void clear() { isClear = true; new2_Bitmap = Bitmap.createBitmap(originalBitmap); invalidate(); } public void setstyle(float strokeWidth) { this.strokeWidth = strokeWidth; } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawBitmap(HandWriting(new1_Bitmap), 0, 0, null); } public Bitmap HandWriting(Bitmap o_bitmap) { Canvas canvas = null; if (isClear) { canvas = new Canvas(new2_Bitmap); } else { canvas = new Canvas(o_bitmap); } paint = new Paint(); paint.setStyle(Paint.Style.STROKE); paint.setAntiAlias(true); paint.setColor(color); paint.setStrokeWidth(strokeWidth); if (isMove) { canvas.drawLine(startX, startY, clickX, clickY, paint); } startX = clickX; startY = clickY; if (isClear) { return new2_Bitmap; } return o_bitmap; } @Override public boolean onTouchEvent(MotionEvent event) { 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); } }
运行结果