1 /*
2  * Copyright (C) 2019 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.ColorFilter;
21 import android.graphics.Paint;
22 import android.graphics.PixelFormat;
23 import android.graphics.drawable.Drawable;
24 
25 /**
26  * A simple drawable which draws a bitmap at a fixed position irrespective of the bounds
27  */
28 public class ShiftedBitmapDrawable extends Drawable {
29 
30     private final Paint mPaint = new Paint(Paint.FILTER_BITMAP_FLAG);
31     private final Bitmap mBitmap;
32     private float mShiftX;
33     private float mShiftY;
34 
35     private final ConstantState mConstantState;
36 
ShiftedBitmapDrawable(Bitmap bitmap, float shiftX, float shiftY)37     public ShiftedBitmapDrawable(Bitmap bitmap, float shiftX, float shiftY) {
38         mBitmap = bitmap;
39         mShiftX = shiftX;
40         mShiftY = shiftY;
41 
42         mConstantState = new MyConstantState(mBitmap, mShiftX, mShiftY);
43     }
44 
getShiftX()45     public float getShiftX() {
46         return mShiftX;
47     }
48 
getShiftY()49     public float getShiftY() {
50         return mShiftY;
51     }
52 
setShiftX(float shiftX)53     public void setShiftX(float shiftX) {
54         mShiftX = shiftX;
55     }
56 
setShiftY(float shiftY)57     public void setShiftY(float shiftY) {
58         mShiftY = shiftY;
59     }
60 
61     @Override
draw(Canvas canvas)62     public void draw(Canvas canvas) {
63         canvas.drawBitmap(mBitmap, mShiftX, mShiftY, mPaint);
64     }
65 
66     @Override
setAlpha(int i)67     public void setAlpha(int i) { }
68 
69     @Override
setColorFilter(ColorFilter colorFilter)70     public void setColorFilter(ColorFilter colorFilter) {
71         mPaint.setColorFilter(colorFilter);
72     }
73 
74     @Override
getOpacity()75     public int getOpacity() {
76         return PixelFormat.TRANSLUCENT;
77     }
78 
79     @Override
getConstantState()80     public ConstantState getConstantState() {
81         return mConstantState;
82     }
83 
84     private static class MyConstantState extends ConstantState {
85         private final Bitmap mBitmap;
86         private float mShiftX;
87         private float mShiftY;
88 
MyConstantState(Bitmap bitmap, float shiftX, float shiftY)89         MyConstantState(Bitmap bitmap, float shiftX, float shiftY) {
90             mBitmap = bitmap;
91             mShiftX = shiftX;
92             mShiftY = shiftY;
93         }
94 
95         @Override
newDrawable()96         public Drawable newDrawable() {
97             return new ShiftedBitmapDrawable(mBitmap, mShiftX, mShiftY);
98         }
99 
100         @Override
getChangingConfigurations()101         public int getChangingConfigurations() {
102             return 0;
103         }
104     }
105 }