Home | History | Annotate | Download | only in systemui
      1 /*
      2  * Copyright (C) 2014 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.systemui;
     18 
     19 import android.graphics.Bitmap;
     20 import android.graphics.BitmapShader;
     21 import android.graphics.Canvas;
     22 import android.graphics.Matrix;
     23 import android.graphics.Paint;
     24 import android.graphics.RectF;
     25 import android.graphics.Shader;
     26 
     27 public class BitmapHelper {
     28     /**
     29      * Generate a new bitmap (width x height pixels, ARGB_8888) with the input bitmap scaled
     30      * to fit and clipped to an inscribed circle.
     31      * @param input Bitmap to resize and clip
     32      * @param width Width of output bitmap (and diameter of circle)
     33      * @param height Height of output bitmap
     34      * @return A shiny new bitmap for you to use
     35      */
     36     public static Bitmap createCircularClip(Bitmap input, int width, int height) {
     37         if (input == null) return null;
     38 
     39         final int inWidth = input.getWidth();
     40         final int inHeight = input.getHeight();
     41         final Bitmap output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
     42         final Canvas canvas = new Canvas(output);
     43         final Paint paint = new Paint();
     44         paint.setShader(new BitmapShader(input, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));
     45         paint.setAntiAlias(true);
     46         final RectF srcRect = new RectF(0, 0, inWidth, inHeight);
     47         final RectF dstRect = new RectF(0, 0, width, height);
     48         final Matrix m = new Matrix();
     49         m.setRectToRect(srcRect, dstRect, Matrix.ScaleToFit.CENTER);
     50         canvas.setMatrix(m);
     51         canvas.drawCircle(inWidth / 2, inHeight / 2, inWidth / 2, paint);
     52         return output;
     53     }
     54 }
     55