PorterDuffColorFilter.java revision 417ee5ba89f7e0fe5efd34fce74bf1ee5923d976
1/* 2 * Copyright (C) 2006 The Android Open Source Project 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 17package android.graphics; 18 19import android.annotation.ColorInt; 20import android.annotation.NonNull; 21 22/** 23 * A color filter that can be used to tint the source pixels using a single 24 * color and a specific {@link PorterDuff Porter-Duff composite mode}. 25 */ 26public class PorterDuffColorFilter extends ColorFilter { 27 private int mColor; 28 private PorterDuff.Mode mMode; 29 30 /** 31 * Create a color filter that uses the specified color and Porter-Duff mode. 32 * 33 * @param color The ARGB source color used with the specified Porter-Duff mode 34 * @param mode The porter-duff mode that is applied 35 * 36 * @see Color 37 * @see #setColor(int) 38 * @see #setMode(android.graphics.PorterDuff.Mode) 39 */ 40 public PorterDuffColorFilter(@ColorInt int color, @NonNull PorterDuff.Mode mode) { 41 mColor = color; 42 mMode = mode; 43 update(); 44 } 45 46 /** 47 * Returns the ARGB color used to tint the source pixels when this filter 48 * is applied. 49 * 50 * @see Color 51 * @see #setColor(int) 52 * 53 * @hide 54 */ 55 public int getColor() { 56 return mColor; 57 } 58 59 /** 60 * Specifies the color to tint the source pixels with when this color 61 * filter is applied. 62 * 63 * @param color An ARGB {@link Color color} 64 * 65 * @see Color 66 * @see #getColor() 67 * @see #getMode() 68 * 69 * @hide 70 */ 71 public void setColor(int color) { 72 mColor = color; 73 update(); 74 } 75 76 /** 77 * Returns the Porter-Duff mode used to composite this color filter's 78 * color with the source pixel when this filter is applied. 79 * 80 * @see PorterDuff 81 * @see #setMode(android.graphics.PorterDuff.Mode) 82 * 83 * @hide 84 */ 85 public PorterDuff.Mode getMode() { 86 return mMode; 87 } 88 89 /** 90 * Specifies the Porter-Duff mode to use when compositing this color 91 * filter's color with the source pixel at draw time. 92 * 93 * @see PorterDuff 94 * @see #getMode() 95 * @see #getColor() 96 * 97 * @hide 98 */ 99 public void setMode(@NonNull PorterDuff.Mode mode) { 100 mMode = mode; 101 update(); 102 } 103 104 private void update() { 105 destroyFilter(native_instance); 106 native_instance = native_CreatePorterDuffFilter(mColor, mMode.nativeInt); 107 } 108 109 @Override 110 public boolean equals(Object object) { 111 if (this == object) { 112 return true; 113 } 114 if (object == null || getClass() != object.getClass()) { 115 return false; 116 } 117 final PorterDuffColorFilter other = (PorterDuffColorFilter) object; 118 if (mColor != other.mColor || mMode != other.mMode) { 119 return false; 120 } 121 return true; 122 } 123 124 @Override 125 public int hashCode() { 126 return 31 * mMode.hashCode() + mColor; 127 } 128 129 private static native long native_CreatePorterDuffFilter(int srcColor, int porterDuffMode); 130} 131