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