自定义view的类,代码如下:
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); } }
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); } } } }
<?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="清屏" /> <Button android:id="@+id/save" android:layout_width="50dp" android:layout_height="wrap_content" android:text="保存" /> </LinearLayout> <ImageView android:id="@+id/saveimage" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> 下过图如下:
自己可以再加点功能了,比如整合都短信发送里面里面......