Skip to content

Commit 51ce67d

Browse files
author
Jeremy Walker
committed
Adds ConstraintLayoutExamples samples.
Change-Id: I4a8fe277a65316e114e2d16f84d6b8fae78fbae5
1 parent b70b035 commit 51ce67d

File tree

238 files changed

+13489
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

238 files changed

+13489
-0
lines changed

ConstraintLayoutExamples/.gitignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
*.iml
2+
.gradle
3+
local.properties
4+
/.idea/workspace.xml
5+
/.idea/libraries
6+
.DS_Store
7+
/build
8+
/captures
9+
.externalNativeBuild
10+
.idea/
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
# GOOGLE SAMPLE PACKAGING DATA
3+
#
4+
# This file is used by Google as part of our samples packaging process.
5+
# End users may safely ignore this file. It has no relevance to other systems.
6+
---
7+
status: PUBLISHED
8+
technologies: [Android]
9+
categories: [UI]
10+
languages: [Java]
11+
solutions: [Mobile]
12+
github: android/views-widgets
13+
license: apache2
Lines changed: 243 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,243 @@
1+
/*
2+
* Copyright (C) 2019 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.google.androidstudio.motionlayoutcycles;
18+
19+
import javax.swing.*;
20+
import javax.swing.border.LineBorder;
21+
import java.awt.*;
22+
import java.awt.geom.AffineTransform;
23+
24+
/**
25+
* This panel simulates and Android view parameter for controlling a button
26+
*/
27+
class AnimationPanel extends JPanel {
28+
29+
public static final String[] EASING_OPTIONS = Easing.NAMED_EASING;
30+
private static final boolean RENDERING_STATS = Boolean.parseBoolean(System.getProperty("stats"));
31+
private float mDuration = 20000; // duration in milliseconds
32+
private float myAnimationPercent;
33+
private long myLastTime;
34+
private static final int BUTTON_WIDTH = 123;
35+
private static final int BUTTON_HEIGHT = 40;
36+
private String myTitle = "button";
37+
private JButton myPlayButton;
38+
private boolean myIsPlaying = false;
39+
private Timer myPlayTimer = new Timer(14, e -> step());
40+
private CycleSetModel myCycleModel;
41+
private int myAttributeMask;
42+
private Easing myEasing = Easing.getInterpolator(Easing.STANDARD_NAME);
43+
private float myEasedPercent;
44+
static final Stroke DASH_STROKE = new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER,
45+
1, new float[]{10, 10}, 0);
46+
47+
AnimationPanel(CycleSetModel cycleModel, JButton play) {
48+
super(new FlowLayout(FlowLayout.LEFT));
49+
setBackground(new Color(0xF8F8F4));
50+
setBorder(new LineBorder(new Color(0x888888),1));
51+
myCycleModel = cycleModel;
52+
myPlayButton = play;
53+
myPlayButton.addActionListener(e -> play());
54+
}
55+
56+
public void setEasing(String easing) {
57+
myEasing = Easing.getInterpolator(easing);
58+
}
59+
60+
public void setModel(CycleSetModel cycleModel) {
61+
myCycleModel = cycleModel;
62+
}
63+
64+
public void setMode() {
65+
myAttributeMask = myCycleModel.getAttributeMask();
66+
}
67+
68+
public void play() {
69+
if (myIsPlaying) {
70+
myCycleModel.setDot(Float.NaN);
71+
pause();
72+
myIsPlaying = false;
73+
return;
74+
}
75+
myPlayButton.setText("pause");
76+
myIsPlaying = true;
77+
myAttributeMask = myCycleModel.getAttributeMask();
78+
myPlayTimer.start();
79+
}
80+
81+
int call_count = 0;
82+
int paint_count = 0;
83+
long last_update;
84+
85+
public void step() {
86+
long time = System.currentTimeMillis();
87+
myAnimationPercent += (time - myLastTime) / mDuration;
88+
if (myAnimationPercent > 1.0f) {
89+
myAnimationPercent = 0;
90+
}
91+
myEasedPercent = (float) myEasing.get(myAnimationPercent);
92+
93+
myCycleModel.setDot(myEasedPercent);
94+
call_count++;
95+
repaint();
96+
myLastTime = time;
97+
}
98+
99+
public void pause() {
100+
myPlayButton.setText("play");
101+
myPlayTimer.stop();
102+
}
103+
104+
@Override
105+
public void paint(Graphics g) {
106+
super.paint(g);
107+
paint_count++;
108+
long time = System.currentTimeMillis();
109+
int w = getWidth();
110+
int h = getHeight();
111+
float buttonCX = w / 2.0f;
112+
float buttonCY = h / 2.0f;
113+
float startX = buttonCX;
114+
float startY = buttonCY;
115+
float endX = buttonCX;
116+
float endY = buttonCY;
117+
if (myMoveObject != 0) {
118+
float dx = movement[myMoveObject][0];
119+
float dy = movement[myMoveObject][1];
120+
startX = buttonCX - (dx * w) / 3.0f;
121+
startY = buttonCY - (dy * h) / 3.0f;
122+
endX = buttonCX + (dx * w) / 3.0f;
123+
endY = buttonCY + (dy * h) / 3.0f;
124+
buttonCX = startX + myEasedPercent * (endX - startX);
125+
buttonCY = startY + myEasedPercent * (endY - startY);
126+
127+
}
128+
Graphics2D g2d = (Graphics2D) g.create();
129+
AffineTransform at;
130+
Stroke old = g2d.getStroke();
131+
g2d.setColor(Color.RED);
132+
g2d.setStroke(DASH_STROKE);
133+
g2d.drawLine((int) startX, (int) startY, (int) endX, (int) endY);
134+
g2d.setStroke(old);
135+
Color background = Color.LIGHT_GRAY;
136+
Color border = Color.DARK_GRAY;
137+
Color text = Color.BLACK;
138+
if (myAttributeMask != 0) {
139+
140+
if ((myAttributeMask & (1 << CycleView.Prop.PATH_ROTATE.ordinal())) != 0) {
141+
at = new AffineTransform();
142+
at.rotate(Math.toRadians(myCycleModel.getValue(CycleView.Prop.PATH_ROTATE, myEasedPercent)), buttonCX,
143+
buttonCY);
144+
g2d.transform(at);
145+
}
146+
if ((myAttributeMask & (1 << CycleView.Prop.ALPHA.ordinal())) != 0) {
147+
int alpha = Math
148+
.max(0, Math.min(255, (int) (myCycleModel.getValue(CycleView.Prop.ALPHA, myEasedPercent) * 255)));
149+
background = new Color(background.getRed(), background.getGreen(), background.getBlue(),
150+
alpha);
151+
border = new Color(border.getRed(), border.getGreen(), border.getBlue(), alpha);
152+
text = new Color(text.getRed(), text.getGreen(), text.getBlue(), alpha);
153+
}
154+
155+
if ((myAttributeMask & (1 << CycleView.Prop.SCALE_X.ordinal())) != 0) {
156+
at = new AffineTransform();
157+
at.translate(w / 2.0, buttonCY);
158+
at.scale(myCycleModel.getValue(CycleView.Prop.SCALE_X, myEasedPercent), 1);
159+
at.translate(-w / 2.0, -buttonCY);
160+
161+
g2d.transform(at);
162+
}
163+
164+
if ((myAttributeMask & (1 << CycleView.Prop.SCALE_Y.ordinal())) != 0) {
165+
166+
at = new AffineTransform();
167+
at.translate(buttonCX, buttonCY);
168+
at.scale(1, myCycleModel.getValue(CycleView.Prop.SCALE_Y, myEasedPercent));
169+
at.translate(-buttonCX, -buttonCY);
170+
g2d.transform(at);
171+
}
172+
173+
if ((myAttributeMask & (1 << CycleView.Prop.TRANSLATION_X.ordinal())) != 0) {
174+
175+
at = new AffineTransform();
176+
at.translate(myCycleModel.getValue(CycleView.Prop.TRANSLATION_X, myEasedPercent), 0);
177+
g2d.transform(at);
178+
}
179+
180+
if ((myAttributeMask & (1 << CycleView.Prop.TRANSLATION_Y.ordinal())) != 0) {
181+
182+
at = new AffineTransform();
183+
at.translate(0, myCycleModel.getValue(CycleView.Prop.TRANSLATION_Y, myEasedPercent));
184+
g2d.transform(at);
185+
}
186+
if ((myAttributeMask & (1 << CycleView.Prop.ROTATION.ordinal())) != 0) {
187+
at = new AffineTransform();
188+
at.rotate(Math.toRadians(myCycleModel.getValue(CycleView.Prop.ROTATION, myEasedPercent)), buttonCX,
189+
buttonCY);
190+
g2d.transform(at);
191+
}
192+
193+
}
194+
195+
int px = (int) (0.5 + buttonCX - BUTTON_WIDTH / 2.0f);
196+
int py = (int) (0.5 + buttonCY - BUTTON_HEIGHT / 2.0f);
197+
198+
g2d.setColor(background);
199+
g2d.fillRoundRect(px, py, BUTTON_WIDTH, BUTTON_HEIGHT, 5, 5);
200+
g2d.setColor(border);
201+
g2d.drawRoundRect(px, py, BUTTON_WIDTH, BUTTON_HEIGHT, 5, 5);
202+
int sw = g.getFontMetrics().stringWidth(myTitle);
203+
int fh = g.getFontMetrics().getHeight();
204+
int fa = g.getFontMetrics().getAscent();
205+
g2d.setColor(text);
206+
g2d.drawString(myTitle, px + BUTTON_WIDTH / 2 - sw / 2, py + BUTTON_HEIGHT / 2 + fa - fh / 2);
207+
if (time - last_update > 1000) {
208+
if (RENDERING_STATS) {
209+
210+
System.out.println("render" + 1000 * call_count / (time - last_update) + "fps paint "
211+
+ 1000 * paint_count / (time - last_update) + "fps");
212+
}
213+
last_update = time;
214+
paint_count = 0;
215+
call_count = 0;
216+
}
217+
}
218+
219+
static String[] MOVE_NAMES = {"Stationary", "South to North", "West to East", "SW to NE",
220+
"NW to SE", "SE to NW", "NE to SW"};
221+
static String[] DURATION = {"0.1s", "0.5s", "1.0s", "2.0s", "5s", "10s", "20s"};
222+
static int[] DURATION_VAL = {100, 500, 1000, 2000, 5000, 10000, 20000};
223+
224+
static int[][] movement = {
225+
{0, 0},
226+
{0, -1},
227+
{1, 0},
228+
{1, -1},
229+
{1, 1},
230+
{-1, -1},
231+
{-1, 1},
232+
};
233+
int myMoveObject = 0;
234+
235+
public void setMovement(int move) {
236+
myMoveObject = move;
237+
repaint();
238+
}
239+
240+
public void setDurationIndex(int selectedIndex) {
241+
mDuration = DURATION_VAL[selectedIndex];
242+
}
243+
}

0 commit comments

Comments
 (0)