1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
| \android\frameworks\base\tests\HwAccelerationTest\src\com\android\test\hwui\GradientsActivity.java
public class GradientsActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
final FrameLayout layout = new FrameLayout(this);
final ShadersView shadersView = new ShadersView(this); final GradientView gradientView = new GradientView(this); final RadialGradientView radialGradientView = new RadialGradientView(this); final SweepGradientView sweepGradientView = new SweepGradientView(this); final BitmapView bitmapView = new BitmapView(this);
final SeekBar rotateView = new SeekBar(this); rotateView.setMax(360); rotateView.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { public void onStopTrackingTouch(SeekBar seekBar) { }
public void onStartTrackingTouch(SeekBar seekBar) { }
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { gradientView.setRotationY((float) progress); radialGradientView.setRotationX((float) progress); sweepGradientView.setRotationY((float) progress); bitmapView.setRotationX((float) progress); } });
layout.addView(shadersView); layout.addView(gradientView, new FrameLayout.LayoutParams( 200, 200, Gravity.CENTER));
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(200, 200, Gravity.CENTER); lp.setMargins(220, 0, 0, 0); layout.addView(radialGradientView, lp);
lp = new FrameLayout.LayoutParams(200, 200, Gravity.CENTER); lp.setMargins(440, 0, 0, 0); layout.addView(sweepGradientView, lp);
lp = new FrameLayout.LayoutParams(200, 200, Gravity.CENTER); lp.setMargins(220, -220, 0, 0); layout.addView(bitmapView, lp);
layout.addView(rotateView, new FrameLayout.LayoutParams( 300, FrameLayout.LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM));
setContentView(layout); }
static class BitmapView extends View { private final Paint mPaint;
BitmapView(Context c) { super(c);
Bitmap texture = BitmapFactory.decodeResource(c.getResources(), R.drawable.sunset1); BitmapShader shader = new BitmapShader(texture, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT); mPaint = new Paint(); mPaint.setShader(shader); }
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); setMeasuredDimension(200, 200); }
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawRect(0.0f, 0.0f, getWidth(), getHeight(), mPaint); } }
static class GradientView extends View { private final Paint mPaint;
GradientView(Context c) { super(c);
LinearGradient gradient = new LinearGradient(0, 0, 200, 0, 0xFF000000, 0, Shader.TileMode.CLAMP); mPaint = new Paint(); mPaint.setShader(gradient); }
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); setMeasuredDimension(200, 200); }
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawRect(0.0f, 0.0f, getWidth(), getHeight(), mPaint); } }
static class RadialGradientView extends View { private final Paint mPaint;
RadialGradientView(Context c) { super(c);
RadialGradient gradient = new RadialGradient(0.0f, 0.0f, 100.0f, 0xff000000, 0xffffffff, Shader.TileMode.MIRROR); mPaint = new Paint(); mPaint.setShader(gradient); }
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); setMeasuredDimension(200, 200); }
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawRect(0.0f, 0.0f, getWidth(), getHeight(), mPaint); } }
static class SweepGradientView extends View { private final Paint mPaint;
SweepGradientView(Context c) { super(c);
SweepGradient gradient = new SweepGradient(100.0f, 100.0f, 0xff000000, 0xffffffff); mPaint = new Paint(); mPaint.setShader(gradient); }
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); setMeasuredDimension(200, 200); }
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawRect(0.0f, 0.0f, getWidth(), getHeight(), mPaint); } }
static class ShadersView extends View { private final Paint mPaint; private final float mDrawWidth; private final float mDrawHeight; private final LinearGradient mGradient; private final LinearGradient mGradientStops; private final Matrix mMatrix;
ShadersView(Context c) { super(c);
mDrawWidth = 200; mDrawHeight = 200;
mGradient = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP); mGradientStops = new LinearGradient(0, 0, 0, 1, new int[] { 0xFFFF0000, 0xFF00FF00, 0xFF0000FF }, null, Shader.TileMode.CLAMP);
mMatrix = new Matrix();
mPaint = new Paint(); }
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawRGB(255, 255, 255);
canvas.save(); float top = 40.0f; float right = 40.0f + mDrawWidth; float left = 40.0f; float bottom = 40.0f + mDrawHeight;
mMatrix.setScale(1, mDrawWidth); mMatrix.postRotate(90); mMatrix.postTranslate(right, top); mGradient.setLocalMatrix(mMatrix); mPaint.setShader(mGradient); canvas.drawRect(right - mDrawWidth, top, right, top + mDrawHeight, mPaint);
top += 40.0f + mDrawHeight; bottom += 40.0f + mDrawHeight;
mMatrix.setScale(1, mDrawHeight); mMatrix.postTranslate(left, top); mGradient.setLocalMatrix(mMatrix); mPaint.setShader(mGradient); canvas.drawRect(left, top, right, top + mDrawHeight, mPaint);
left += 40.0f + mDrawWidth; right += 40.0f + mDrawWidth; top -= 40.0f + mDrawHeight; bottom -= 40.0f + mDrawHeight;
mMatrix.setScale(1, mDrawHeight); mMatrix.postRotate(180); mMatrix.postTranslate(left, bottom); mGradient.setLocalMatrix(mMatrix); mPaint.setShader(mGradient); canvas.drawRect(left, bottom - mDrawHeight, right, bottom, mPaint);
top += 40.0f + mDrawHeight; bottom += 40.0f + mDrawHeight;
mMatrix.setScale(1, mDrawWidth); mMatrix.postRotate(-90); mMatrix.postTranslate(left, top); mGradient.setLocalMatrix(mMatrix); mPaint.setShader(mGradient); canvas.drawRect(left, top, left + mDrawWidth, bottom, mPaint);
right = left + mDrawWidth; left = 40.0f; top = bottom + 20.0f; bottom = top + 50.0f;
mMatrix.setScale(1, mDrawWidth); mMatrix.postRotate(90); mMatrix.postTranslate(right, top); mGradientStops.setLocalMatrix(mMatrix); mPaint.setShader(mGradientStops); canvas.drawRect(left, top, right, bottom, mPaint); canvas.restore(); } } }
|