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 
17 package com.android.test.hwui;
18 
19 import android.app.Activity;
20 import android.content.Context;
21 import android.content.res.Resources;
22 import android.graphics.Canvas;
23 import android.graphics.Paint;
24 import android.graphics.Path;
25 import android.graphics.PorterDuff;
26 import android.graphics.PorterDuffColorFilter;
27 import android.os.Bundle;
28 import android.util.AttributeSet;
29 import android.util.DisplayMetrics;
30 import android.view.View;
31 import android.view.ViewGroup;
32 import android.widget.ArrayAdapter;
33 import android.widget.FrameLayout;
34 import android.widget.ListView;
35 import android.widget.TextView;
36 
37 @SuppressWarnings({"UnusedDeclaration"})
38 public class ViewLayersActivity5 extends Activity {
39     private final Paint mPaint = new Paint();
40 
41     @Override
onCreate(Bundle savedInstanceState)42     protected void onCreate(Bundle savedInstanceState) {
43         super.onCreate(savedInstanceState);
44 
45         init();
46 
47         setContentView(R.layout.view_layers_5);
48         setupList(R.id.list1);
49     }
50 
51     public static class ClipFrameLayout extends FrameLayout {
52         private final Path mClipPath = new Path();
53         private boolean mClipEnabled;
54 
ClipFrameLayout(Context context, AttributeSet attrs)55         public ClipFrameLayout(Context context, AttributeSet attrs) {
56             super(context, attrs);
57         }
58 
ClipFrameLayout(Context context, AttributeSet attrs, int defStyle)59         public ClipFrameLayout(Context context, AttributeSet attrs, int defStyle) {
60             super(context, attrs, defStyle);
61         }
62 
isClipEnabled()63         public boolean isClipEnabled() {
64             return mClipEnabled;
65         }
66 
setClipEnabled(boolean clipEnabled)67         public void setClipEnabled(boolean clipEnabled) {
68             mClipEnabled = clipEnabled;
69             invalidate();
70         }
71 
72         @Override
dispatchDraw(Canvas canvas)73         protected void dispatchDraw(Canvas canvas) {
74             if (mClipEnabled) {
75                 mClipPath.reset();
76                 mClipPath.addCircle(getWidth() / 2.0f, getHeight() / 2.0f,
77                         Math.min(getWidth(), getHeight()) / 3.0f, Path.Direction.CW);
78 
79                 canvas.clipPath(mClipPath);
80             }
81             super.dispatchDraw(canvas);
82         }
83     }
84 
init()85     private void init() {
86         mPaint.setColorFilter(new PorterDuffColorFilter(0xff00ff00, PorterDuff.Mode.MULTIPLY));
87     }
88 
enableClip(View v)89     public void enableClip(View v) {
90         ((ClipFrameLayout) findViewById(R.id.container)).setClipEnabled(true);
91     }
92 
disableClip(View v)93     public void disableClip(View v) {
94         ((ClipFrameLayout) findViewById(R.id.container)).setClipEnabled(false);
95     }
96 
enableLayer(View v)97     public void enableLayer(View v) {
98         findViewById(R.id.container).setLayerType(View.LAYER_TYPE_HARDWARE, mPaint);
99     }
100 
disableLayer(View v)101     public void disableLayer(View v) {
102         findViewById(R.id.container).setLayerType(View.LAYER_TYPE_NONE, null);
103     }
104 
growLayer(View v)105     public void growLayer(View v) {
106         findViewById(R.id.container).getLayoutParams().height = ViewGroup.LayoutParams.MATCH_PARENT;
107         findViewById(R.id.container).requestLayout();
108     }
109 
shrinkLayer(View v)110     public void shrinkLayer(View v) {
111         findViewById(R.id.container).getLayoutParams().height = 300;
112         findViewById(R.id.container).requestLayout();
113     }
114 
setupList(int listId)115     private void setupList(int listId) {
116         final ListView list = (ListView) findViewById(listId);
117         list.setAdapter(new SimpleListAdapter(this));
118     }
119 
120     private static class SimpleListAdapter extends ArrayAdapter<String> {
SimpleListAdapter(Context context)121         public SimpleListAdapter(Context context) {
122             super(context, android.R.layout.simple_list_item_1, DATA_LIST);
123         }
124 
125         @Override
getView(int position, View convertView, ViewGroup parent)126         public View getView(int position, View convertView, ViewGroup parent) {
127             TextView v = (TextView) super.getView(position, convertView, parent);
128             final Resources r = getContext().getResources();
129             final DisplayMetrics metrics = r.getDisplayMetrics();
130             v.setCompoundDrawablePadding((int) (6 * metrics.density + 0.5f));
131             v.setCompoundDrawablesWithIntrinsicBounds(r.getDrawable(R.drawable.icon),
132                     null, null, null);
133             return v;
134         }
135     }
136 
137     private static final String[] DATA_LIST = {
138             "Afghanistan", "Albania", "Algeria", "American Samoa", "Andorra",
139             "Angola", "Anguilla", "Antarctica", "Antigua and Barbuda", "Argentina",
140             "Armenia", "Aruba", "Australia", "Austria", "Azerbaijan",
141             "Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium",
142             "Belize", "Benin", "Bermuda", "Bhutan", "Bolivia",
143             "Bosnia and Herzegovina", "Botswana", "Bouvet Island", "Brazil",
144             "British Indian Ocean Territory", "British Virgin Islands", "Brunei", "Bulgaria",
145             "Burkina Faso", "Burundi", "Cote d'Ivoire", "Cambodia", "Cameroon", "Canada", "Cape Verde",
146             "Cayman Islands", "Central African Republic", "Chad", "Chile", "China",
147             "Christmas Island", "Cocos (Keeling) Islands", "Colombia", "Comoros", "Congo",
148             "Cook Islands", "Costa Rica", "Croatia", "Cuba", "Cyprus", "Czech Republic",
149             "Democratic Republic of the Congo", "Denmark", "Djibouti", "Dominica", "Dominican Republic",
150             "East Timor", "Ecuador", "Egypt", "El Salvador", "Equatorial Guinea", "Eritrea",
151             "Estonia", "Ethiopia", "Faeroe Islands", "Falkland Islands", "Fiji", "Finland",
152             "Former Yugoslav Republic of Macedonia", "France", "French Guiana", "French Polynesia",
153             "French Southern Territories", "Gabon", "Georgia", "Germany", "Ghana", "Gibraltar",
154             "Greece", "Greenland", "Grenada", "Guadeloupe", "Guam", "Guatemala", "Guinea", "Guinea-Bissau",
155             "Guyana", "Haiti", "Heard Island and McDonald Islands", "Honduras", "Hong Kong", "Hungary",
156             "Iceland", "India", "Indonesia", "Iran", "Iraq", "Ireland", "Israel", "Italy", "Jamaica",
157             "Japan", "Jordan", "Kazakhstan", "Kenya", "Kiribati", "Kuwait", "Kyrgyzstan", "Laos",
158             "Latvia", "Lebanon", "Lesotho", "Liberia", "Libya", "Liechtenstein", "Lithuania", "Luxembourg",
159             "Macau", "Madagascar", "Malawi", "Malaysia", "Maldives", "Mali", "Malta", "Marshall Islands",
160             "Martinique", "Mauritania", "Mauritius", "Mayotte", "Mexico", "Micronesia", "Moldova",
161             "Monaco", "Mongolia", "Montserrat", "Morocco", "Mozambique", "Myanmar", "Namibia",
162             "Nauru", "Nepal", "Netherlands", "Netherlands Antilles", "New Caledonia", "New Zealand",
163             "Nicaragua", "Niger", "Nigeria", "Niue", "Norfolk Island", "North Korea", "Northern Marianas",
164             "Norway", "Oman", "Pakistan", "Palau", "Panama", "Papua New Guinea", "Paraguay", "Peru",
165             "Philippines", "Pitcairn Islands", "Poland", "Portugal", "Puerto Rico", "Qatar",
166             "Reunion", "Romania", "Russia", "Rwanda", "Sqo Tome and Principe", "Saint Helena",
167             "Saint Kitts and Nevis", "Saint Lucia", "Saint Pierre and Miquelon",
168             "Saint Vincent and the Grenadines", "Samoa", "San Marino", "Saudi Arabia", "Senegal",
169             "Seychelles", "Sierra Leone", "Singapore", "Slovakia", "Slovenia", "Solomon Islands",
170             "Somalia", "South Africa", "South Georgia and the South Sandwich Islands", "South Korea",
171             "Spain", "Sri Lanka", "Sudan", "Suriname", "Svalbard and Jan Mayen", "Swaziland", "Sweden",
172             "Switzerland", "Syria", "Taiwan", "Tajikistan", "Tanzania", "Thailand", "The Bahamas",
173             "The Gambia", "Togo", "Tokelau", "Tonga", "Trinidad and Tobago", "Tunisia", "Turkey",
174             "Turkmenistan", "Turks and Caicos Islands", "Tuvalu", "Virgin Islands", "Uganda",
175             "Ukraine", "United Arab Emirates", "United Kingdom",
176             "United States", "United States Minor Outlying Islands", "Uruguay", "Uzbekistan",
177             "Vanuatu", "Vatican City", "Venezuela", "Vietnam", "Wallis and Futuna", "Western Sahara",
178             "Yemen", "Yugoslavia", "Zambia", "Zimbabwe"
179     };
180 }
181