1 /*
2  * Copyright (C) 2010 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.animation.ObjectAnimator;
20 import android.app.Activity;
21 import android.content.Context;
22 import android.graphics.Bitmap;
23 import android.graphics.BitmapFactory;
24 import android.graphics.BitmapShader;
25 import android.graphics.Canvas;
26 import android.graphics.Paint;
27 import android.graphics.drawable.ColorDrawable;
28 import android.os.Bundle;
29 import android.view.View;
30 
31 @SuppressWarnings({"UnusedDeclaration"})
32 public class LinesActivity extends Activity {
33     private ObjectAnimator mAnimator;
34 
35     @Override
onCreate(Bundle savedInstanceState)36     protected void onCreate(Bundle savedInstanceState) {
37         super.onCreate(savedInstanceState);
38         getWindow().setBackgroundDrawable(new ColorDrawable(0xffffffff));
39         final LinesView view = new LinesView(this);
40         setContentView(view);
41 
42         mAnimator = ObjectAnimator.ofFloat(view, "offset", 0.0f, 15.0f);
43         mAnimator.setDuration(1500);
44         mAnimator.setRepeatCount(ObjectAnimator.INFINITE);
45         mAnimator.setRepeatMode(ObjectAnimator.REVERSE);
46         mAnimator.start();
47     }
48 
49     @Override
onDestroy()50     protected void onDestroy() {
51         super.onDestroy();
52         mAnimator.cancel();
53     }
54 
55     public static class LinesView extends View {
56         private static final boolean LINE_AA = true;
57 
58         private final Bitmap mBitmap1;
59         private final Paint mSmallPaint;
60         private final Paint mMediumPaint;
61         private final Paint mLargePaint;
62         private final BitmapShader mShader;
63         private final float[] mPoints;
64         private final Paint mAlphaPaint;
65         private final Paint mHairLinePaint;
66 
67         private float mOffset;
68 
LinesView(Context c)69         public LinesView(Context c) {
70             super(c);
71 
72             mBitmap1 = BitmapFactory.decodeResource(c.getResources(), R.drawable.sunset1);
73 
74             mSmallPaint = new Paint();
75             mSmallPaint.setAntiAlias(LINE_AA);
76             mSmallPaint.setColor(0xffff0000);
77             mSmallPaint.setStrokeWidth(1.0f);
78 
79             mMediumPaint = new Paint();
80             mMediumPaint.setAntiAlias(LINE_AA);
81             mMediumPaint.setColor(0xff0000ff);
82             mMediumPaint.setStrokeWidth(4.0f);
83 
84             mLargePaint = new Paint();
85             mLargePaint.setAntiAlias(LINE_AA);
86             mLargePaint.setColor(0xff00ff00);
87             mLargePaint.setStrokeWidth(15.0f);
88 
89             mAlphaPaint = new Paint();
90             mAlphaPaint.setAntiAlias(LINE_AA);
91             mAlphaPaint.setColor(0x7fff0050);
92             mAlphaPaint.setStrokeWidth(10.0f);
93 
94             mHairLinePaint = new Paint();
95             mHairLinePaint.setAntiAlias(LINE_AA);
96             mHairLinePaint.setColor(0xff0000ff);
97             mHairLinePaint.setStrokeWidth(0.0f);
98 
99             mShader = new BitmapShader(mBitmap1, BitmapShader.TileMode.MIRROR,
100                     BitmapShader.TileMode.MIRROR);
101 
102             mPoints = new float[] {
103                     62.0f, 0.0f, 302.0f, 400.0f,
104                     302.0f, 400.0f, 352.0f, 400.0f,
105                     352.0f, 400.0f, 352.0f, 500.0f
106             };
107         }
108 
setOffset(float offset)109         public void setOffset(float offset) {
110             mOffset = offset;
111             invalidate();
112         }
113 
114         @Override
onDraw(Canvas canvas)115         protected void onDraw(Canvas canvas) {
116             super.onDraw(canvas);
117 
118             canvas.save();
119             canvas.translate(100.0f, 20.0f);
120 
121             canvas.drawLine(0.0f, 0.0f, 40.0f, 400.0f, mSmallPaint);
122             canvas.drawLine(5.0f, 0.0f, 95.0f, 400.0f, mMediumPaint);
123             canvas.drawLine(22.0f, 0.0f, 162.0f, 400.0f, mLargePaint);
124 
125             mLargePaint.setShader(mShader);
126             canvas.drawLine(42.0f, 0.0f, 222.0f, 400.0f, mLargePaint);
127             for (int x = 0; x < 20; x++) {
128                 for (int y = 0; y < 20; y++) {
129                     canvas.drawPoint(500.0f + x * (15.0f + mOffset),
130                             y * (15.0f + mOffset), mLargePaint);
131                 }
132             }
133             mLargePaint.setShader(null);
134 
135             canvas.drawLines(mPoints, mAlphaPaint);
136 
137             mSmallPaint.setAntiAlias(false);
138             canvas.drawLine(0.0f, 0.0f, 400.0f, 0.0f, mSmallPaint);
139             mSmallPaint.setAntiAlias(LINE_AA);
140             canvas.drawLine(0.0f, 0.0f, 0.0f, 400.0f, mSmallPaint);
141             canvas.drawLine(0.0f, 400.0f, 400.0f, 400.0f, mSmallPaint);
142 
143             canvas.translate(120.0f, 0.0f);
144             mAlphaPaint.setShader(mShader);
145             canvas.drawLines(mPoints, mAlphaPaint);
146             mAlphaPaint.setShader(null);
147 
148             canvas.restore();
149 
150             canvas.save();
151             canvas.scale(10.0f, 10.0f);
152             canvas.drawLine(50.0f, 40.0f, 10.0f, 40.0f, mSmallPaint);
153             canvas.drawLine(10.0f, 45.0f, 20.0f, 55.0f, mSmallPaint);
154             canvas.drawLine(10.0f, 60.0f, 50.0f, 60.0f, mHairLinePaint);
155             canvas.restore();
156 
157             canvas.save();
158             canvas.scale(10.0f, 50.0f);
159             mSmallPaint.setStrokeWidth(0.0f);
160             canvas.drawLine(20.0f, 9.0f, 30.0f, 11.0f, mSmallPaint);
161             mSmallPaint.setStrokeWidth(1.0f);
162             canvas.drawLine(30.0f, 9.0f, 40.0f, 11.0f, mSmallPaint);
163             canvas.restore();
164         }
165     }
166 }
167