Android手写板和涂鸦的功能,代码如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"      xmlns:greendroid="http://schemas.android.com/apk/res/com.cyrilmottier.android.gdcatalog"      android:layout_width="fill_parent"      android:layout_height="fill_parent"      android:orientation="vertical" >        <FrameLayout          android:id="@+id/tablet_view"          android:layout_width="fill_parent"          android:layout_height="300dp" >      FrameLayout>        <LinearLayout          android:layout_width="fill_parent"          android:layout_height="wrap_content"          android:background="@android:drawable/bottom_bar"          android:paddingTop="4dp" >            <Button              android:id="@+id/write_pad_ok"              android:layout_width="wrap_content"              android:layout_height="wrap_content"              android:layout_weight="1"              android:text="确定" />            <Button              android:id="@+id/write_pad_clear"              android:layout_width="wrap_content"              android:layout_height="wrap_content"              android:layout_weight="1"              android:text="清除" />            <Button              android:id="@+id/write_pad_cancel"              android:layout_width="wrap_content"              android:layout_height="wrap_content"              android:layout_weight="1"              android:text="取消" />      LinearLayout>    LinearLayout>  
public class MainActivity extends Activity {      private ImageView mIVSign;      private TextView mTVSign;      private Bitmap mSignBitmap;        /** Called when the activity is first created. */        @Override      public void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.activity_main);            mIVSign = (ImageView) findViewById(R.id.iv_sign);          mTVSign = (TextView) findViewById(R.id.tv_sign);            mTVSign.setOnClickListener(new OnClickListener() {                @Override              public void onClick(View view) {                  WritePadDialog mWritePadDialog new WritePadDialog(                          MainActivity.thisnew WriteDialogListener() {                                @Override                              public void onPaintDone(Object object) {                                  mSignBitmap = (Bitmap) object;                                  createSignFile();                                  mIVSign.setImageBitmap(mSignBitmap);                                  mTVSign.setVisibility(View.GONE);                              }                          });                    mWritePadDialog.show();              }          });      }            //创建签名文件      private void createSignFile() {          ByteArrayOutputStream baos null;          FileOutputStream fos null;          String path null;            File file null;          try {                path = Environment.getExternalStorageDirectory() + File.separator + System.currentTimeMillis() + ".jpg";               file new File(path);              fos new FileOutputStream(file);              baos new ByteArrayOutputStream();              //如果设置成Bitmap.compress(CompressFormat.JPEG, 100, fos) 图片的背景都是黑色的              mSignBitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);                byte[] b = baos.toByteArray();                if (b != null) {                    fos.write(b);               }            } catch (IOException e) {                e.printStackTrace();            } finally {                try {                    if (fos != null) {                      fos.close();                  }                  if (baos != null) {                      baos.close();                  }              } catch (IOException e) {                    e.printStackTrace();                }            }        }  }  

 

public class PaintView extends View {      private Paint mPaint;      private Path mPath;      private Bitmap mBitmap;      private Canvas mCanvas;        private int screenWidth, screenHeight;      private float currentX, currentY;        public PaintView(Context context, int screenWidth, int screenHeight) {          super(context);          this.screenWidth = screenWidth;          this.screenHeight = screenHeight;          init();      }        private void init() {          mPaint new Paint();          mPaint.setAntiAlias(true); // 去除锯齿          mPaint.setStrokeWidth(5);          mPaint.setStyle(Paint.Style.STROKE);          mPaint.setColor(Color.BLACK);            mPath new Path();            mBitmap = Bitmap.createBitmap(screenWidth, screenHeight, Config.ARGB_8888);          mCanvas new Canvas(mBitmap);  //      mCanvas.drawColor(Color.WHITE);      }        @Override      protected void onDraw(Canvas canvas) {          canvas.drawBitmap(mBitmap, 0, 0, null);          canvas.drawPath(mPath, mPaint);      }        @Override      public boolean onTouchEvent(MotionEvent event) {          float x = event.getX();          float y = event.getY();            switch (event.getAction()) {          case MotionEvent.ACTION_DOWN:              currentX = x;              currentY = y;              mPath.moveTo(currentX, currentY);              break;          case MotionEvent.ACTION_MOVE:              currentX = x;              currentY = y;              mPath.quadTo(currentX, currentY, x, y); // 画线              break;          case MotionEvent.ACTION_UP:              mCanvas.drawPath(mPath, mPaint);              break;          }            invalidate();          return true;      }        public Bitmap getPaintBitmap() {          return resizeImage(mBitmap, 320, 480);      }            public Path getPath() {          return mPath;      }        // 缩放      public static Bitmap resizeImage(Bitmap bitmap, int width, int height) {          int originWidth = bitmap.getWidth();          int originHeight = bitmap.getHeight();            float scaleWidth = ((float) width) / originWidth;          float scaleHeight = ((float) height) / originHeight;            Matrix matrix new Matrix();          matrix.postScale(scaleWidth, scaleHeight);          Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, originWidth,                  originHeight, matrix, true);          return resizedBitmap;      }        //清除画板      public void clear() {          if (mCanvas != null) {              mPath.reset();              mCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);              invalidate();          }      }  }  

 

 

public class WritePadDialog extends Dialog {      private Context mContext;      private WriteDialogListener mWriteDialogListener;      private PaintView mPaintView;      private FrameLayout mFrameLayout;      private Button mBtnOK, mBtnClear, mBtnCancel;        public WritePadDialog(Context context,              WriteDialogListener writeDialogListener) {          super(context);          this.mContext = context;          this.mWriteDialogListener = writeDialogListener;      }        @Override      protected void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          requestWindowFeature(Window.FEATURE_NO_TITLE); //无标题          setContentView(R.layout.write_pad);            mFrameLayout = (FrameLayout) findViewById(R.id.tablet_view);            // 获取屏幕尺寸          DisplayMetrics mDisplayMetrics = new DisplayMetrics();          getWindow().getWindowManager().getDefaultDisplay().getMetrics(mDisplayMetrics);          int screenWidth = mDisplayMetrics.widthPixels;          int screenHeight = mDisplayMetrics.heightPixels;          mPaintView new PaintView(mContext, screenWidth, screenHeight);          mFrameLayout.addView(mPaintView);          mPaintView.requestFocus();            mBtnOK = (Button) findViewById(R.id.write_pad_ok);          mBtnOK.setOnClickListener(new View.OnClickListener() {                @Override              public void onClick(View v) {                  if (mPaintView.getPath().isEmpty()) {                      Toast.makeText(mContext, "请写下你的大名", Toast.LENGTH_SHORT).show();                      return;                  }                  mWriteDialogListener.onPaintDone(mPaintView.getPaintBitmap());                  dismiss();              }          });            mBtnClear = (Button) findViewById(R.id.write_pad_clear);          mBtnClear.setOnClickListener(new View.OnClickListener() {                @Override              public void onClick(View v) {                  mPaintView.clear();              }          });            mBtnCancel = (Button) findViewById(R.id.write_pad_cancel);          mBtnCancel.setOnClickListener(new View.OnClickListener() {                @Override              public void onClick(View v) {                  cancel();              }          });      }  }  

 

/**  * 监听手写板对话框 * @author chengcj1  *   */  public interface WriteDialogListener {      public void onPaintDone(Object object);  }  

 

更多相关文章

  1. Android 开源绘画板项目 (ScaleSketchPadDemo)
  2. Android实现手写板和涂鸦功能
  3. Android -- 图片画画板(canvas、paint、bitmap)
  4. Android画板
  5. android实现简单的画画板
  6. android 抗锯齿效果
  7. Android canvas PaintFlagsDrawFilter 抗锯齿
  8. Android studio 学习3:实现倒计时、画板
  9. 调整 FMX Android 文字显示「锯齿」效果

随机推荐

  1. Java基础编程练习11:求sum=1+2+…+n,当n为
  2. JS 网站性能优化笔记
  3. Java基础编程练习10:求1!+2!+3!+…+n!, n<
  4. javascript数据类型转换
  5. Java基础编程练习9:已知三角形的三条边a,b
  6. 2021北京高新技术企业申报难度及相关要求
  7. Java基础编程练习8:猜数字游戏
  8. Java基础编程练习7:百钱百鸡问题。
  9. Linux宝塔负载均衡使用教程
  10. javascript创建对象几种方式