Slide 1

Slide 1 text

CONSTRAINT LAYOUT 2.0 Nicolas Roard & John Hoford @camaelon @johnhoford (Sneak peek)

Slide 2

Slide 2 text

BUT FIRST…

Slide 3

Slide 3 text

CL 1.1 1.1

Slide 4

Slide 4 text

OVERVIEW… goo.gl/txuisJ

Slide 5

Slide 5 text

CONSTRAINT LAYOUT 1.1 ➤ Circular Constraints ➤ Helper objects ➤ Optimizer ➤ many bug fixes!

Slide 6

Slide 6 text

ConstraintLayout 2.0 preview GOOGLE MAVEN REPOSITORY dependencies { compile 'com.android.support.constraint:constraint-layout:1.1.0' }

Slide 7

Slide 7 text

BARRIERS ➤ A barrier takes the minimum or maximum of a set of widget ➤ Other widgets can then be constrained to it ➤ Allows to build more flexible layouts Helper object

Slide 8

Slide 8 text

BARRIERS

Slide 9

Slide 9 text

BARRIERS

Slide 10

Slide 10 text

BARRIERS

Slide 11

Slide 11 text

BARRIERS

Slide 12

Slide 12 text

BARRIERS

Slide 13

Slide 13 text

BARRIERS

Slide 14

Slide 14 text

Barriers

Slide 15

Slide 15 text

Barriers

Slide 16

Slide 16 text

Barriers 1 2 3

Slide 17

Slide 17 text

Barriers 1 2 3 4

Slide 18

Slide 18 text

Barriers

Slide 19

Slide 19 text

Barriers

Slide 20

Slide 20 text

OPTIMIZER ➤ New optimizer in 1.1 ➤ A lot more powerful :) ➤ Multiple optimizations passes available ➤ Direct & Center connections ➤ Barriers ➤ Chains (experimental) ➤ Dimensions (experimental)

Slide 21

Slide 21 text

OPTIMIZER CONSTRAINTS OPTIMIZER SOLVER MEASURES

Slide 22

Slide 22 text

OPTIMIZER Widget A

Slide 23

Slide 23 text

OPTIMIZER Widget A

Slide 24

Slide 24 text

OPTIMIZER Widget A

Slide 25

Slide 25 text

OPTIMIZER Widget A

Slide 26

Slide 26 text

OPTIMIZER Widget A Widget B

Slide 27

Slide 27 text

OPTIMIZER Widget A Widget B

Slide 28

Slide 28 text

OPTIMIZER Widget A Widget B

Slide 29

Slide 29 text

OPTIMIZER

Slide 30

Slide 30 text

OPTIMIZER

Slide 31

Slide 31 text

OPTIMIZER ➤ Direct ➤ Barriers ➤ Chains (experimental) ➤ Dimensions (experimental)

Slide 32

Slide 32 text

CHAINS OPTIMIZER ➤ Apply if all elements have fixed dimensions ➤ Or if all elements are (simple) match_constraint ➤ …more to come

Slide 33

Slide 33 text

DIMENSIONS OPTIMIZER match_constraint match_constraint 1 match_constraint fixed / wrap_content 2 fixed / wrap_content fixed / wrap_content 1

Slide 34

Slide 34 text

DIMENSIONS OPTIMIZER Match_Constraint

Slide 35

Slide 35 text

ConstraintLayout 2.0 preview OPTIMIZER ATTRIBUTE app:layout_optimizationLevel=“standard” app:layout_optimizationLevel=“direct|barriers|chains|dimensions”

Slide 36

Slide 36 text

CONSTRAINT LAYOUT 2.0Preview

Slide 37

Slide 37 text

Slide 38

Slide 38 text

CL 2.0 WHY?

Slide 39

Slide 39 text

CL 2.0 UI : RAISING THE BAR

Slide 40

Slide 40 text

CL 2.0 HELP!

Slide 41

Slide 41 text

CL 2.0 VOCABULARY

Slide 42

Slide 42 text

CL 2.0 HELPERS

Slide 43

Slide 43 text

WHAT’S A HELPER ➤ Not necessarily part of your view hierarchy ➤ Helps you specify and build your UI ➤ example: Guideline, Barrier

Slide 44

Slide 44 text

THREE MAIN CATEGORIES OF HELPERS Layout Manipulation Rendering or Decorating Post-Layout Manipulation

Slide 45

Slide 45 text

CONSTRAINT HELPERS ➤ Keep a reference to existing views ➤ Flat hierarchy ➤ Views can be referenced by multiple helpers ➤ Essentially, it allows you to tag views, setting specific behaviors

Slide 46

Slide 46 text

API ➤ Access a list of views ➤ pre/post layouts callbacks ➤ normal views ➤ already used ➤ barriers ➤ groups

Slide 47

Slide 47 text

TOOLING ➤ Integrated manipulation in the component tree (like for barriers) ➤ Exposed in the Palette ➤ Support 3rd party libraries ➤ exposing their custom views / helpers

Slide 48

Slide 48 text

TOOL INTEGRATION

Slide 49

Slide 49 text

TOOL INTEGRATION

Slide 50

Slide 50 text

TOOL INTEGRATION

Slide 51

Slide 51 text

TOOL INTEGRATION

Slide 52

Slide 52 text

TOOL INTEGRATION

Slide 53

Slide 53 text

TOOL INTEGRATION

Slide 54

Slide 54 text

TOOL INTEGRATION

Slide 55

Slide 55 text

TOOL INTEGRATION

Slide 56

Slide 56 text

TOOL INTEGRATION

Slide 57

Slide 57 text

TOOL INTEGRATION

Slide 58

Slide 58 text

EXAMPLE : COLLECTION HELPER ➤ Trampoline object ➤ forward function calls to referenced views ➤ setVisibility ➤ setAlpha ➤ setRotation, etc. ➤ no getter!

Slide 59

Slide 59 text

CL 2.0 DECORATORS

Slide 60

Slide 60 text

DECORATORS ➤ Can reference views ➤ Easily build decorators that depends on views location / visibility / etc

Slide 61

Slide 61 text

ConstraintLayout 2.0 preview METABALLS public class MetaballsDecorator extends ConstraintHelper { public void updatePostLayout(ConstraintLayout container) { int[] ids = getReferencedIds(); final int count = ids.length; for (int i = 0; i < count; i++) { View view = container.getViewById(ids[i]); // do something } } @Override public void onDraw(Canvas canvas) { // do something } }

Slide 62

Slide 62 text

Decorator Canvas ImageViews

Slide 63

Slide 63 text

Decorator Canvas ImageViews

Slide 64

Slide 64 text

Decorator Canvas ImageViews

Slide 65

Slide 65 text

Metaballs Decorator

Slide 66

Slide 66 text

Metaballs Decorator

Slide 67

Slide 67 text

Metaballs Decorator

Slide 68

Slide 68 text

Metaballs Decorator

Slide 69

Slide 69 text

LAYER DECORATOR ➤ Manipulate graphically a collection of views ➤ Supports transforms, etc. ➤ Can be set as the bounding box of the referenced views

Slide 70

Slide 70 text

ConstraintLayout 2.0 preview LET’S CREATE A DRAWABLE…

Slide 71

Slide 71 text

ConstraintLayout 2.0 preview LAYER DECORATOR

Slide 72

Slide 72 text

ConstraintLayout 2.0 preview LAYER DECORATOR

Slide 73

Slide 73 text

ConstraintLayout 2.0 preview LAYER DECORATOR

Slide 74

Slide 74 text

ConstraintLayout 2.0 preview LAYER DECORATOR

Slide 75

Slide 75 text

ConstraintLayout 2.0 preview LAYER DECORATOR

Slide 76

Slide 76 text

ConstraintLayout 2.0 preview LAYER DECORATOR

Slide 77

Slide 77 text

ConstraintLayout 2.0 preview LAYER DECORATOR

Slide 78

Slide 78 text

ConstraintLayout 2.0 preview LAYER DECORATOR

Slide 79

Slide 79 text

ConstraintLayout 2.0 preview LAYERS : PROGRAMMATIC ACCESS Layer layer = findViewById(R.id.layer); layer.setRotation(angle); layer.setScaleX(1+(180 - Math.abs(angle-180))/20f); layer.setScaleY(1+(180 - Math.abs(angle-180))/20f); float shift_x = 500 * (float) Math.sin(Math.toRadians(angle*5)); float shift_y = 500 * (float) Math.sin(Math.toRadians(angle*7)); layer.setTranslationX(shift_x); layer.setTranslationY(shift_y);

Slide 80

Slide 80 text

ConstraintLayout 2.0 preview LAYERS : PROGRAMMATIC ACCESS Layer layer = findViewById(R.id.layer); layer.setRotation(angle); layer.setScaleX(1+(180 - Math.abs(angle-180))/20f); layer.setScaleY(1+(180 - Math.abs(angle-180))/20f); float shift_x = 500 * (float) Math.sin(Math.toRadians(angle*5)); float shift_y = 500 * (float) Math.sin(Math.toRadians(angle*7)); layer.setTranslationX(shift_x); layer.setTranslationY(shift_y);

Slide 81

Slide 81 text

ConstraintLayout 2.0 preview FLY-IN DECORATOR

Slide 82

Slide 82 text

ConstraintLayout 2.0 preview FLY-IN DECORATOR

Slide 83

Slide 83 text

ConstraintLayout 2.0 preview FLY-IN DECORATOR

Slide 84

Slide 84 text

ConstraintLayout 2.0 preview FLY-IN DECORATOR

Slide 85

Slide 85 text

ConstraintLayout 2.0 preview FLY-IN DECORATOR

Slide 86

Slide 86 text

ConstraintLayout 2.0 preview FLY-IN DECORATOR

Slide 87

Slide 87 text

ConstraintLayout 2.0 preview FLY-IN DECORATOR

Slide 88

Slide 88 text

ConstraintLayout 2.0 preview FLY-IN DECORATOR

Slide 89

Slide 89 text

ConstraintLayout 2.0 preview FLY IN DECORATOR: XML

Slide 90

Slide 90 text

ConstraintLayout 2.0 preview FLY IN DECORATOR @Override public void updatePostLayout(ConstraintLayout container) { update(); } void update(){ if (mContainer == null) { return; } mComputedCenterX = Float.NaN; mComputedCenterY = Float.NaN; View[] views = getViews(mContainer); calcCenters(); float shift = myFlyValue-1; for (int i = 0; i < mCount; i++) { View view = views[i]; int x = (view.getLeft() + view.getRight()) / 2; int y = (view.getTop() + view.getBottom()) / 2; view.setTranslationX((x - mComputedCenterX)*shift); view.setTranslationY((y - mComputedCenterY)*shift); } }

Slide 91

Slide 91 text

ConstraintLayout 2.0 preview FLY IN DECORATOR @Override public void updatePreLayout(ConstraintLayout container) { if (mContainer!=container) { setFlyIn(10); ObjectAnimator.ofFloat(this, "FlyIn", 1f) .setDuration(1000).start(); } mContainer = container; } public void setFlyIn(float flyIn) { myFlyValue = flyIn; update(); }

Slide 92

Slide 92 text

ConstraintLayout 2.0 preview (BONUS CODE) public void calcCenters() { if (!(Float.isNaN(mComputedCenterX) || Float.isNaN(mComputedCenterY))) { return; } if (Float.isNaN(mCenterX) || Float.isNaN(mCenterY)) { int minx = Integer.MAX_VALUE, miny= Integer.MAX_VALUE; int maxx= Integer.MIN_VALUE,maxy= Integer.MIN_VALUE; View []views = getViews(mContainer); for (int i = 0; i < mCount; i++) { View view = views[i]; minx = Math.min(minx, view.getLeft()); miny = Math.min(miny, view.getTop()); maxx = Math.max(maxx, view.getRight()); maxy = Math.max(maxy, view.getBottom()); } mComputedCenterX = (Float.isNaN(mCenterX))?(minx + maxx) / 2:mCenterX; mComputedCenterY = (Float.isNaN(mCenterY))?(miny + maxy) / 2:mCenterY; } else { mComputedCenterY = mCenterY; mComputedCenterX = mCenterX; } }

Slide 93

Slide 93 text

ConstraintLayout 2.0 preview CIRCULAR REVEAL

Slide 94

Slide 94 text

ConstraintLayout 2.0 preview CIRCULAR REVEAL

Slide 95

Slide 95 text

ConstraintLayout 2.0 preview CIRCULAR REVEAL

Slide 96

Slide 96 text

ConstraintLayout 2.0 preview CIRCULAR REVEAL

Slide 97

Slide 97 text

ConstraintLayout 2.0 preview CIRCULAR REVEAL

Slide 98

Slide 98 text

ConstraintLayout 2.0 preview CIRCULAR REVEAL

Slide 99

Slide 99 text

ConstraintLayout 2.0 preview CIRCULAR REVEAL

Slide 100

Slide 100 text

ConstraintLayout 2.0 preview CIRCULAR REVEAL

Slide 101

Slide 101 text

ConstraintLayout 2.0 preview CIRCULAR REVEAL

Slide 102

Slide 102 text

ConstraintLayout 2.0 preview CIRCULAR REVEAL

Slide 103

Slide 103 text

ConstraintLayout 2.0 preview CIRCULAR REVEAL @Override public void updatePostLayout(ConstraintLayout container) { super.updatePostLayout(container); if (mContainer != container) { int rad =(int) Math.hypot(mComputedMaxY- mComputedMinY,mComputedMaxX-mComputedMinX); Animator anim = ViewAnimationUtils.createCircularReveal(this, (int)mComputedCenterX-getLeft(), (int)mComputedCenterY-getTop(), 0, rad); anim.setDuration(2000); anim.start(); } mContainer = container; }

Slide 104

Slide 104 text

ALL TOGETHER…

Slide 105

Slide 105 text

No content

Slide 106

Slide 106 text

No content

Slide 107

Slide 107 text

No content

Slide 108

Slide 108 text

No content

Slide 109

Slide 109 text

No content

Slide 110

Slide 110 text

No content

Slide 111

Slide 111 text

No content

Slide 112

Slide 112 text

No content

Slide 113

Slide 113 text

No content

Slide 114

Slide 114 text

No content

Slide 115

Slide 115 text

No content

Slide 116

Slide 116 text

No content

Slide 117

Slide 117 text

No content

Slide 118

Slide 118 text

No content

Slide 119

Slide 119 text

No content

Slide 120

Slide 120 text

No content

Slide 121

Slide 121 text

No content

Slide 122

Slide 122 text

No content

Slide 123

Slide 123 text

No content

Slide 124

Slide 124 text

No content

Slide 125

Slide 125 text

No content

Slide 126

Slide 126 text

No content

Slide 127

Slide 127 text

No content

Slide 128

Slide 128 text

No content

Slide 129

Slide 129 text

No content

Slide 130

Slide 130 text

SUMMARY : HELPERS ➤ Tag your views with behavior ➤ Encapsulate behavior ➤ Declaratively use them ➤ Tool integration

Slide 131

Slide 131 text

CL 2.0 VIRTUAL LAYOUTS

Slide 132

Slide 132 text

VIRTUAL LAYOUTS ➤ Layout the referenced views ➤ Still keep flat hierarchy ➤ Linear ➤ Flow

Slide 133

Slide 133 text

LINEAR ➤ Create horizontal or vertical chains ➤ Somewhat similar to linear layout, but as a helper

Slide 134

Slide 134 text

ConstraintLayout 2.0 preview LINEAR

Slide 135

Slide 135 text

ConstraintLayout 2.0 preview LINEAR

Slide 136

Slide 136 text

ConstraintLayout 2.0 preview

Slide 137

Slide 137 text

ConstraintLayout 2.0 preview LINEAR

Slide 138

Slide 138 text

FLOW ➤ Implements FlexboxLayout-like semantics ➤ Overflow elements will be pushed to the next row ➤ Still flat layout! ➤ Able to position outside elements relative to the ones in Flow

Slide 139

Slide 139 text

ConstraintLayout 2.0 preview FLOW

Slide 140

Slide 140 text

ConstraintLayout 2.0 preview REFERENCE OTHER VIEWS

Slide 141

Slide 141 text

FLOW

Slide 142

Slide 142 text

ConstraintLayout 2.0 preview FLOW

Slide 143

Slide 143 text

ConstraintLayout 2.0 preview FLOW

Slide 144

Slide 144 text

ConstraintLayout 2.0 preview FLOW

Slide 145

Slide 145 text

ConstraintLayout 2.0 preview FLOW

Slide 146

Slide 146 text

ConstraintLayout 2.0 preview FLOW

Slide 147

Slide 147 text

ConstraintLayout 2.0 preview FLOW

Slide 148

Slide 148 text

CL 2.0 LAYOUT MANAGEMENT

Slide 149

Slide 149 text

ConstraintLayout 2.0 preview MANAGING STATES

Slide 150

Slide 150 text

ConstraintLayout 2.0 preview MANAGING STATES

Slide 151

Slide 151 text

ConstraintLayout 2.0 preview MANAGING STATES

Slide 152

Slide 152 text

ConstraintLayout 2.0 preview MANAGING STATES

Slide 153

Slide 153 text

ConstraintLayout 2.0 preview MANAGING STATES

Slide 154

Slide 154 text

ConstraintLayout 2.0 preview MANAGING STATES

Slide 155

Slide 155 text

ConstraintLayout 2.0 preview STATE XML

Slide 156

Slide 156 text

ConstraintLayout 2.0 preview JAVA-SIDE @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.layout); cl = findViewById(R.id.root); cl.setLayoutDescription(R.xml.layout_states); }

Slide 157

Slide 157 text

ConstraintLayout 2.0 preview JAVA-SIDE @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.layout); cl = findViewById(R.id.root); cl.setLayoutDescription(R.xml.layout_states); } public void change(View v) { cl.setState(closed ? R.id.large : R.id.small); closed = !closed; }

Slide 158

Slide 158 text

ConstraintLayout 2.0 preview STATE XML : SIZE QUALIFIERS

Slide 159

Slide 159 text

ConstraintLayout 2.0 preview ON CONFIGURATION CHANGE @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); cl.setState(newConfig.screenWidthDp, newConfig.screenHeightDp); }

Slide 160

Slide 160 text

ConstraintLayout 2.0 preview LIVE RESIZE

Slide 161

Slide 161 text

ConstraintLayout 2.0 preview LIVE RESIZE

Slide 162

Slide 162 text

ConstraintLayout 2.0 preview LIVE RESIZE

Slide 163

Slide 163 text

ConstraintLayout 2.0 preview LIVE RESIZE

Slide 164

Slide 164 text

ConstraintLayout 2.0 preview LIVE RESIZE

Slide 165

Slide 165 text

ConstraintLayout 2.0 preview LIVE RESIZE

Slide 166

Slide 166 text

ConstraintLayout 2.0 preview CALLBACK cl.setOnConstraintsChanged(new ConstraintsChangedListener() { @Override public void preLayoutChange(int state,int layoutId) { TransitionManager.beginDelayedTransition(cl); } });

Slide 167

Slide 167 text

ConstraintLayout 2.0 preview ANIMATED LIVE RESIZE (TRANSITION MANAGER)

Slide 168

Slide 168 text

ConstraintLayout 2.0 preview ANIMATED LIVE RESIZE (TRANSITION MANAGER)

Slide 169

Slide 169 text

ConstraintLayout 2.0 preview ANIMATED LIVE RESIZE (TRANSITION MANAGER)

Slide 170

Slide 170 text

ConstraintLayout 2.0 preview ANIMATED LIVE RESIZE (TRANSITION MANAGER)

Slide 171

Slide 171 text

ConstraintLayout 2.0 preview ANIMATED LIVE RESIZE (TRANSITION MANAGER)

Slide 172

Slide 172 text

ConstraintLayout 2.0 preview ANIMATED LIVE RESIZE (TRANSITION MANAGER)

Slide 173

Slide 173 text

LAYOUT MANAGEMENT ➤ Centralize multiple layouts in a single XML ➤ Allows to easily initialize and deal with different representations for your layout ➤ Size qualifiers & live resize

Slide 174

Slide 174 text

CL 2.0 FLUENT API

Slide 175

Slide 175 text

FLUENT API ➤ ConstraintSet: great to capture full state and manage it ➤ …But not that ideal for direct manipulation ➤ Fluent API to change layout params directly

Slide 176

Slide 176 text

ConstraintLayout 2.0 preview FLUENT API new Constraints(view).margin(ConstraintProperties.TOP,23).apply(); Constraints.on(view) .margin(TOP, 16) .margin(BOTTOM, 16) .margin(LEFT, 16) .margin(RIGHT, 16) .apply(); Constraints.on(view) .center(R.id.button1, LEFT, 23, R.id.button2, RIGHT, 23,.05f) .connect(TOP,PARENT_ID, TOP, 32) .apply();

Slide 177

Slide 177 text

ConstraintLayout 2.0 preview EXAMPLE : CENTER Constraints.on(myView) .center(R.id.button1, LEFT, R.id.button2, RIGHT) .horizontalBias(.5f) .top(PARENT_ID,TOP,32) .apply();

Slide 178

Slide 178 text

ConstraintLayout 2.0 preview center(int firstID, int firstSide, int firstMargin, int secondId, int secondSide, int secondMargin, f centerHorizontally(int leftId, int leftSide, int leftMargin, int rightId, int rightSide, int rightMar centerHorizontallyRtl(int startId, int startSide, int startMargin, int endId, int endSide, int endMar centerVertically(int topId, int topSide, int topMargin, int bottomId, int bottomSide, int bottomMargi centerHorizontally(int toView) centerHorizontallyRtl(int toView) centerVertically(int toView) removeConstraints(int anchor) margin(int anchor, int value) goneMargin(int anchor, int value) horizontalBias(float bias) verticalBias(float bias) dimensionRatio(String ratio) visibility(int visibility) alpha(float alpha) elevation(float elevation) rotation(float rotation) rotationX(float rotationX) rotationY(float rotationY) scaleX(float scaleX) scaleY(float scaleY) translationX(float translationX) translationY(float translationY) CONSTRAINTS API…

Slide 179

Slide 179 text

ConstraintLayout 2.0 preview visibility(int visibility) alpha(float alpha) elevation(float elevation) rotation(float rotation) rotationX(float rotationX) rotationY(float rotationY) scaleX(float scaleX) scaleY(float scaleY) translationX(float translationX) translationY(float translationY) translation(float translationX, float translationY) translationZ(float translationZ) constrainHeight(int height) constrainWidth(int width) constrainMaxHeight(int height) constrainMaxWidth(int width) constrainMinHeight(int height) constrainMinWidth(int width) constrainDefaultHeight(int height) constrainDefaultWidth(int width) horizontalWeight(float weight) verticalWeight(float weight) horizontalChainStyle(int chainStyle) verticalChainStyle(int chainStyle) addToHorizontalChain(int leftId, int rightId) addToHorizontalChainRTL(int leftId, int rightId) addToVerticalChain(int topId, int bottomId) removeFromVerticalChain() removeFromHorizontalChain() connect(int startSide, int endID, int endSide, int margin) CONSTRAINTS API…

Slide 180

Slide 180 text

CONSTRAINT LAYOUT 2.0 - SUMMARY ➤ Virtual Layouts ➤ Layers & Transitions ➤ Decorators ➤ State management & Live Resize ➤ Fluent API for LayoutParams ➤ …more to come!

Slide 181

Slide 181 text

THANKS! Nicolas Roard & John Hoford @camaelon @johnhoford

Slide 182

Slide 182 text

CL 2.0 TO BE CONTINUED… … at Google IO 2018!

Slide 183

Slide 183 text

DOCUMENTATION ➤ http://www.constraintlayout.com ➤ https://developer.android.com/reference/android/support/ constraint/package-summary.html ➤ https://developer.android.com/training/constraint-layout/ index.html ➤ https://codelabs.developers.google.com/codelabs/constraint-layout ➤ https://medium.com/google-developers/building-interfaces-with- constraintlayout-3958fa38a9f7