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

相关文章

分布式锁在Spring Boot应用中的实现过程

《分布式锁在SpringBoot应用中的实现过程》文章介绍在SpringBoot中通过自定义Lock注解、LockAspect切面和RedisLockUtils工具类实现分布式锁,确保多实例并发操作... 目录Lock注解LockASPect切面RedisLockUtils工具类总结在现代微服务架构中,分布

Python标准库之数据压缩和存档的应用详解

《Python标准库之数据压缩和存档的应用详解》在数据处理与存储领域,压缩和存档是提升效率的关键技术,Python标准库提供了一套完整的工具链,下面小编就来和大家简单介绍一下吧... 目录一、核心模块架构与设计哲学二、关键模块深度解析1.tarfile:专业级归档工具2.zipfile:跨平台归档首选3.

使用IDEA部署Docker应用指南分享

《使用IDEA部署Docker应用指南分享》本文介绍了使用IDEA部署Docker应用的四步流程:创建Dockerfile、配置IDEADocker连接、设置运行调试环境、构建运行镜像,并强调需准备本... 目录一、创建 dockerfile 配置文件二、配置 IDEA 的 Docker 连接三、配置 Do

Android Paging 分页加载库使用实践

《AndroidPaging分页加载库使用实践》AndroidPaging库是Jetpack组件的一部分,它提供了一套完整的解决方案来处理大型数据集的分页加载,本文将深入探讨Paging库... 目录前言一、Paging 库概述二、Paging 3 核心组件1. PagingSource2. Pager3.

深入浅出SpringBoot WebSocket构建实时应用全面指南

《深入浅出SpringBootWebSocket构建实时应用全面指南》WebSocket是一种在单个TCP连接上进行全双工通信的协议,这篇文章主要为大家详细介绍了SpringBoot如何集成WebS... 目录前言为什么需要 WebSocketWebSocket 是什么Spring Boot 如何简化 We

Java Stream流之GroupBy的用法及应用场景

《JavaStream流之GroupBy的用法及应用场景》本教程将详细介绍如何在Java中使用Stream流的groupby方法,包括基本用法和一些常见的实际应用场景,感兴趣的朋友一起看看吧... 目录Java Stream流之GroupBy的用法1. 前言2. 基础概念什么是 GroupBy?Stream

python中列表应用和扩展性实用详解

《python中列表应用和扩展性实用详解》文章介绍了Python列表的核心特性:有序数据集合,用[]定义,元素类型可不同,支持迭代、循环、切片,可执行增删改查、排序、推导式及嵌套操作,是常用的数据处理... 目录1、列表定义2、格式3、列表是可迭代对象4、列表的常见操作总结1、列表定义是处理一组有序项目的

C#中的Converter的具体应用

《C#中的Converter的具体应用》C#中的Converter提供了一种灵活的类型转换机制,本文详细介绍了Converter的基本概念、使用场景,具有一定的参考价值,感兴趣的可以了解一下... 目录Converter的基本概念1. Converter委托2. 使用场景布尔型转换示例示例1:简单的字符串到

Spring Boot Actuator应用监控与管理的详细步骤

《SpringBootActuator应用监控与管理的详细步骤》SpringBootActuator是SpringBoot的监控工具,提供健康检查、性能指标、日志管理等核心功能,支持自定义和扩展端... 目录一、 Spring Boot Actuator 概述二、 集成 Spring Boot Actuat

PyTorch中的词嵌入层(nn.Embedding)详解与实战应用示例

《PyTorch中的词嵌入层(nn.Embedding)详解与实战应用示例》词嵌入解决NLP维度灾难,捕捉语义关系,PyTorch的nn.Embedding模块提供灵活实现,支持参数配置、预训练及变长... 目录一、词嵌入(Word Embedding)简介为什么需要词嵌入?二、PyTorch中的nn.Em