This article describes the effect of Android programming to implement graffiti on Bitmap. Share it for your reference, as follows:
Layout file:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andro android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <LinearLayout android: android:layout_width="wrap_content" android:layout_height="wrap_content" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" android:gravity="center_horizontal" > <Button android: android:layout_width="200dp" android:layout_height="wrap_content" android:text="Clear the screen" /> </LinearLayout> </LinearLayout>
Rewritten View file:
public class HandWrite 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 = ; private float strokeWidth = 2.0f; public HandWrite(Context context,Bitmap b) { super(context); originalBitmap = (b).copy(.ARGB_8888, true); new1Bitmap = (originalBitmap); } public void clear(){ isClear = true; new2Bitmap = (originalBitmap); invalidate(); } public void setstyle(float strokeWidth){ = strokeWidth; } @Override protected void onDraw(Canvas canvas) { (canvas); (HandWriting(new1Bitmap), 0, 0,null); } public Bitmap HandWriting(Bitmap originalBitmap) { Canvas canvas = null; if(isClear){ canvas = new Canvas(new2Bitmap); } else{ canvas = new Canvas(originalBitmap); } paint = new Paint(); (); (true); (color); (strokeWidth); if(isMove){ (startX, startY, clickX, clickY, paint); } startX = clickX; startY = clickY; if(isClear){ return new2Bitmap; } return originalBitmap; } @Override public boolean onTouchEvent(MotionEvent event) { clickX = (); clickY = (); if(() == MotionEvent.ACTION_DOWN){ isMove = false; invalidate(); return true; } else if(() == MotionEvent.ACTION_MOVE){ isMove = true; invalidate(); return true; } return (event); } }
Activity file:
public class HandWritingActivity extends Activity { /** Called when the activity is first created. */ private LinearLayout handWrite = null; private Button clear = null; int requestWidth=116; int requestHeight=173; int inSampleSize; @Override public void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_hand_writing); handWrite = (LinearLayout)findViewById(); clear = (Button)findViewById(); (new clearListener()); } private class clearListener implements OnClickListener{ public void onClick(View v) { // (); opts = new Options(); = true;// Let bimapfactory parse this bitmap and only obtain the border information of the bitmap (getResources(), , opts); if ( > requestHeight || > requestWidth) { if ( > ) { inSampleSize = ((float) / (float) requestHeight); } else { inSampleSize = ((float) / (float) requestWidth); } } ("width:" + ); ("high:" + ); = inSampleSize; (inSampleSize); = false;// Since the scaling ratio has been obtained, the bitmap factory gives way to the real parsing of this bitmap // Since we have parsed this input stream before, we need to re-initialize this input stream Bitmap b = (getResources(), , opts); HandWrite hw = new HandWrite(, b); (()); (hw); } } }
Integrated graffiti tool class:
/** *How to use: * 1. Create a TuYaView class instance * 2. Call the drawTuya method * 3. Parameter 1: context * 4. Parameter 2: byte[] byte array of image * 5. ImageView instance * 6. Brush definition * **/ import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; public class TuYaView { // Original picture private Bitmap mOrignBitmap; private Bitmap mEditBitmap; private int inSampleSize; private int requestWidth = 500; private int requestHeight = 700; /** Edit the picture canvas */ private Canvas mCanvas; private ImageView image; private Paint mPaint; public Bitmap drawTuya(Context context, byte[] _data, ImageView image, Paint mPaint) { = image; = mPaint; mOrignBitmap = (_data, 0, _data.length); return showEditBitmap(context, _data, image); } /** * Show edited pictures */ private Bitmap showEditBitmap(Context context, byte[] _data, ImageView image) { mOrignBitmap = getScaleBitmap(_data, image); if (mOrignBitmap == null) { (context, "Edit error"); } mEditBitmap = ((), true); mCanvas = new Canvas(mEditBitmap); (mOrignBitmap, new Matrix(), new Paint()); (mEditBitmap); (mTouchListener); return mEditBitmap; } /** * Get the result zoomed picture * * @return */ private Bitmap getScaleBitmap(byte[] _data, ImageView image) { opts = new Options(); = true;// Let bimapfactory parse this bitmap and only obtain the border information of the bitmap (_data, 0, _data.length, opts); if ( > requestHeight || > requestWidth) { if ( > ) { inSampleSize = ((float) / (float) requestHeight); } else { inSampleSize = ((float) / (float) requestWidth); } } = inSampleSize; = false;// Since the scaling ratio has been obtained, the bitmap factory gives way to the real parsing of this bitmap // Since we have parsed this input stream before, we need to re-initialize this input stream Bitmap bmp = BitmapFactory .decodeByteArray(_data, 0, _data.length, opts); return bmp; } // touch event private OnTouchListener mTouchListener = new OnTouchListener() { int startx = 0; int starty = 0; @Override public boolean onTouch(View v, MotionEvent event) { switch (()) { case MotionEvent.ACTION_DOWN:// The first time your finger touches the screen startx = (int) (); starty = (int) (); break; case MotionEvent.ACTION_MOVE: // Move your fingers in the imageview int x = (int) (); int y = (int) (); (startx, starty, x, y, mPaint); startx = (int) (); starty = (int) (); (); break; } return true; } }; }
I hope this article will be helpful to everyone's Android programming design.