1 /* 2 * Copyright (C) 2011 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.android.calendar; 18 19 import android.content.Context; 20 import android.graphics.BitmapFactory; 21 import android.graphics.Canvas; 22 import android.graphics.Paint; 23 import android.graphics.Paint.Style; 24 import android.graphics.PorterDuff; 25 import android.graphics.Shader.TileMode; 26 import android.graphics.drawable.BitmapDrawable; 27 import android.util.AttributeSet; 28 import android.view.View; 29 30 31 32 33 /** 34 * A custom view for a color chip for an event that can be drawn differently 35 * accroding to the event's status. 36 * 37 */ 38 public class ColorChipView extends View { 39 40 private static final String TAG = "ColorChipView"; 41 // Style of drawing 42 // Full rectangle for accepted events 43 // Border for tentative events 44 // Cross-hatched with 50% transparency for declined events 45 46 public static final int DRAW_FULL = 0; 47 public static final int DRAW_BORDER = 1; 48 public static final int DRAW_FADED = 2; 49 50 private static final float DECLINED_ALPHA = (float) 0.625; 51 private static final float DEFAULT_ALPHA = 1; 52 53 int mDrawStyle = DRAW_FULL; 54 55 private static final int DEF_BORDER_WIDTH = 4; 56 57 int mBorderWidth = DEF_BORDER_WIDTH; 58 59 int mColor; 60 ColorChipView(Context context)61 public ColorChipView(Context context) { 62 super(context); 63 } 64 ColorChipView(Context context, AttributeSet attrs)65 public ColorChipView(Context context, AttributeSet attrs) { 66 super(context, attrs); 67 } 68 setDrawStyle(int style)69 public void setDrawStyle(int style) { 70 if (style != DRAW_FULL && style != DRAW_BORDER && style != DRAW_FADED) { 71 return; 72 } 73 mDrawStyle = style; 74 invalidate(); 75 } 76 setBorderWidth(int width)77 public void setBorderWidth(int width) { 78 if (width >= 0) { 79 mBorderWidth = width; 80 invalidate(); 81 } 82 } 83 setColor(int color)84 public void setColor(int color) { 85 mColor = color; 86 invalidate(); 87 } 88 89 @Override onDraw(Canvas c)90 public void onDraw(Canvas c) { 91 92 int right = getWidth() - 1; 93 int bottom = getHeight() - 1; 94 Paint p = new Paint(); 95 p.setColor(mDrawStyle == DRAW_FADED ? Utils.getDeclinedColorFromColor(mColor) : mColor); 96 p.setStyle(Style.FILL_AND_STROKE); 97 98 switch (mDrawStyle) { 99 case DRAW_FADED: 100 case DRAW_FULL: 101 c.drawRect(0, 0, right, bottom, p); 102 break; 103 case DRAW_BORDER: 104 if (mBorderWidth <= 0) { 105 return; 106 } 107 int halfBorderWidth = mBorderWidth / 2; 108 int top = halfBorderWidth; 109 int left = halfBorderWidth; 110 p.setStrokeWidth(mBorderWidth); 111 112 float[] lines = new float[16]; 113 int ptr = 0; 114 lines [ptr++] = 0; 115 lines [ptr++] = top; 116 lines [ptr++] = right; 117 lines [ptr++] = top; 118 lines [ptr++] = 0; 119 lines [ptr++] = bottom - halfBorderWidth; 120 lines [ptr++] = right; 121 lines [ptr++] = bottom - halfBorderWidth; 122 lines [ptr++] = left; 123 lines [ptr++] = 0; 124 lines [ptr++] = left; 125 lines [ptr++] = bottom; 126 lines [ptr++] = right - halfBorderWidth; 127 lines [ptr++] = 0; 128 lines [ptr++] = right - halfBorderWidth; 129 lines [ptr++] = bottom; 130 c.drawLines(lines, p); 131 break; 132 } 133 } 134 } 135