Android plotting Skills Application Method Analysis

Source: Internet
Author: User

In Android operating system development, a basic operation is very common for graphic interface operations. We can first use the operations related to Android plotting described in this article to learn more about the operation skills of a single interface, so as to improve our awareness of this system.

 
 
  1. Import java. io. InputStream;
  2. Import android. app. Activity;
  3. Import android. content. Context;
  4. Import android. graphics. Bitmap;
  5. Import android. graphics. BitmapFactory;
  6. Import android. graphics. Canvas;
  7. Import android. graphics. Color;
  8. Import android. graphics. LinearGradient;
  9. Import android. graphics. Paint;
  10. Import android. graphics. PorterDuff;
  11. Import android. graphics. porterduduxfermode;
  12. Import android. graphics. Shader;
  13. Import android. graphics. Typeface;
  14. Import android. OS. Bundle;
  15. Import android. view. View;
  16. Public class AlphaBitmap extends Activity {
  17. @ Override
  18. Protected void onCreate (Bundle savedInstanceState ){
  19. Super. onCreate (savedInstanceState );
  20. SetContentView (new SampleView (this ));
  21. }
  22. Private static class SampleView extends View {
  23. Private Bitmap mBitmap;
  24. Private Bitmap mBitmap2;
  25. Private Bitmap mBitmap3;
  26. Private Shader mShader;
  27. Private static void drawIntoBitmap (Bitmap bm ){
  28. Float x = bm. getWidth ();
  29. Float y = bm. getHeight ();
  30. // Create a canvas with a specific object
  31. Canvas c = new Canvas (bm );
  32. Paint p = new Paint ();
  33. // It is mainly used to remove the edge. In effect, it seems that there is a margin if false.
  34. P. setAntiAlias (true );
  35. P. setAlpha (0x555555); // gray
  36. C. drawCircle (x/2, y/2, x/2, p );
  37. P. setAlpha (0x0000ff); // blue
  38. P. setXfermode (new porterduxfermode (PorterDuff. Mode. SRC_OVER ));
  39. // The following figure shows the font settings.
  40. P. setTextSize (20 );
  41. String familyName = "";
  42. Typeface font = Typeface. create (familyName, Typeface. ITALIC );
  43. P. setTypeface (font );
  44. P. setTextAlign (Paint. Align. CENTER );
  45. Paint. FontMetrics fm = p. getFontMetrics ();
  46. C. drawText ("I love android", x/2, (y-fm.ascent)/2, p );
  47. }
  48. Public SampleView (Context context ){
  49. Super (context );
  50. SetFocusable (true );
  51. // Several methods for obtaining images
  52. InputStream is = context. getResources ().
    OpenRawResource (R. drawable. app_sample_code );
  53. MBitmap = BitmapFactory. decodeStream (is );
  54. // Select a color value from an image and change it to a new image. Generally, the color of the paint brush is the same as that of the image below.
    P. setColor (Color. BLUE );
  55. MBitmapmBitmap2 = mBitmap. extractAlpha ();
  56. MBitmap3 = Bitmap. createBitmap (200,200, Bitmap. Config. ALPHA_8 );
  57. DrawIntoBitmap (mBitmap3 );
  58. // Gradient effect
  59. MShader = new LinearGradient (0, 0,150, 70, new int [] {
  60. Color. RED, Color. GREEN, Color. BLUE },
  61. Null, Shader. TileMode. MIRROR );
  62. }
  63. @ Override protected void onDraw (Canvas canvas ){
  64. Canvas. drawColor (Color. LTGRAY );
  65. Paint p = new Paint ();
  66. Float y = 10;
  67. P. setColor (Color. BLUE );
  68. Canvas. drawBitmap (mBitmap, 10, y, p );
  69. Y + = mBitmap. getHeight () + 10;
  70. Canvas. drawBitmap (mBitmap2, 10, y, p );
  71. Y + = mBitmap2.getHeight () + 10;
  72. P. setShader (mShader );
  73. Canvas. drawBitmap (mBitmap3, 10, y, p );
  74. }
  75. }
  76. }

If you change an image into a mutable image, you can mark it as your logo. This program is mainly used to draw an image by yourself. It can be used to draw an image, just like a blank sheet of white paper. At the same time, there is a function that can be used to extract the shape.

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.