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