1 /* 2 * Copyright (C) 2009 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 17 package com.example.android.apis.graphics; 18 19 import com.example.android.apis.R; 20 21 import android.app.Activity; 22 import android.content.Context; 23 import android.graphics.*; 24 import android.graphics.drawable.*; 25 import android.os.Bundle; 26 import android.view.*; 27 28 public class ColorFilters extends GraphicsActivity { 29 30 @Override 31 protected void onCreate(Bundle savedInstanceState) { 32 super.onCreate(savedInstanceState); 33 setContentView(new SampleView(this)); 34 35 } 36 37 private static class SampleView extends View { 38 private Activity mActivity; 39 private Drawable mDrawable; 40 private Drawable[] mDrawables; 41 private Paint mPaint; 42 private Paint mPaint2; 43 private float mPaintTextOffset; 44 private int[] mColors; 45 private PorterDuff.Mode[] mModes; 46 private int mModeIndex; 47 48 private static void addToTheRight(Drawable curr, Drawable prev) { 49 Rect r = prev.getBounds(); 50 int x = r.right + 12; 51 int center = (r.top + r.bottom) >> 1; 52 int h = curr.getIntrinsicHeight(); 53 int y = center - (h >> 1); 54 55 curr.setBounds(x, y, x + curr.getIntrinsicWidth(), y + h); 56 } 57 58 public SampleView(Activity activity) { 59 super(activity); 60 mActivity = activity; 61 Context context = activity; 62 setFocusable(true); 63 64 mDrawable = context.getResources().getDrawable(R.drawable.btn_default_normal); 65 mDrawable.setBounds(0, 0, 150, 48); 66 mDrawable.setDither(true); 67 68 int[] resIDs = new int[] { 69 R.drawable.btn_circle_normal, 70 R.drawable.btn_check_off, 71 R.drawable.btn_check_on 72 }; 73 mDrawables = new Drawable[resIDs.length]; 74 Drawable prev = mDrawable; 75 for (int i = 0; i < resIDs.length; i++) { 76 mDrawables[i] = context.getResources().getDrawable(resIDs[i]); 77 mDrawables[i].setDither(true); 78 addToTheRight(mDrawables[i], prev); 79 prev = mDrawables[i]; 80 } 81 82 mPaint = new Paint(); 83 mPaint.setAntiAlias(true); 84 mPaint.setTextSize(16); 85 mPaint.setTextAlign(Paint.Align.CENTER); 86 87 mPaint2 = new Paint(mPaint); 88 mPaint2.setAlpha(64); 89 90 Paint.FontMetrics fm = mPaint.getFontMetrics(); 91 mPaintTextOffset = (fm.descent + fm.ascent) * 0.5f; 92 93 mColors = new int[] { 94 0, 95 0xCC0000FF, 96 0x880000FF, 97 0x440000FF, 98 0xFFCCCCFF, 99 0xFF8888FF, 100 0xFF4444FF, 101 }; 102 103 mModes = new PorterDuff.Mode[] { 104 PorterDuff.Mode.SRC_ATOP, 105 PorterDuff.Mode.MULTIPLY, 106 }; 107 mModeIndex = 0; 108 109 updateTitle(); 110 } 111 112 private void swapPaintColors() { 113 if (mPaint.getColor() == 0xFF000000) { 114 mPaint.setColor(0xFFFFFFFF); 115 mPaint2.setColor(0xFF000000); 116 } else { 117 mPaint.setColor(0xFF000000); 118 mPaint2.setColor(0xFFFFFFFF); 119 } 120 mPaint2.setAlpha(64); 121 } 122 123 private void updateTitle() { 124 mActivity.setTitle(mModes[mModeIndex].toString()); 125 } 126 127 private void drawSample(Canvas canvas, ColorFilter filter) { 128 Rect r = mDrawable.getBounds(); 129 float x = (r.left + r.right) * 0.5f; 130 float y = (r.top + r.bottom) * 0.5f - mPaintTextOffset; 131 132 mDrawable.setColorFilter(filter); 133 mDrawable.draw(canvas); 134 canvas.drawText("Label", x+1, y+1, mPaint2); 135 canvas.drawText("Label", x, y, mPaint); 136 137 for (Drawable dr : mDrawables) { 138 dr.setColorFilter(filter); 139 dr.draw(canvas); 140 } 141 } 142 143 @Override protected void onDraw(Canvas canvas) { 144 canvas.drawColor(0xFFCCCCCC); 145 146 canvas.translate(8, 12); 147 for (int color : mColors) { 148 ColorFilter filter; 149 if (color == 0) { 150 filter = null; 151 } else { 152 filter = new PorterDuffColorFilter(color, 153 mModes[mModeIndex]); 154 } 155 drawSample(canvas, filter); 156 canvas.translate(0, 55); 157 } 158 } 159 160 @Override 161 public boolean onTouchEvent(MotionEvent event) { 162 switch (event.getAction()) { 163 case MotionEvent.ACTION_DOWN: 164 break; 165 case MotionEvent.ACTION_MOVE: 166 break; 167 case MotionEvent.ACTION_UP: 168 // update mode every other time we change paint colors 169 if (mPaint.getColor() == 0xFFFFFFFF) { 170 mModeIndex = (mModeIndex + 1) % mModes.length; 171 updateTitle(); 172 } 173 swapPaintColors(); 174 invalidate(); 175 break; 176 } 177 return true; 178 } 179 } 180 } 181