xref: /aosp_15_r20/cts/tests/tests/uirendering/src/android/uirendering/cts/testclasses/view/AlphaTestView.java (revision b7c941bb3fa97aba169d73cee0bed2de8ac964bf)
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 
17 package android.uirendering.cts.testclasses.view;
18 
19 import android.content.Context;
20 import android.graphics.Canvas;
21 import android.graphics.Color;
22 import android.graphics.Paint;
23 import android.util.AttributeSet;
24 import android.view.View;
25 
26 /**
27  * Test View used to verify a View's custom alpha implementation logic
28  * in conjunction with {@link android.view.ViewPropertyAnimator}
29  */
30 public class AlphaTestView extends View {
31 
32     private Paint mPaint = new Paint();
33     private int mStartColor = Color.RED;
34     private int mEndColor = Color.BLUE;
35 
AlphaTestView(Context context)36     public AlphaTestView(Context context) {
37         super(context);
38     }
39 
AlphaTestView(Context context, AttributeSet attrs)40     public AlphaTestView(Context context, AttributeSet attrs) {
41         super(context, attrs);
42     }
43 
AlphaTestView(Context context, AttributeSet attrs, int defStyleAttr)44     public AlphaTestView(Context context, AttributeSet attrs, int defStyleAttr) {
45         super(context, attrs, defStyleAttr);
46     }
47 
AlphaTestView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)48     public AlphaTestView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
49         super(context, attrs, defStyleAttr, defStyleRes);
50     }
51 
setStartColor(int startColor)52     public void setStartColor(int startColor) {
53         mStartColor = startColor;
54         mPaint.setColor(mStartColor);
55     }
56 
setEndColor(int endColor)57     public void setEndColor(int endColor) {
58         mEndColor = endColor;
59     }
60 
61     @Override
onSetAlpha(int alpha)62     protected boolean onSetAlpha(int alpha) {
63         mPaint.setColor(blendColor(mStartColor, mEndColor, 1.0f - alpha / 255.0f));
64         return true;
65     }
66 
67     @Override
onDraw(Canvas canvas)68     protected void onDraw(Canvas canvas) {
69         canvas.drawRect(getLeft(), getTop(), getRight(), getBottom(), mPaint);
70     }
71 
getBlendedColor()72     public int getBlendedColor() {
73         return mPaint.getColor();
74     }
75 
blendColor(int color1, int color2, float ratio)76     private int blendColor(int color1, int color2, float ratio) {
77         float inverseRatio = 1 - ratio;
78         float a = (float) Color.alpha(color1) * inverseRatio + (float) Color.alpha(color2) * ratio;
79         float r = (float) Color.red(color1) * inverseRatio + (float) Color.red(color2) * ratio;
80         float g = (float) Color.green(color1) * inverseRatio + (float) Color.green(color2) * ratio;
81         float b = (float) Color.blue(color1) * inverseRatio + (float) Color.blue(color2) * ratio;
82         return Color.argb((int) a, (int) r, (int) g, (int) b);
83     }
84 }
85