| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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 RenderEffectShaderActivity extends Activity { |
| 35 |
@Override |
| 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 ShaderRenderEffectView(this), params); |
| 49 |
|
| 50 |
setContentView(layout); |
| 51 |
} |
| 52 |
|
| 53 |
public static class ShaderRenderEffectView extends View { |
| 54 |
|
| 55 |
private final Paint mPaint; |
| 56 |
private final RenderNode mRenderNode; |
| 57 |
|
| 58 |
public ShaderRenderEffectView(Context c) { |
| 59 |
super(c); |
| 60 |
|
| 61 |
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); |
| 62 |
mRenderNode = new RenderNode("blurNode"); |
| 63 |
|
| 64 |
} |
| 65 |
|
| 66 |
@Override |
| 67 |
protected void onLayout(boolean changed, int left, int top, int right, int bottom) { |
| 68 |
if (changed) { |
| 69 |
LinearGradient gradient = new LinearGradient( |
| 70 |
0f, 0f, |
| 71 |
0f, bottom - top, |
| 72 |
new int[]{Color.CYAN, Color.MAGENTA}, |
| 73 |
null, |
| 74 |
Shader.TileMode.CLAMP |
| 75 |
); |
| 76 |
mRenderNode.setRenderEffect( |
| 77 |
RenderEffect.createShaderEffect(gradient) |
| 78 |
); |
| 79 |
|
| 80 |
int width = right - left; |
| 81 |
int height = bottom - top; |
| 82 |
mRenderNode.setPosition(0, 0, width, height); |
| 83 |
Canvas canvas = mRenderNode.beginRecording(width, height); |
| 84 |
mPaint.setColor(Color.BLUE); |
| 85 |
|
| 86 |
canvas.drawRect( |
| 87 |
0, |
| 88 |
0, |
| 89 |
width, |
| 90 |
height, |
| 91 |
mPaint |
| 92 |
); |
| 93 |
|
| 94 |
mPaint.setColor(Color.RED); |
| 95 |
canvas.drawCircle((right - left) / 2f, (bottom - top) / 2f, 50f, mPaint); |
| 96 |
|
| 97 |
mRenderNode.endRecording(); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
@Override |
| 102 |
protected void onDraw(Canvas canvas) { |
| 103 |
super.onDraw(canvas); |
| 104 |
canvas.drawRenderNode(mRenderNode); |
| 105 |
} |
| 106 |
} |
| 107 |
} |