Android 仿PhotoShop调色板应用(二) 透明度绘制之AlphaPatternDrawable
2023-09-11 14:17:58 时间
版权声明:本文为博主原创文章,未经博主允许不得转载。
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);
- }
- @Override
- public void draw(Canvas canvas) {
- canvas.drawBitmap(mBitmap, null, getBounds(), mPaint);
- }
- @Override
- public int getOpacity() {
- return 0;
- }
- @Override
- public void setAlpha(int alpha) {
- throw new UnsupportedOperationException("Alpha is not supported by this drawwable.");
- }
- @Override
- public void setColorFilter(ColorFilter cf) {
- throw new UnsupportedOperationException("ColorFilter is not supported by this drawwable.");
- }
- @Override
- protected 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开发之解决APP启动白屏或者黑屏闪现的问题
- eclipse,android keystore
- [Android Pro] Android学习——在线查看android源代码的3种方式
- [Android Pro] ant 编译android工程
- Android教程-android studio 制作.9 图片
- 浅论Android网络请求库——android-async-http
- Failed to fetch URl https://dl-ssl.google.com/android/repository/addo Android SDK更新以及ADT更新出现问题的解决办法
- Android之shape与selector实现圆角
- Android开发指南(41) —— Searchable Configuration
- Android stuido 解决Caused by: android.view.InflateException: Binary XML file line #8: Binary XML file
- Android Studio / IDEA 中.android 文件夹移动出默认位置
- android 证书验证流程分析_Android签名机制之---签名验证过程详解
- Android 9.0 10.0 去掉开机提示 android is starting 的对话框
- Android 9.0 10.0代码设置系统默认语言为中文
- Android android studio 项目放在源码中编译
- 【Android 逆向】使用 Python 解析 ELF 文件 ( Capstone 反汇编框架 | PyCharm 中导入 Capstone 反汇编框架 )
- Android培训班(45)
- Android抽屉(SlidingDrawer --类似android通知栏下拉效果)
- Android Studio 理解com.android.support:appcompat-v7:x.x.x依赖包(建议收藏)
- Android Device Monitor中File Explorer的db文件如何导出到桌面
- Android 7.0 SEAndroid app权限配置