Android getting started tutorial (25) -- Drawing)

Source: Internet
Author: User
Tags drawtext

This article comes fromHttp://blog.csdn.net/hellogv/, The reference must indicate the source!

There are a lot of commonly used Widgets. Now I want to talk about the commonly used widgets in mobile phone development. To understand how to draw images for Android, you must first understand the basic graphic interfaces used:

1. bitmap, which can be from resources/files or inProgramActually, the function is equivalent to the storage space of images;

2. Canvas is closely related to bitmap. If bitmap is used as a metaphor, canvas is a platform that provides many methods to operate bitamp;

3. Paint, which is closely related to canvas. It is a paint tool on the "canvas" and is also used to set the style on the View control;

4. drawable: if the first three pictures are invisible in the memory, drawable is the interface that shows the drawing results of the first three. Drawable has multiple child classes, such as bitmapdrawable, shapedrawable, and layerdrawable.

 

This article mainly explains how to draw images in the imageview and how to draw custom images directly on the button (inheriting the view control.

Directly draw resource Images

 

 

Draw pictures and draw words on imageview

 

 

Draw a picture directly on the control background

 

Source code of Main. xml:

View plain Copy to clipboard Print ?
  1. <?XML Version="1.0" Encoding="UTF-8"?>
  2. <Linearlayout Xmlns: Android=Http://schemas.android.com/apk/res/android"
  3. Android: Orientation="Vertical"
  4. Android: layout_width="Fill_parent"
  5. Android: layout_height="Fill_parent"
  6. >
  7. button Android: ID = "@ + ID/button01" Android: layout_width = "fill_parent" Android: layout_height = "44px" Android: text = "display resource images" > button >
  8. button Android: ID = "@ + ID/button02" Android: layout_width = "fill_parent" Android: layout_height = "44px" Android: text = "display and draw resource images" > button >
  9. button Android: ID = "@ + ID/button03" Android: layout_height = "44px" Android: layout_width = "fill_parent" Android: text = "drawing on Control" > button >
  10. <Imageview Android: ID="@ + ID/imageview01" Android: layout_width="Wrap_content" Android: layout_height="Wrap_content"></Imageview>
  11. </Linearlayout>

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: Orientation = "vertical" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" <br/> <button Android: Id = "@ + ID/button01" Android: layout_width = "fill_parent" Android: layout_height = "44px" Android: text = "show resource image"> </button> <br/> <button Android: id = "@ + ID/button02" Android: layout_width = "fill_parent" Android: layout_height = "44px" Android: TEXT = "show and draw resource images"> </button> <br/> <button Android: Id = "@ + ID/button03" Android: layout_height = "44px" Android: layout_width = "fill_parent" Android: text = ""> </button> <br/> <imageview Android: Id = "@ + ID/imageview01" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"> </imageview> </P> <p> </linearlayout> <br/>

 

 

Program source code:

 

View plain Copy to clipboard Print ?
  1. PackageCom. testdraw;
  2. ImportAndroid. App. activity;
  3. ImportAndroid. content. res. Resources;
  4. ImportAndroid. Graphics. Bitmap;
  5. ImportAndroid. Graphics. bitmap. config;
  6. ImportAndroid. Graphics. bitmapfactory;
  7. ImportAndroid. Graphics. Canvas;
  8. ImportAndroid. Graphics. color;
  9. ImportAndroid. Graphics. paint;
  10. ImportAndroid. Graphics. typeface;
  11. ImportAndroid. Graphics. drawable. bitmapdrawable;
  12. ImportAndroid. Graphics. drawable. drawable;
  13. ImportAndroid. OS. Bundle;
  14. ImportAndroid. View. view;
  15. ImportAndroid. widget. Button;
  16. ImportAndroid. widget. imageview;
  17. Public ClassTestdrawExtendsActivity {
  18. Imageview IV;
  19. Button btn1, btn2, btn3, btn4;
  20. Resources R;
  21. @ Override
  22. Public VoidOncreate (bundle savedinstancestate ){
  23. Super. Oncreate (savedinstancestate );
  24. Setcontentview (R. layout. Main );
  25. IV = (imageview)This. Findviewbyid (R. Id. imageview01 );
  26. Btn1 = (button)This. Findviewbyid (R. Id. button01 );
  27. Btn2 = (button)This. Findviewbyid (R. Id. button02 );
  28. Btn3 = (button)This. Findviewbyid (R. Id. button03 );
  29. Btn1.setonclicklistener (NewClickevent ());
  30. Btn2.setonclicklistener (NewClickevent ());
  31. Btn3.setonclicklistener (NewClickevent ());
  32. R =This. Getresources ();
  33. }
  34. ClassClickeventImplementsView. onclicklistener {
  35. Public VoidOnclick (view v ){
  36. If(V = btn1)// Display resource Images
  37. {// Function equivalent
  38. // IV. setbackgroundresource (R. drawable. Icon); // open the resource Image
  39. Bitmap BMP = bitmapfactory. decoderesource (R, R. drawable. Icon );// Open the resource Image
  40. Iv. setimagebitmap (BMP );
  41. }
  42. Else If(V = btn2)// Display and draw resource Images
  43. {
  44. Bitmap BMP = bitmapfactory. decoderesource (R, R. drawable. Icon );// Read-only, cannot be directly painted on BMP
  45. Bitmap newb = bitmap. createbitmap (300,300, Config. argb_8888 );
  46. Canvas canvastemp =NewCanvas (NEWB );
  47. Canvastemp. drawcolor (color. Transparent );
  48. Paint P =NewPaint ();
  49. String familyname ="";
  50. Typeface font = typeface. Create (familyname, typeface. Bold );
  51. P. setcolor (color. Red );
  52. P. settypeface (font );
  53. P. settextsize (22);
  54. Canvastemp. drawtext ("Writing... ",50,50, P );
  55. Canvastemp. drawbitmap (BMP,50,50, P );// Draw
  56. Iv. setimagebitmap (NEWB );
  57. }
  58. Else If(V = btn3)// Draw the image directly on the button
  59. {
  60. Bitmap newb = bitmap. createbitmap (btn3.getwidth (), btn3.getheight (), config. argb_8888 );
  61. Canvas canvastemp =NewCanvas (NEWB );
  62. Canvastemp. drawcolor (color. White );
  63. Paint P =NewPaint ();
  64. String familyname ="";
  65. Typeface font = typeface. Create (familyname, typeface. Bold );
  66. P. setcolor (color. Red );
  67. P. settypeface (font );
  68. P. settextsize (20);
  69. Canvastemp. drawtext ("Writing... ",30,30, P );
  70. Drawable =NewBitmapdrawable (NEWB );
  71. Btn3.setbackgrounddrawable (drawable );
  72. }
  73. }
  74. }
  75. }

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.