Home | History | Annotate | Download | only in graphics
      1 /*
      2  * Copyright (C) 2008 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.example.android.apis.graphics;
     18 
     19 import android.app.Activity;
     20 import android.content.Context;
     21 import android.graphics.*;
     22 import android.os.Bundle;
     23 import android.view.*;
     24 
     25 public class Layers extends GraphicsActivity {
     26 
     27     @Override
     28     protected void onCreate(Bundle savedInstanceState) {
     29         super.onCreate(savedInstanceState);
     30         setContentView(new SampleView(this));
     31     }
     32 
     33     private static class SampleView extends View {
     34         private static final int LAYER_FLAGS = Canvas.MATRIX_SAVE_FLAG |
     35                                             Canvas.CLIP_SAVE_FLAG |
     36                                             Canvas.HAS_ALPHA_LAYER_SAVE_FLAG |
     37                                             Canvas.FULL_COLOR_LAYER_SAVE_FLAG |
     38                                             Canvas.CLIP_TO_LAYER_SAVE_FLAG;
     39 
     40         private Paint mPaint;
     41 
     42         public SampleView(Context context) {
     43             super(context);
     44             setFocusable(true);
     45 
     46             mPaint = new Paint();
     47             mPaint.setAntiAlias(true);
     48         }
     49 
     50         @Override protected void onDraw(Canvas canvas) {
     51             canvas.drawColor(Color.WHITE);
     52 
     53             canvas.translate(10, 10);
     54 
     55             canvas.saveLayerAlpha(0, 0, 200, 200, 0x88, LAYER_FLAGS);
     56 
     57             mPaint.setColor(Color.RED);
     58             canvas.drawCircle(75, 75, 75, mPaint);
     59             mPaint.setColor(Color.BLUE);
     60             canvas.drawCircle(125, 125, 75, mPaint);
     61 
     62             canvas.restore();
     63         }
     64     }
     65 }
     66 
     67