Home | History | Annotate | Download | only in hwui
      1 /*
      2  * Copyright (C) 2012 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.Shader;
     26 import android.os.Bundle;
     27 import android.view.View;
     28 
     29 @SuppressWarnings("UnusedDeclaration")
     30 public class GradientStopsActivity extends Activity {
     31     @Override
     32     protected void onCreate(Bundle savedInstanceState) {
     33         super.onCreate(savedInstanceState);
     34 
     35         setContentView(new GradientView(this));
     36     }
     37 
     38     private class GradientView extends View {
     39         public GradientView(Context context) {
     40             super(context);
     41         }
     42 
     43         @Override
     44         protected void onDraw(Canvas canvas) {
     45             int[] colors = new int[] { 0xffff0000, 0xff0000ff };
     46             float[] positions = new float[] { 0.3f, 0.6f };
     47             LinearGradient gradient = new LinearGradient(0.0f, 0.0f, 256.0f, 0.0f,
     48                     colors, positions, Shader.TileMode.CLAMP);
     49 
     50             Paint paint = new Paint();
     51             paint.setShader(gradient);
     52 
     53             canvas.drawRect(0.0f, 0.0f, 256.0f, 50.0f, paint);
     54 
     55             colors = new int[] { 0xffff0000, 0xff0000ff, 0xff00ff00 };
     56             positions = new float[] { 0.3f, 0.6f, 1.0f };
     57             gradient = new LinearGradient(0.0f, 0.0f, 256.0f, 0.0f,
     58                     colors, positions, Shader.TileMode.CLAMP);
     59 
     60             paint.setShader(gradient);
     61 
     62             canvas.translate(0.0f, 75.0f);
     63             canvas.drawRect(0.0f, 0.0f, 256.0f, 50.0f, paint);
     64 
     65             colors = new int[] { 0xffff0000, 0xff0000ff, 0xff00ff00 };
     66             positions = new float[] { 0.0f, 0.3f, 0.6f };
     67             gradient = new LinearGradient(0.0f, 0.0f, 256.0f, 0.0f,
     68                     colors, positions, Shader.TileMode.CLAMP);
     69 
     70             paint.setShader(gradient);
     71 
     72             canvas.translate(0.0f, 75.0f);
     73             canvas.drawRect(0.0f, 0.0f, 256.0f, 50.0f, paint);
     74 
     75             colors = new int[] { 0xff000000, 0xffffffff };
     76             gradient = new LinearGradient(0.0f, 0.0f, 256.0f, 0.0f,
     77                     colors, null, Shader.TileMode.CLAMP);
     78 
     79             paint.setShader(gradient);
     80 
     81             canvas.translate(0.0f, 75.0f);
     82             canvas.drawRect(0.0f, 0.0f, 256.0f, 50.0f, paint);
     83 
     84             gradient = new LinearGradient(0.0f, 0.0f, 256.0f, 0.0f,
     85                     colors, null, Shader.TileMode.REPEAT);
     86 
     87             paint.setShader(gradient);
     88 
     89             canvas.translate(0.0f, 75.0f);
     90             canvas.drawRect(0.0f, 0.0f, 768.0f, 50.0f, paint);
     91 
     92             gradient = new LinearGradient(0.0f, 0.0f, 256.0f, 0.0f,
     93                     colors, null, Shader.TileMode.MIRROR);
     94 
     95             paint.setShader(gradient);
     96 
     97             canvas.translate(0.0f, 75.0f);
     98             canvas.drawRect(0.0f, 0.0f, 768.0f, 50.0f, paint);
     99 
    100             gradient = new LinearGradient(0.0f, 0.0f, 256.0f, 0.0f,
    101                     colors, null, Shader.TileMode.CLAMP);
    102 
    103             paint.setShader(gradient);
    104 
    105             canvas.translate(0.0f, 75.0f);
    106             canvas.drawRect(0.0f, 0.0f, 768.0f, 50.0f, paint);
    107 
    108             gradient = new LinearGradient(0.0f, 0.0f, 768.0f, 0.0f,
    109                     colors, null, Shader.TileMode.CLAMP);
    110 
    111             paint.setShader(gradient);
    112 
    113             canvas.translate(0.0f, 75.0f);
    114             canvas.drawRect(0.0f, 0.0f, 768.0f, 50.0f, paint);
    115 
    116             gradient = new LinearGradient(0.0f, 0.0f, 512.0f, 0.0f,
    117                     colors, null, Shader.TileMode.CLAMP);
    118 
    119             paint.setShader(gradient);
    120 
    121             canvas.translate(0.0f, 75.0f);
    122             canvas.drawRect(0.0f, 0.0f, 512.0f, 50.0f, paint);
    123         }
    124     }
    125 }
    126