Lines2Activity.java revision 5b0200bd47e8a9a4dc8d2e6c3a110d522b30bf82
1/* 2 * Copyright (C) 2011 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 17package com.android.test.hwui; 18 19import android.animation.ObjectAnimator; 20import android.app.Activity; 21import android.content.Context; 22import android.graphics.Canvas; 23import android.graphics.Color; 24import android.graphics.Paint; 25import android.graphics.drawable.ColorDrawable; 26import android.os.Bundle; 27import android.view.View; 28import android.widget.FrameLayout; 29 30@SuppressWarnings({"UnusedDeclaration"}) 31public class Lines2Activity extends Activity { 32 private ObjectAnimator mAnimator; 33 34 @Override 35 protected void onCreate(Bundle savedInstanceState) { 36 super.onCreate(savedInstanceState); 37 getWindow().setBackgroundDrawable(new ColorDrawable(0xff000000)); 38 FrameLayout frame = new FrameLayout(this); 39 final LinesView gpuView = new LinesView(this, 0, Color.GREEN); 40 frame.addView(gpuView); 41 final LinesView swView = new LinesView(this, 400, Color.RED); 42 swView.setLayerType(View.LAYER_TYPE_SOFTWARE, null); 43 frame.addView(swView); 44 final LinesView hwBothView = new LinesView(this, 850, Color.GREEN); 45 // BUG: some lines not drawn or drawn with alpha when enabling hw layers 46// hwBothView.setLayerType(View.LAYER_TYPE_HARDWARE, null); 47 frame.addView(hwBothView); 48 final LinesView swBothView = new LinesView(this, 854, Color.RED); 49 swBothView.setLayerType(View.LAYER_TYPE_SOFTWARE, null); 50 frame.addView(swBothView); 51 setContentView(frame); 52 } 53 54 @Override 55 protected void onDestroy() { 56 super.onDestroy(); 57 } 58 59 public static class LinesView extends View { 60 61 private float mOffset; 62 private int mColor; 63 private float[] basePoints = { 64 120, 0, 120, 20, 120, 20, 125, 0, 130, 0, 132, 10 65 }; 66 private float[] copyPoints = new float[12]; 67 68 public LinesView(Context c, float offset, int color) { 69 super(c); 70 mOffset = offset; 71 mColor = color; 72 } 73 74 private void drawLines(Canvas canvas, Paint p, float xOffset, float yOffset) { 75 canvas.drawLine(10 + xOffset, yOffset, 10 + xOffset, 40 + yOffset, p); 76 canvas.drawLine(30 + xOffset, yOffset, 40 + xOffset, 40 + yOffset, p); 77 canvas.drawLine(40 + xOffset, yOffset, 75 + xOffset, 35 + yOffset, p); 78 canvas.drawLine(50 + xOffset, 5+ yOffset, 100 + xOffset, 15 + yOffset, p); 79 canvas.drawLine(60 + xOffset, yOffset, 110 + xOffset, 2 + yOffset, p); 80 canvas.drawLine(60 + xOffset, 40 + yOffset, 110 + xOffset, 40 + yOffset, p); 81 for (int i = 0; i < 12; i += 2) { 82 copyPoints[i] = basePoints[i] + xOffset; 83 copyPoints[i+1] = basePoints[i+1] + yOffset; 84 } 85 canvas.drawLines(copyPoints, 0, 12, p); 86 } 87 88 @Override 89 protected void onDraw(Canvas canvas) { 90 super.onDraw(canvas); 91 Paint p = new Paint(); 92 p.setColor(mColor); 93 float yOffset = 10; 94 95 canvas.save(); 96 drawLines(canvas, p, mOffset, yOffset); 97 canvas.scale(2, 2); 98 canvas.translate(60, 0); 99 drawLines(canvas, p, mOffset/2, yOffset/2); 100 canvas.restore(); 101 102 yOffset +=100; 103 canvas.save(); 104 p.setStrokeWidth(1); 105 drawLines(canvas, p, mOffset, yOffset); 106 canvas.scale(2, 2); 107 canvas.translate(60, 0); 108 drawLines(canvas, p, mOffset/2, yOffset/2); 109 canvas.restore(); 110 111 yOffset += 100; 112 canvas.save(); 113 p.setStrokeWidth(2); 114 drawLines(canvas, p, mOffset, yOffset); 115 canvas.scale(2, 2); 116 canvas.translate(60, 0); 117 drawLines(canvas, p, mOffset/2, yOffset/2); 118 canvas.restore(); 119 120 p.setAntiAlias(true); 121 p.setStrokeWidth(0); 122 yOffset += 100; 123 canvas.save(); 124 drawLines(canvas, p, mOffset, yOffset); 125 canvas.scale(2, 2); 126 canvas.translate(60, 0); 127 drawLines(canvas, p, mOffset/2, yOffset/2); 128 canvas.restore(); 129 130 yOffset += 100; 131 canvas.save(); 132 p.setStrokeWidth(1); 133 drawLines(canvas, p, mOffset, yOffset); 134 canvas.scale(2, 2); 135 canvas.translate(60, 0); 136 drawLines(canvas, p, mOffset/2, yOffset/2); 137 canvas.restore(); 138 139 yOffset += 100; 140 canvas.save(); 141 p.setStrokeWidth(2); 142 drawLines(canvas, p, mOffset, yOffset); 143 canvas.scale(2, 2); 144 canvas.translate(60, 0); 145 drawLines(canvas, p, mOffset/2, yOffset/2); 146 canvas.restore(); 147 } 148 } 149} 150