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

本文主要是介绍Android 仿PhotoShop调色板应用(二) 透明度绘制之AlphaPatternDrawable,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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

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

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

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


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

/** Copyright (C) 2010 Daniel Nilsson** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at**      http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/package net.margaritov.preference.colorpicker;import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;/*** This drawable that draws a simple white and gray chessboard pattern.* It's pattern you will often see as a background behind a* partly transparent image in many applications.* @author Daniel Nilsson*/
public class AlphaPatternDrawable extends Drawable {private int mRectangleSize = 10;private Paint mPaint = new Paint();private Paint mPaintWhite = new Paint();private Paint mPaintGray = new Paint();private int numRectanglesHorizontal;private int numRectanglesVertical;/*** Bitmap in which the pattern will be cahched.*/private Bitmap		mBitmap;public AlphaPatternDrawable(int rectangleSize) {mRectangleSize = rectangleSize;mPaintWhite.setColor(0xffffffff);mPaintGray.setColor(0xffcbcbcb);}@Overridepublic void draw(Canvas canvas) {canvas.drawBitmap(mBitmap, null, getBounds(), mPaint);}@Overridepublic int getOpacity() {return 0;}@Overridepublic void setAlpha(int alpha) {throw new UnsupportedOperationException("Alpha is not supported by this drawwable.");}@Overridepublic void setColorFilter(ColorFilter cf) {throw new UnsupportedOperationException("ColorFilter is not supported by this drawwable.");}@Overrideprotected void onBoundsChange(Rect bounds) {super.onBoundsChange(bounds);int height = bounds.height();int width = bounds.width();numRectanglesHorizontal = (int) Math.ceil((width / mRectangleSize));numRectanglesVertical = (int) Math.ceil(height / mRectangleSize);generatePatternBitmap();}/*** This will generate a bitmap with the pattern* as big as the rectangle we were allow to draw on.* We do this to chache the bitmap so we don't need to* recreate it each time draw() is called since it* takes a few milliseconds.*/private void generatePatternBitmap(){if(getBounds().width() <= 0 || getBounds().height() <= 0){return;}mBitmap = Bitmap.createBitmap(getBounds().width(), getBounds().height(), Config.ARGB_8888);Canvas canvas = new Canvas(mBitmap);Rect r = new Rect();boolean verticalStartWhite = true;for (int i = 0; i <= numRectanglesVertical; i++) {boolean isWhite = verticalStartWhite;for (int j = 0; j <= numRectanglesHorizontal; j++) {r.top = i * mRectangleSize;r.left = j * mRectangleSize;r.bottom = r.top + mRectangleSize;r.right = r.left + mRectangleSize;canvas.drawRect(r, isWhite ? mPaintWhite : mPaintGray);isWhite = !isWhite;}verticalStartWhite = !verticalStartWhite;}}}


这篇关于Android 仿PhotoShop调色板应用(二) 透明度绘制之AlphaPatternDrawable的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/792777

相关文章

nginx -t、nginx -s stop 和 nginx -s reload 命令的详细解析(结合应用场景)

《nginx-t、nginx-sstop和nginx-sreload命令的详细解析(结合应用场景)》本文解析Nginx的-t、-sstop、-sreload命令,分别用于配置语法检... 以下是关于 nginx -t、nginx -s stop 和 nginx -s reload 命令的详细解析,结合实际应

Android kotlin中 Channel 和 Flow 的区别和选择使用场景分析

《Androidkotlin中Channel和Flow的区别和选择使用场景分析》Kotlin协程中,Flow是冷数据流,按需触发,适合响应式数据处理;Channel是热数据流,持续发送,支持... 目录一、基本概念界定FlowChannel二、核心特性对比数据生产触发条件生产与消费的关系背压处理机制生命周期

Android ClassLoader加载机制详解

《AndroidClassLoader加载机制详解》Android的ClassLoader负责加载.dex文件,基于双亲委派模型,支持热修复和插件化,需注意类冲突、内存泄漏和兼容性问题,本文给大家介... 目录一、ClassLoader概述1.1 类加载的基本概念1.2 android与Java Class

PostgreSQL的扩展dict_int应用案例解析

《PostgreSQL的扩展dict_int应用案例解析》dict_int扩展为PostgreSQL提供了专业的整数文本处理能力,特别适合需要精确处理数字内容的搜索场景,本文给大家介绍PostgreS... 目录PostgreSQL的扩展dict_int一、扩展概述二、核心功能三、安装与启用四、字典配置方法

Python中re模块结合正则表达式的实际应用案例

《Python中re模块结合正则表达式的实际应用案例》Python中的re模块是用于处理正则表达式的强大工具,正则表达式是一种用来匹配字符串的模式,它可以在文本中搜索和匹配特定的字符串模式,这篇文章主... 目录前言re模块常用函数一、查看文本中是否包含 A 或 B 字符串二、替换多个关键词为统一格式三、提

Java MQTT实战应用

《JavaMQTT实战应用》本文详解MQTT协议,涵盖其发布/订阅机制、低功耗高效特性、三种服务质量等级(QoS0/1/2),以及客户端、代理、主题的核心概念,最后提供Linux部署教程、Sprin... 目录一、MQTT协议二、MQTT优点三、三种服务质量等级四、客户端、代理、主题1. 客户端(Clien

使用Python绘制3D堆叠条形图全解析

《使用Python绘制3D堆叠条形图全解析》在数据可视化的工具箱里,3D图表总能带来眼前一亮的效果,本文就来和大家聊聊如何使用Python实现绘制3D堆叠条形图,感兴趣的小伙伴可以了解下... 目录为什么选择 3D 堆叠条形图代码实现:从数据到 3D 世界的搭建核心代码逐行解析细节优化应用场景:3D 堆叠图

Android DataBinding 与 MVVM使用详解

《AndroidDataBinding与MVVM使用详解》本文介绍AndroidDataBinding库,其通过绑定UI组件与数据源实现自动更新,支持双向绑定和逻辑运算,减少模板代码,结合MV... 目录一、DataBinding 核心概念二、配置与基础使用1. 启用 DataBinding 2. 基础布局

Android ViewBinding使用流程

《AndroidViewBinding使用流程》AndroidViewBinding是Jetpack组件,替代findViewById,提供类型安全、空安全和编译时检查,代码简洁且性能优化,相比Da... 目录一、核心概念二、ViewBinding优点三、使用流程1. 启用 ViewBinding (模块级

CSS中的Static、Relative、Absolute、Fixed、Sticky的应用与详细对比

《CSS中的Static、Relative、Absolute、Fixed、Sticky的应用与详细对比》CSS中的position属性用于控制元素的定位方式,不同的定位方式会影响元素在页面中的布... css 中的 position 属性用于控制元素的定位方式,不同的定位方式会影响元素在页面中的布局和层叠关