Learn android together and add graffiti (text) to images (37 ),

Source: Internet
Author: User

Learn android together and add graffiti (text) to images (37 ),

Source image:


:



The Code is as follows:

Public class GraffitiView extends View {private Paint paint = null;/** source map */private Bitmap originalBitmap = null;/** images to be graffiti */private Bitmap new1Bitmap = null; /** before graffiti */private Bitmap new2Bitmap = null;/** X and Y coordinates of the touch */private float clickX = 0; private float clickY = 0; /** start point of each painting */private float startX = 0; private float startY = 0;/** whether to draw */private boolean isMove = true ;/* * Whether to clear */private boolean isClear = false;/** paint color */private int Color = color. GREEN;/** tip size */private float strokeWidth = 2.0f; public GraffitiView (Context context, AttributeSet attrs) {super (context, attrs); originalBitmap = BitmapFactory. decodeResource (getResources (), R. drawable. hh ). copy (Bitmap. config. ARGB_8888, true); new1Bitmap = Bitmap. createBitmap (originalBitmap);} @ Override pro Tected void onDraw (Canvas canvas) {super. onDraw (canvas); canvas. drawBitmap (HandWriting (new1Bitmap, isClear), 0, 0, null );} /***** graffiti pattern * @ param originalBitmap * @ param isClear whether to clear * @ return graffiti pattern */public Bitmap HandWriting (Bitmap originalBitmap, boolean isClear) {Canvas canvas = null; if (isClear) {// clear canvas = new Canvas (new2Bitmap);} else {// do not clear canvas = new Canvas (originalBitmap);} paint = New Paint (); paint. setStyle (Style. STROKE); paint. setAntiAlias (true); paint. setColor (color); paint. setStrokeWidth (strokeWidth); if (isMove) {// draw a canvas when moving. drawLine (startX, startY, clickX, clickY, paint);}/** start point of each painting */startX = clickX; startY = clickY; if (isClear) {// clear return new2Bitmap;} return originalBitmap;} @ Override public boolean onTouchEvent (MotionEvent event) {clickX = event. get X (); clickY = event. getY (); if (event. getAction () = MotionEvent. ACTION_DOWN) {/** refresh, but not draw, when pressing, but record the coordinate point when pressing, according to (isMove ). */IsMove = false; invalidate (); return true;} else if (event. getAction () = MotionEvent. ACTION_MOVE) {/** when moving, draw according to (isMove), starting from the coordinate point pressed. */IsMove = true; invalidate (); return true;} return super. onTouchEvent (event);}/*** clear */public void clear () {isClear = true; new2Bitmap = Bitmap. createBitmap (originalBitmap); invalidate ();}/*** set the width of the graffiti line * @ param strokeWidth */public void setstyle (float strokeWidth) {this. strokeWidth = strokeWidth;}/*** set the paint brush color * @ param color */public void setColor (int color) {this. color = color ;}}



Activity_main.xml:

<? 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"> <com. example. tuya. view. graffitiView android: id = "@ + id/handwriteview" android: layout_width = "fill_parent" android: layout_height = "500dp"/> <LinearLayout android: layout_width = "fill_parent" android: layout_height = "fill_parent" android: gravity = "center_horizontal" android: orientation = "horizontal"> <Button android: id = "@ + id/clear" android: layout_width = "200dp" android: layout_height = "wrap_content" android: text = "clear screen"/> </LinearLayout>



MainActivity:

public class MainActivity extends Activity {private GraffitiView handWrite = null;private Button clear = null;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);handWrite = (GraffitiView) findViewById(R.id.handwriteview);handWrite.setstyle(10);handWrite.setColor(Color.RED);clear = (Button) findViewById(R.id.clear);clear.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {handWrite.clear();}});}}



For more information, see http://blog.csdn.net/hai_qing_xu_kong/article/details/45620.93# _





Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.