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.KeyEvent;
     24 import android.view.MotionEvent;
     25 import android.view.*;
     26 
     27 public class Patterns extends GraphicsActivity {
     28 
     29     @Override
     30     protected void onCreate(Bundle savedInstanceState) {
     31         super.onCreate(savedInstanceState);
     32         setContentView(new SampleView(this));
     33     }
     34 
     35     private static Bitmap makeBitmap1() {
     36         Bitmap bm = Bitmap.createBitmap(40, 40, Bitmap.Config.RGB_565);
     37         Canvas c = new Canvas(bm);
     38         c.drawColor(Color.RED);
     39         Paint p = new Paint();
     40         p.setColor(Color.BLUE);
     41         c.drawRect(5, 5, 35, 35, p);
     42         return bm;
     43     }
     44 
     45     private static Bitmap makeBitmap2() {
     46         Bitmap bm = Bitmap.createBitmap(64, 64, Bitmap.Config.ARGB_8888);
     47         Canvas c = new Canvas(bm);
     48         Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
     49         p.setColor(Color.GREEN);
     50         p.setAlpha(0xCC);
     51         c.drawCircle(32, 32, 27, p);
     52         return bm;
     53     }
     54 
     55     private static class SampleView extends View {
     56         private final Shader mShader1;
     57         private final Shader mShader2;
     58         private final Paint mPaint;
     59         private final DrawFilter mFastDF;
     60 
     61         private float mTouchStartX;
     62         private float mTouchStartY;
     63         private float mTouchCurrX;
     64         private float mTouchCurrY;
     65         private DrawFilter mDF;
     66 
     67         public SampleView(Context context) {
     68             super(context);
     69             setFocusable(true);
     70             setFocusableInTouchMode(true);
     71 
     72             mFastDF = new PaintFlagsDrawFilter(Paint.FILTER_BITMAP_FLAG |
     73                                                Paint.DITHER_FLAG,
     74                                                0);
     75 
     76             mShader1 = new BitmapShader(makeBitmap1(), Shader.TileMode.REPEAT,
     77                                         Shader.TileMode.REPEAT);
     78             mShader2 = new BitmapShader(makeBitmap2(), Shader.TileMode.REPEAT,
     79                                         Shader.TileMode.REPEAT);
     80 
     81             Matrix m = new Matrix();
     82             m.setRotate(30);
     83             mShader2.setLocalMatrix(m);
     84 
     85             mPaint = new Paint(Paint.FILTER_BITMAP_FLAG);
     86         }
     87 
     88         @Override protected void onDraw(Canvas canvas) {
     89             canvas.setDrawFilter(mDF);
     90 
     91             mPaint.setShader(mShader1);
     92             canvas.drawPaint(mPaint);
     93 
     94             canvas.translate(mTouchCurrX - mTouchStartX,
     95                              mTouchCurrY - mTouchStartY);
     96 
     97             mPaint.setShader(mShader2);
     98             canvas.drawPaint(mPaint);
     99         }
    100 
    101         @Override
    102         public boolean onTouchEvent(MotionEvent event) {
    103             float x = event.getX();
    104             float y = event.getY();
    105 
    106             switch (event.getAction()) {
    107                 case MotionEvent.ACTION_DOWN:
    108                     mTouchStartX = mTouchCurrX = x;
    109                     mTouchStartY = mTouchCurrY = y;
    110                     mDF = mFastDF;
    111                     invalidate();
    112                     break;
    113                 case MotionEvent.ACTION_MOVE:
    114                     mTouchCurrX = x;
    115                     mTouchCurrY = y;
    116                     invalidate();
    117                     break;
    118                 case MotionEvent.ACTION_UP:
    119                     mDF = null;
    120                     invalidate();
    121                     break;
    122             }
    123             return true;
    124         }
    125     }
    126 }
    127 
    128