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


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

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

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

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

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

    1. /* 
    2.  * Copyright (C) 2010 Daniel Nilsson 
    3.  * 
    4.  * Licensed under the Apache License, Version 2.0 (the "License"); 
    5.  * you may not use this file except in compliance with the License. 
    6.  * You may obtain a copy of the License at 
    7.  * 
    8.  *      http://www.apache.org/licenses/LICENSE-2.0 
    9.  * 
    10.  * Unless required by applicable law or agreed to in writing, software 
    11.  * distributed under the License is distributed on an "AS IS" BASIS, 
    12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
    13.  * See the License for the specific language governing permissions and 
    14.  * limitations under the License. 
    15.  */  
    16.   
    17. package net.margaritov.preference.colorpicker;  
    18.   
    19. import android.graphics.Bitmap;  
    20. import android.graphics.Bitmap.Config;  
    21. import android.graphics.Canvas;  
    22. import android.graphics.ColorFilter;  
    23. import android.graphics.Paint;  
    24. import android.graphics.Rect;  
    25. import android.graphics.drawable.Drawable;  
    26.   
    27. /** 
    28.  * This drawable that draws a simple white and gray chessboard pattern. 
    29.  * It's pattern you will often see as a background behind a 
    30.  * partly transparent image in many applications. 
    31.  * @author Daniel Nilsson 
    32.  */  
    33. public class AlphaPatternDrawable extends Drawable {  
    34.   
    35.     private int mRectangleSize = 10;  
    36.   
    37.     private Paint mPaint = new Paint();  
    38.     private Paint mPaintWhite = new Paint();  
    39.     private Paint mPaintGray = new Paint();  
    40.   
    41.     private int numRectanglesHorizontal;  
    42.     private int numRectanglesVertical;  
    43.   
    44.     /** 
    45.      * Bitmap in which the pattern will be cahched. 
    46.      */  
    47.     private Bitmap      mBitmap;  
    48.   
    49.     public AlphaPatternDrawable(int rectangleSize) {  
    50.         mRectangleSize = rectangleSize;  
    51.         mPaintWhite.setColor(0xffffffff);  
    52.         mPaintGray.setColor(0xffcbcbcb);  
    53.     }  
    54.   
    55.     @Override  
    56.     public void draw(Canvas canvas) {  
    57.         canvas.drawBitmap(mBitmap, null, getBounds(), mPaint);  
    58.     }  
    59.   
    60.     @Override  
    61.     public int getOpacity() {  
    62.         return 0;  
    63.     }  
    64.   
    65.     @Override  
    66.     public void setAlpha(int alpha) {  
    67.         throw new UnsupportedOperationException("Alpha is not supported by this drawwable.");  
    68.     }  
    69.   
    70.     @Override  
    71.     public void setColorFilter(ColorFilter cf) {  
    72.         throw new UnsupportedOperationException("ColorFilter is not supported by this drawwable.");  
    73.     }  
    74.   
    75.     @Override  
    76.     protected void onBoundsChange(Rect bounds) {  
    77.         super.onBoundsChange(bounds);  
    78.   
    79.         int height = bounds.height();  
    80.         int width = bounds.width();  
    81.   
    82.         numRectanglesHorizontal = (int) Math.ceil((width / mRectangleSize));  
    83.         numRectanglesVertical = (int) Math.ceil(height / mRectangleSize);  
    84.   
    85.         generatePatternBitmap();  
    86.   
    87.     }  
    88.   
    89.     /** 
    90.      * This will generate a bitmap with the pattern 
    91.      * as big as the rectangle we were allow to draw on. 
    92.      * We do this to chache the bitmap so we don't need to 
    93.      * recreate it each time draw() is called since it 
    94.      * takes a few milliseconds. 
    95.      */  
    96.     private void generatePatternBitmap(){  
    97.   
    98.         if(getBounds().width() <= 0 || getBounds().height() <= 0){  
    99.             return;  
    100.         }  
    101.           
    102.         mBitmap = Bitmap.createBitmap(getBounds().width(), getBounds().height(), Config.ARGB_8888);  
    103.         Canvas canvas = new Canvas(mBitmap);  
    104.   
    105.         Rect r = new Rect();  
    106.         boolean verticalStartWhite = true;  
    107.         for (int i = 0; i <= numRectanglesVertical; i++) {  
    108.   
    109.             boolean isWhite = verticalStartWhite;  
    110.             for (int j = 0; j <= numRectanglesHorizontal; j++) {  
    111.   
    112.                 r.top = i * mRectangleSize;  
    113.                 r.left = j * mRectangleSize;  
    114.                 r.bottom = r.top + mRectangleSize;  
    115.                 r.right = r.left + mRectangleSize;  
    116.   
    117.                 canvas.drawRect(r, isWhite ? mPaintWhite : mPaintGray);  
    118.   
    119.                 isWhite = !isWhite;  
    120.             }  
    121.   
    122.             verticalStartWhite = !verticalStartWhite;  
    123.   
    124.         }  
    125.   
    126.     }  
    127.   
  • 相关阅读:
    函数是什么?
    设置mac笔记本为固定ip
    JMeter-充值-生成随机数
    JMeter_方案上架,遇到的问题及解决
    做有态度的测试做
    JMeter-标的上架调整与完成
    上标-担保机构
    JMeter已传值但是提示为空
    JMeter上架标的(yyb-csg)
    JMeter中的正则表达式的匹配
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/5571866.html
Copyright © 2020-2023  润新知