Android Photoshop Palette App (ii) alphapatterndrawable of transparency drawing

Source: Internet
Author: User
Tags transparent image

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android Photoshop Palette App (ii) alphapatterndrawable of transparency drawing

Here's how to implement the transparency selection bar in the PS palette. First of all, the main points:

1. The transparency selection bar is actually based on the selection of color bands between white (0xFFFFFFFF) and Gray (0XFFCBCBCB), which allows us to select a translucent color

2. The application can not only do the transparency color selection, but also can realize the translucent image effect in the application.

Here's a look at the code, mainly based on the drawable rewrite:

[Java]View PlainCopy
  1. /*
  2. * Copyright (C) Daniel Nilsson
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * You are not a use this file except in compliance with the License.
  6. * Obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable or agreed to writing, software
  11. * Distributed under the License is distributed on a "as is" BASIS,
  12. * Without warranties or CONDITIONS of any KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * Limitations under the License.
  15. */
  16. Package net.margaritov.preference.colorpicker;
  17. Import Android.graphics.Bitmap;
  18. Import Android.graphics.Bitmap.Config;
  19. Import Android.graphics.Canvas;
  20. Import Android.graphics.ColorFilter;
  21. Import Android.graphics.Paint;
  22. Import Android.graphics.Rect;
  23. Import android.graphics.drawable.Drawable;
  24. /**
  25. * This drawable, draws a simple white and gray chessboard pattern.
  26. * It ' s pattern you'll often see as a background behind a
  27. * Partly transparent image in many applications.
  28. * @author Daniel Nilsson
  29. */
  30. Public class Alphapatterndrawable extends drawable {
  31. private int mrectanglesize = 10;
  32. private Paint Mpaint = new Paint ();
  33. private Paint Mpaintwhite = new Paint ();
  34. private Paint Mpaintgray = new Paint ();
  35. private int numrectangleshorizontal;
  36. private int numrectanglesvertical;
  37. /** 
  38. * Bitmap in which the pattern would be cahched.
  39. */
  40. private Bitmap Mbitmap;
  41. Public alphapatterndrawable (int rectanglesize) {
  42. Mrectanglesize = rectanglesize;
  43. Mpaintwhite.setcolor (0xFFFFFFFF);
  44. Mpaintgray.setcolor (0XFFCBCBCB);
  45. }
  46. @Override
  47. public void Draw (canvas canvas) {
  48. Canvas.drawbitmap (Mbitmap, null, getbounds (), mpaint);
  49. }
  50. @Override
  51. public int getopacity () {
  52. return 0;
  53. }
  54. @Override
  55. public void Setalpha (int alpha) {
  56. throw New Unsupportedoperationexception ("Alpha is not supported by this drawwable.");
  57. }
  58. @Override
  59. public void Setcolorfilter (Colorfilter CF) {
  60. throw New Unsupportedoperationexception ("Colorfilter is not supported by this drawwable.");
  61. }
  62. @Override
  63. protected void Onboundschange (Rect bounds) {
  64. Super.onboundschange (bounds);
  65. int height = bounds.height ();
  66. int width = bounds.width ();
  67. Numrectangleshorizontal = (int) Math.ceil ((width/mrectanglesize));
  68. numrectanglesvertical = (int) Math.ceil (height/mrectanglesize);
  69. Generatepatternbitmap ();
  70. }
  71. /** 
  72. * This would generate a bitmap with the pattern
  73. * As big as the rectangle we were allow to draw on.
  74. * We do this to chache the bitmap so We don ' t need to
  75. * Recreate it each time draw () is called since it
  76. * Takes a few milliseconds.
  77. */
  78. private void Generatepatternbitmap () {
  79. if (getbounds (). Width () <= 0 | | getbounds (). Height () <= 0) {
  80. return;
  81. }
  82. Mbitmap = Bitmap.createbitmap (GetBounds (). Width (), getbounds (). Height (), config.argb_8888);
  83. Canvas canvas = new Canvas (MBITMAP);
  84. Rect r = new Rect ();
  85. Boolean verticalstartwhite = true;
  86. For (int i = 0; I <= numrectanglesvertical; i++) {
  87. Boolean iswhite = Verticalstartwhite;
  88. For (int j = 0; J <= Numrectangleshorizontal; j + +) {
  89. R.top = i * mrectanglesize;
  90. R.left = J * mrectanglesize;
  91. R.bottom = R.top + mrectanglesize;
  92. R.right = R.left + mrectanglesize;
  93. Canvas.drawrect (R, Iswhite Mpaintwhite:mpaintgray);
  94. Iswhite =!iswhite;
  95. }
  96. Verticalstartwhite =!verticalstartwhite;
  97. }
  98. }
  99. }

Android Photoshop Palette App (ii) alphapatterndrawable of transparency drawing

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.