ClipRegion2Activity.java revision b7b93e00893f5c690a96bd3e0e10583bc5721f83
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
17package com.android.test.hwui;
18
19import android.app.Activity;
20import android.content.Context;
21import android.graphics.Canvas;
22import android.graphics.Region;
23import android.os.Bundle;
24import android.widget.FrameLayout;
25import android.widget.TextView;
26
27@SuppressWarnings({"UnusedDeclaration"})
28public class ClipRegion2Activity extends Activity {
29    @Override
30    protected void onCreate(Bundle savedInstanceState) {
31        super.onCreate(savedInstanceState);
32
33        final RegionView group = new RegionView(this);
34
35        final TextView text = new TextView(this);
36        text.setText(buildText());
37        group.addView(text);
38
39        setContentView(group);
40    }
41
42    private static CharSequence buildText() {
43        StringBuffer buffer = new StringBuffer();
44        for (int i = 0; i < 10; i++) {
45            buffer.append(LOREM_IPSUM);
46        }
47        return buffer;
48    }
49
50    public static class RegionView extends FrameLayout {
51        private Region mRegion = new Region();
52        private float mClipPosition = 0.0f;
53
54        public RegionView(Context c) {
55            super(c);
56        }
57
58        public float getClipPosition() {
59            return mClipPosition;
60        }
61
62        public void setClipPosition(float clipPosition) {
63            mClipPosition = clipPosition;
64            invalidate();
65        }
66
67        @Override
68        protected void dispatchDraw(Canvas canvas) {
69
70            canvas.save();
71
72            mRegion.set(0, 0, getWidth(), getHeight());
73            mRegion.op(getWidth() / 4, getHeight() / 4, 3 * getWidth() / 4, 3 * getHeight() / 4,
74                    Region.Op.DIFFERENCE);
75
76            canvas.clipRegion(mRegion);
77            super.dispatchDraw(canvas);
78
79            canvas.restore();
80        }
81    }
82
83    private static final String LOREM_IPSUM = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed sagittis molestie aliquam. Donec metus metus, laoreet nec sagittis vitae, ultricies sit amet eros. Suspendisse sed massa sit amet felis consectetur gravida. In vitae erat mi, in egestas nisl. Phasellus quis ipsum massa, at scelerisque arcu. Nam lectus est, pellentesque eget lacinia non, congue vitae augue. Aliquam erat volutpat. Pellentesque bibendum tincidunt viverra. Aliquam erat volutpat. Maecenas pretium vulputate placerat. Nulla varius elementum rutrum. Aenean mollis blandit imperdiet. Pellentesque interdum fringilla ligula.";
84}
85