mariagpuyol | acef32a | 2016-12-12 11:33:20 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | package com.android.emergency; |
| 17 | |
| 18 | import android.content.Context; |
| 19 | import android.content.res.Resources; |
| 20 | import android.graphics.Bitmap; |
| 21 | import android.graphics.Canvas; |
| 22 | import android.graphics.Color; |
| 23 | import android.graphics.ColorFilter; |
| 24 | import android.graphics.Paint; |
| 25 | import android.graphics.Path; |
| 26 | import android.graphics.PixelFormat; |
| 27 | import android.graphics.PorterDuff; |
| 28 | import android.graphics.PorterDuffXfermode; |
| 29 | import android.graphics.Rect; |
| 30 | import android.graphics.RectF; |
| 31 | import android.graphics.drawable.Drawable; |
| 32 | |
| 33 | /** |
| 34 | * Converts the user avatar icon to a circularly clipped one. |
| 35 | * TODO: Use CircleFramedDrawable from Settings once it's moved to frameworks. |
| 36 | */ |
| 37 | public class CircleFramedDrawable extends Drawable { |
| 38 | |
| 39 | private final Bitmap mBitmap; |
| 40 | private final int mSize; |
| 41 | private final Paint mPaint; |
| 42 | |
| 43 | private float mScale; |
| 44 | private Rect mSrcRect; |
| 45 | private RectF mDstRect; |
| 46 | |
| 47 | public static CircleFramedDrawable getInstance(Context context, Bitmap icon) { |
| 48 | Resources res = context.getResources(); |
| 49 | float iconSize = res.getDimension(R.dimen.circle_avatar_size); |
| 50 | |
| 51 | CircleFramedDrawable instance = new CircleFramedDrawable(icon, (int) iconSize); |
| 52 | return instance; |
| 53 | } |
| 54 | |
| 55 | public CircleFramedDrawable(Bitmap icon, int size) { |
| 56 | super(); |
| 57 | mSize = size; |
| 58 | |
| 59 | mBitmap = Bitmap.createBitmap(mSize, mSize, Bitmap.Config.ARGB_8888); |
| 60 | final Canvas canvas = new Canvas(mBitmap); |
| 61 | |
| 62 | final int width = icon.getWidth(); |
| 63 | final int height = icon.getHeight(); |
| 64 | final int square = Math.min(width, height); |
| 65 | |
| 66 | final Rect cropRect = new Rect((width - square) / 2, (height - square) / 2, square, square); |
| 67 | final RectF circleRect = new RectF(0f, 0f, mSize, mSize); |
| 68 | |
| 69 | final Path fillPath = new Path(); |
| 70 | fillPath.addArc(circleRect, 0f, 360f); |
| 71 | |
| 72 | canvas.drawColor(0, PorterDuff.Mode.CLEAR); |
| 73 | |
| 74 | // opaque circle matte |
| 75 | mPaint = new Paint(); |
| 76 | mPaint.setAntiAlias(true); |
| 77 | mPaint.setColor(Color.BLACK); |
| 78 | mPaint.setStyle(Paint.Style.FILL); |
| 79 | canvas.drawPath(fillPath, mPaint); |
| 80 | |
| 81 | // mask in the icon where the bitmap is opaque |
| 82 | mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); |
| 83 | canvas.drawBitmap(icon, cropRect, circleRect, mPaint); |
| 84 | |
| 85 | // prepare paint for frame drawing |
| 86 | mPaint.setXfermode(null); |
| 87 | |
| 88 | mScale = 1f; |
| 89 | |
| 90 | mSrcRect = new Rect(0, 0, mSize, mSize); |
| 91 | mDstRect = new RectF(0, 0, mSize, mSize); |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public void draw(Canvas canvas) { |
| 96 | final float inside = mScale * mSize; |
| 97 | final float pad = (mSize - inside) / 2f; |
| 98 | |
| 99 | mDstRect.set(pad, pad, mSize - pad, mSize - pad); |
| 100 | canvas.drawBitmap(mBitmap, mSrcRect, mDstRect, null); |
| 101 | } |
| 102 | |
| 103 | public void setScale(float scale) { |
| 104 | mScale = scale; |
| 105 | } |
| 106 | |
| 107 | public float getScale() { |
| 108 | return mScale; |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public int getOpacity() { |
| 113 | return PixelFormat.TRANSLUCENT; |
| 114 | } |
| 115 | |
| 116 | @Override |
| 117 | public void setAlpha(int alpha) { |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public void setColorFilter(ColorFilter cf) { |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public int getIntrinsicWidth() { |
| 126 | return mSize; |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public int getIntrinsicHeight() { |
| 131 | return mSize; |
| 132 | } |
| 133 | } |
| 134 | |