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