• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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.launcher3.graphics;
17 
18 import android.graphics.Bitmap;
19 import android.graphics.Canvas;
20 import android.graphics.Paint;
21 import android.graphics.Rect;
22 import android.graphics.RectF;
23 
24 /**
25  * Utility class which draws a bitmap by dissecting it into 3 segments and stretching
26  * the middle segment.
27  */
28 public class NinePatchDrawHelper {
29 
30     // The extra width used for the bitmap. This portion of the bitmap is stretched to match the
31     // width of the draw region. Randomly chosen, any value > 4 will be sufficient.
32     public static final int EXTENSION_PX = 20;
33 
34     private final Rect mSrc = new Rect();
35     private final RectF mDst = new RectF();
36     public final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
37 
38     /**
39      * Draws the bitmap split into three parts horizontally, with the middle part having width
40      * as {@link #EXTENSION_PX} in the center of the bitmap.
41      */
draw(Bitmap bitmap, Canvas canvas, float left, float top, float right)42     public void draw(Bitmap bitmap, Canvas canvas, float left, float top, float right) {
43         int height = bitmap.getHeight();
44 
45         mSrc.top = 0;
46         mSrc.bottom = height;
47         mDst.top = top;
48         mDst.bottom = top + height;
49         draw3Patch(bitmap, canvas, left, right);
50     }
51 
52 
53     /**
54      * Draws the bitmap split horizontally into 3 parts (same as {@link #draw}) and split
55      * vertically into two parts, bottom part of size {@link #EXTENSION_PX} / 2 which is
56      * stretched vertically.
57      */
drawVerticallyStretched(Bitmap bitmap, Canvas canvas, float left, float top, float right, float bottom)58     public void drawVerticallyStretched(Bitmap bitmap, Canvas canvas, float left, float top,
59             float right, float bottom) {
60         draw(bitmap, canvas, left, top, right);
61 
62         // Draw bottom stretched region.
63         int height = bitmap.getHeight();
64         mSrc.top = height - EXTENSION_PX / 4;
65         mSrc.bottom = height;
66         mDst.top = top + height;
67         mDst.bottom = bottom;
68         draw3Patch(bitmap, canvas, left, right);
69     }
70 
71 
72 
draw3Patch(Bitmap bitmap, Canvas canvas, float left, float right)73     private void draw3Patch(Bitmap bitmap, Canvas canvas, float left, float right) {
74         int width = bitmap.getWidth();
75         int halfWidth = width / 2;
76 
77         // Draw left edge
78         drawRegion(bitmap, canvas, 0, halfWidth, left, left + halfWidth);
79 
80         // Draw right edge
81         drawRegion(bitmap, canvas, halfWidth, width, right - halfWidth, right);
82 
83         // Draw middle stretched region
84         int halfExt = EXTENSION_PX / 4;
85         drawRegion(bitmap, canvas, halfWidth - halfExt, halfWidth + halfExt,
86                 left + halfWidth, right - halfWidth);
87     }
88 
drawRegion(Bitmap bitmap, Canvas c, int srcLeft, int srcRight, float dstLeft, float dstRight)89     private void drawRegion(Bitmap bitmap, Canvas c,
90             int srcLeft, int srcRight, float dstLeft, float dstRight) {
91         mSrc.left = srcLeft;
92         mSrc.right = srcRight;
93 
94         mDst.left = dstLeft;
95         mDst.right = dstRight;
96         c.drawBitmap(bitmap, mSrc, mDst, paint);
97     }
98 }
99