Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android Photoshop Palette App (ii) alphapatterndrawable of transparency drawing
Here's how to implement the transparency selection bar in the PS palette. First of all, the main points:
1. The transparency selection bar is actually based on the selection of color bands between white (0xFFFFFFFF) and Gray (0XFFCBCBCB), which allows us to select a translucent color
2. The application can not only do the transparency color selection, but also can realize the translucent image effect in the application.
Here's a look at the code, mainly based on the drawable rewrite:
[Java]View PlainCopy
- /*
- * Copyright (C) Daniel Nilsson
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * You are not a use this file except in compliance with the License.
- * Obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable or agreed to writing, software
- * Distributed under the License is distributed on a "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, draws a simple white and gray chessboard pattern.
- * It ' s pattern you'll 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 would 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 would 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 Palette App (ii) alphapatterndrawable of transparency drawing