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

相关文章

Android 悬浮窗开发示例((动态权限请求 | 前台服务和通知 | 悬浮窗创建 )

《Android悬浮窗开发示例((动态权限请求|前台服务和通知|悬浮窗创建)》本文介绍了Android悬浮窗的实现效果,包括动态权限请求、前台服务和通知的使用,悬浮窗权限需要动态申请并引导... 目录一、悬浮窗 动态权限请求1、动态请求权限2、悬浮窗权限说明3、检查动态权限4、申请动态权限5、权限设置完毕后

Android里面的Service种类以及启动方式

《Android里面的Service种类以及启动方式》Android中的Service分为前台服务和后台服务,前台服务需要亮身份牌并显示通知,后台服务则有启动方式选择,包括startService和b... 目录一句话总结:一、Service 的两种类型:1. 前台服务(必须亮身份牌)2. 后台服务(偷偷干

Android kotlin语言实现删除文件的解决方案

《Androidkotlin语言实现删除文件的解决方案》:本文主要介绍Androidkotlin语言实现删除文件的解决方案,在项目开发过程中,尤其是需要跨平台协作的项目,那么删除用户指定的文件的... 目录一、前言二、适用环境三、模板内容1.权限申请2.Activity中的模板一、前言在项目开发过程中,尤

5分钟获取deepseek api并搭建简易问答应用

《5分钟获取deepseekapi并搭建简易问答应用》本文主要介绍了5分钟获取deepseekapi并搭建简易问答应用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需... 目录1、获取api2、获取base_url和chat_model3、配置模型参数方法一:终端中临时将加

JavaScript中的isTrusted属性及其应用场景详解

《JavaScript中的isTrusted属性及其应用场景详解》在现代Web开发中,JavaScript是构建交互式应用的核心语言,随着前端技术的不断发展,开发者需要处理越来越多的复杂场景,例如事件... 目录引言一、问题背景二、isTrusted 属性的来源与作用1. isTrusted 的定义2. 为

Python调用另一个py文件并传递参数常见的方法及其应用场景

《Python调用另一个py文件并传递参数常见的方法及其应用场景》:本文主要介绍在Python中调用另一个py文件并传递参数的几种常见方法,包括使用import语句、exec函数、subproce... 目录前言1. 使用import语句1.1 基本用法1.2 导入特定函数1.3 处理文件路径2. 使用ex

将Python应用部署到生产环境的小技巧分享

《将Python应用部署到生产环境的小技巧分享》文章主要讲述了在将Python应用程序部署到生产环境之前,需要进行的准备工作和最佳实践,包括心态调整、代码审查、测试覆盖率提升、配置文件优化、日志记录完... 目录部署前夜:从开发到生产的心理准备与检查清单环境搭建:打造稳固的应用运行平台自动化流水线:让部署像

Linux中Curl参数详解实践应用

《Linux中Curl参数详解实践应用》在现代网络开发和运维工作中,curl命令是一个不可或缺的工具,它是一个利用URL语法在命令行下工作的文件传输工具,支持多种协议,如HTTP、HTTPS、FTP等... 目录引言一、基础请求参数1. -X 或 --request2. -d 或 --data3. -H 或

使用Python绘制蛇年春节祝福艺术图

《使用Python绘制蛇年春节祝福艺术图》:本文主要介绍如何使用Python的Matplotlib库绘制一幅富有创意的“蛇年有福”艺术图,这幅图结合了数字,蛇形,花朵等装饰,需要的可以参考下... 目录1. 绘图的基本概念2. 准备工作3. 实现代码解析3.1 设置绘图画布3.2 绘制数字“2025”3.3

在Ubuntu上部署SpringBoot应用的操作步骤

《在Ubuntu上部署SpringBoot应用的操作步骤》随着云计算和容器化技术的普及,Linux服务器已成为部署Web应用程序的主流平台之一,Java作为一种跨平台的编程语言,具有广泛的应用场景,本... 目录一、部署准备二、安装 Java 环境1. 安装 JDK2. 验证 Java 安装三、安装 mys