Android編程實(shí)現(xiàn)的手寫板和涂鴉功能
本文實(shí)例講述了Android編程實(shí)現(xiàn)的手寫板和涂鴉功能。分享給大家供大家參考,具體如下:
下面仿一個Android手寫板和涂鴉的功能,直接上代碼:
write_pad.xml
<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>
這個是手寫板的主要布局文件,能夠手寫的部分是一個FrameLayout。下面有確定、清除和取消按鈕,用來保存和擦除簽名。
主要代碼邏輯如下:
MainActivity.java
package com.jackie.handwriting; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import android.app.Activity; import android.graphics.Bitmap; import android.os.Bundle; import android.os.Environment; import android.view.View; import android.view.View.OnClickListener; import android.widget.ImageView; import android.widget.TextView; 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.this, new WriteDialogListener() { @Override public void onPaintDone(Object object) { mSignBitmap = (Bitmap) object; createSignFile(); mIVSign.setImageBitmap(mSignBitmap); mTVSign.setVisibility(View.GONE); } }); mWritePadDialog.show(); } }); } //創(chuàng)建簽名文件 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(); //如果設(shè)置成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(); } } } }
PaintView.java
package com.jackie.handwriting; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Bitmap.Config; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Matrix; import android.graphics.Paint; import android.graphics.Path; import android.graphics.PorterDuff; import android.view.MotionEvent; import android.view.View; 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(); } } }
WritePadDialog.java
package com.jackie.handwriting; import android.app.Dialog; import android.content.Context; import android.os.Bundle; import android.util.DisplayMetrics; import android.view.View; import android.view.Window; import android.widget.Button; import android.widget.FrameLayout; import android.widget.Toast; 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); //無標(biāo)題 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(); } }); } }
WriteDilogListener.java
package com.jackie.handwriting; /** * 監(jiān)聽手寫板對話框 * @author chengcj1 * */ public interface WriteDialogListener { public void onPaintDone(Object object); }
效果如下:
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android圖形與圖像處理技巧總結(jié)》、《Android開發(fā)入門與進(jìn)階教程》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對大家Android程序設(shè)計有所幫助。
相關(guān)文章
ScrollView嵌套ListView及ListView嵌套的高度計算方法
下面小編就為大家分享一篇ScrollView嵌套ListView及ListView嵌套的高度計算方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01完美解決客戶端webview持有的頁面緩存,不會立即釋放的問題
下面小編就為大家?guī)硪黄昝澜鉀Q客戶端webview持有的頁面緩存,不會立即釋放的問題。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-12-12Android編程中Tween動畫和Frame動畫實(shí)例分析
這篇文章主要介紹了Android編程中Tween動畫和Frame動畫,結(jié)合實(shí)例形式較為詳細(xì)的分析了Android中Tween動畫和Frame動畫的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2015-12-12android實(shí)現(xiàn)查詢公交車還有幾站的功能
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)查詢公交車還有幾站的功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05