首页 技术 正文
技术 2022年11月17日
0 收藏 435 点赞 4,759 浏览 3399 个字

版权声明:本文为博主原创文章,未经博主允许不得转载。

Android 仿PhotoShop调色板应用(二) 透明度绘制之AlphaPatternDrawable

这里讲一下如何实现PS调色板中的透明度选择条.首先说一下要点:

1. 透明度选择条实际上是基于白色(0xffffffff)和灰色(0xffcbcbcb)之间的颜色区间选取, 由此我们可以实现一个半透明颜色的选取

2.该应用不仅可以做透明度颜色选取,也可以在应用中实现半透明的图像效果

下面看一下代码,主要是基于Drawable的重写:

  1. /*
  2. * Copyright (C) 2010 Daniel Nilsson
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the “License”);
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. *      http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an “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 that draws a simple white and gray chessboard pattern.
  26. * It’s pattern you will 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 will 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 will 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. }
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,083
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,558
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,407
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,180
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,817
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,899