1 /* 2 * Copyright (C) 2020 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.test.hwui; 18 19 import android.app.Activity; 20 import android.content.Context; 21 import android.graphics.Canvas; 22 import android.graphics.Color; 23 import android.graphics.LinearGradient; 24 import android.graphics.Paint; 25 import android.graphics.RenderEffect; 26 import android.graphics.RenderNode; 27 import android.graphics.Shader; 28 import android.os.Bundle; 29 import android.view.Gravity; 30 import android.view.View; 31 import android.widget.LinearLayout; 32 33 @SuppressWarnings({"UnusedDeclaration"}) 34 public class BlurActivity extends Activity { 35 @Override onCreate(Bundle savedInstanceState)36 protected void onCreate(Bundle savedInstanceState) { 37 super.onCreate(savedInstanceState); 38 39 LinearLayout layout = new LinearLayout(this); 40 layout.setClipChildren(false); 41 layout.setGravity(Gravity.CENTER); 42 layout.setOrientation(LinearLayout.VERTICAL); 43 44 45 LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(500, 500); 46 params.bottomMargin = 100; 47 48 layout.addView(new BlurGradientView(this), params); 49 layout.addView(new BlurView(this), params); 50 51 setContentView(layout); 52 } 53 54 public static class BlurGradientView extends View { 55 private final float mBlurRadius = 25f; 56 private final Paint mPaint; 57 private final RenderNode mRenderNode; 58 BlurGradientView(Context c)59 public BlurGradientView(Context c) { 60 super(c); 61 mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); 62 mRenderNode = new RenderNode("BlurGradientView"); 63 mRenderNode.setRenderEffect( 64 RenderEffect.createBlurEffect( 65 mBlurRadius, 66 mBlurRadius, 67 null, 68 Shader.TileMode.DECAL 69 ) 70 ); 71 } 72 73 @Override onLayout(boolean changed, int left, int top, int right, int bottom)74 protected void onLayout(boolean changed, int left, int top, int right, int bottom) { 75 if (changed) { 76 LinearGradient gradient = new LinearGradient( 77 0f, 78 0f, 79 right - left, 80 bottom - top, 81 Color.CYAN, 82 Color.YELLOW, 83 Shader.TileMode.CLAMP 84 ); 85 86 mPaint.setShader(gradient); 87 88 final int width = right - left; 89 final int height = bottom - top; 90 mRenderNode.setPosition(0, 0, width, height); 91 92 Canvas canvas = mRenderNode.beginRecording(); 93 canvas.drawRect( 94 mBlurRadius * 2, 95 mBlurRadius * 2, 96 width - mBlurRadius * 2, 97 height - mBlurRadius * 2, 98 mPaint 99 ); 100 mRenderNode.endRecording(); 101 } 102 } 103 104 @Override onDraw(Canvas canvas)105 protected void onDraw(Canvas canvas) { 106 super.onDraw(canvas); 107 canvas.drawRenderNode(mRenderNode); 108 } 109 } 110 111 public static class BlurView extends View { 112 113 private final Paint mPaint; 114 private final RenderNode mRenderNode; 115 private final float mBlurRadius = 20f; 116 BlurView(Context c)117 public BlurView(Context c) { 118 super(c); 119 120 mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); 121 mRenderNode = new RenderNode("blurNode"); 122 mRenderNode.setRenderEffect( 123 RenderEffect.createBlurEffect( 124 mBlurRadius, 125 mBlurRadius, 126 null, 127 Shader.TileMode.DECAL 128 ) 129 ); 130 } 131 132 @Override onLayout(boolean changed, int left, int top, int right, int bottom)133 protected void onLayout(boolean changed, int left, int top, int right, int bottom) { 134 if (changed) { 135 int width = right - left; 136 int height = bottom - top; 137 mRenderNode.setPosition(0, 0, width, height); 138 Canvas canvas = mRenderNode.beginRecording(width, height); 139 mPaint.setColor(Color.BLUE); 140 141 canvas.drawRect( 142 mBlurRadius * 2, 143 mBlurRadius * 2, 144 width - mBlurRadius * 2, 145 height - mBlurRadius * 2, 146 mPaint 147 ); 148 149 mPaint.setColor(Color.RED); 150 canvas.drawCircle((right - left) / 2f, (bottom - top) / 2f, 50f, mPaint); 151 152 mRenderNode.endRecording(); 153 } 154 } 155 156 @Override onDraw(Canvas canvas)157 protected void onDraw(Canvas canvas) { 158 super.onDraw(canvas); 159 canvas.drawRenderNode(mRenderNode); 160 } 161 } 162 } 163