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

相关文章

C语言中位操作的实际应用举例

《C语言中位操作的实际应用举例》:本文主要介绍C语言中位操作的实际应用,总结了位操作的使用场景,并指出了需要注意的问题,如可读性、平台依赖性和溢出风险,文中通过代码介绍的非常详细,需要的朋友可以参... 目录1. 嵌入式系统与硬件寄存器操作2. 网络协议解析3. 图像处理与颜色编码4. 高效处理布尔标志集合

Android实现在线预览office文档的示例详解

《Android实现在线预览office文档的示例详解》在移动端展示在线Office文档(如Word、Excel、PPT)是一项常见需求,这篇文章为大家重点介绍了两种方案的实现方法,希望对大家有一定的... 目录一、项目概述二、相关技术知识三、实现思路3.1 方案一:WebView + Office Onl

Java中的Lambda表达式及其应用小结

《Java中的Lambda表达式及其应用小结》Java中的Lambda表达式是一项极具创新性的特性,它使得Java代码更加简洁和高效,尤其是在集合操作和并行处理方面,:本文主要介绍Java中的La... 目录前言1. 什么是Lambda表达式?2. Lambda表达式的基本语法例子1:最简单的Lambda表

Android实现两台手机屏幕共享和远程控制功能

《Android实现两台手机屏幕共享和远程控制功能》在远程协助、在线教学、技术支持等多种场景下,实时获得另一部移动设备的屏幕画面,并对其进行操作,具有极高的应用价值,本项目旨在实现两台Android手... 目录一、项目概述二、相关知识2.1 MediaProjection API2.2 Socket 网络

Android实现悬浮按钮功能

《Android实现悬浮按钮功能》在很多场景中,我们希望在应用或系统任意界面上都能看到一个小的“悬浮按钮”(FloatingButton),用来快速启动工具、展示未读信息或快捷操作,所以本文给大家介绍... 目录一、项目概述二、相关技术知识三、实现思路四、整合代码4.1 Java 代码(MainActivi

Python结合PyWebView库打造跨平台桌面应用

《Python结合PyWebView库打造跨平台桌面应用》随着Web技术的发展,将HTML/CSS/JavaScript与Python结合构建桌面应用成为可能,本文将系统讲解如何使用PyWebView... 目录一、技术原理与优势分析1.1 架构原理1.2 核心优势二、开发环境搭建2.1 安装依赖2.2 验

Java字符串操作技巧之语法、示例与应用场景分析

《Java字符串操作技巧之语法、示例与应用场景分析》在Java算法题和日常开发中,字符串处理是必备的核心技能,本文全面梳理Java中字符串的常用操作语法,结合代码示例、应用场景和避坑指南,可快速掌握字... 目录引言1. 基础操作1.1 创建字符串1.2 获取长度1.3 访问字符2. 字符串处理2.1 子字

Android Mainline基础简介

《AndroidMainline基础简介》AndroidMainline是通过模块化更新Android核心组件的框架,可能提高安全性,本文给大家介绍AndroidMainline基础简介,感兴趣的朋... 目录关键要点什么是 android Mainline?Android Mainline 的工作原理关键

如何解决idea的Module:‘:app‘platform‘android-32‘not found.问题

《如何解决idea的Module:‘:app‘platform‘android-32‘notfound.问题》:本文主要介绍如何解决idea的Module:‘:app‘platform‘andr... 目录idea的Module:‘:app‘pwww.chinasem.cnlatform‘android-32

SpringShell命令行之交互式Shell应用开发方式

《SpringShell命令行之交互式Shell应用开发方式》本文将深入探讨SpringShell的核心特性、实现方式及应用场景,帮助开发者掌握这一强大工具,具有很好的参考价值,希望对大家有所帮助,如... 目录引言一、Spring Shell概述二、创建命令类三、命令参数处理四、命令分组与帮助系统五、自定