Presentation about Android custom views held on 05.04.2018 on Infinum Android Talks
Code: https://github.com/MatejVukosav/ConceptView
Event:
https://www.facebook.com/events/626682637663384/
https://infinum.co/our-stuff/android-talks/29
11. Why NOT?
- Can be tricky to implement
- Time consuming
- you need to handle everything by yourself
- style, font, text size, rendering on different screens, scaling, aspect ratio, zoom,
- all kind of click listeners, orientation
12. Why NOT?
- Can be tricky to implement
- Time consuming
- you need to handle everything by yourself
- style, font, text size, rendering on different screens, scaling, aspect ratio, zoom,
- all kind of click listeners, orientation
- required more coding skill
26. How Android draws views?
XML layout -> instantiated -> inflated -> allocated views ->
27. How Android draws views?
XML layout -> instantiated -> inflated -> allocated views -> view hierarchy
28. How Android draws views?
Phases before view hierarchy comes to screen:
1. measure
2. layout
3. draw
29. How Android draws views?
Phases before view hierarchy comes to screen:
1. measure -> onMeasure()
2. layout
3. draw
30. How Android draws views?
Phases before view hierarchy comes to screen:
1. measure -> onMeasure()
2. layout -> onLayout()
3. draw
31. How Android draws views?
Phases before view hierarchy comes to screen:
1. measure -> onMeasure()
2. layout -> onLayout()
3. draw -> onDraw()
32. How Android draws views?
Phases before view hierarchy comes to screen:
1. measure -> onMeasure()
a. measure the view and its content
b. find how big view should be
c. must call setMeasuredDimension( width, height)
2. layout -> onLayout()
3. draw -> onDraw()
33. How Android draws views?
Phases before view hierarchy comes to screen:
1. measure -> onMeasure()
2. layout -> onLayout()
a. assign a size and position to each of its children
3. draw -> onDraw()
34. How Android draws views?
Phases before view hierarchy comes to screen:
1. measure -> onMeasure()
2. layout -> onLayout()
3. draw -> onDraw()
a. draw on canvas
37. Approaches
2 general ways
- extend the existing view class
- Button, TextView, EditText, CheckBox, RadioButton
38. Approaches
2 general ways
- extend the existing view class
- Button, TextView, EditText, CheckBox, RadioButton
- extend the base view class/group class
39. Approaches
2 general ways
- extend the existing view class
- Button, TextView, EditText, CheckBox, RadioButton
- extend the base view class/group class
40. Extend the view base class
- extend View class (or subclass)
import android.view.View;
public class MyCustomView extends View{
...
}
41. Extend the view base class
- extend View class (or subclass)
- constructors
import android.view.View;
public class MyCustomView extends View{
...
}
42. Extend the view base class
- extend View class (or subclass)
- constructors
- 4 of them
import android.view.View;
public class MyCustomView extends View{
}
43. Extend the view base class
- extend View class (or subclass)
- constructors
- 4 of them
- but we only need 2
import android.view.View;
public class MyCustomView extends View{
}
44. Constructors
1. Create new from code - View (Context context)
2. Create from XML - View(Context context, AttributeSet attrs)
3. Create from XML with a style from theme attribute - View(Context
context,AttributeSet atts, int deffStyleAttr)
4. Create from XML with a style from theme attribute or style resource -
View(Context context,AttributeSet atts, int deffStyleAttr, int defStyleRes)
45. Constructors
1. Create new from code - View (Context context)
2. Create from XML - View(Context context, AttributeSet attrs)
3. Create from XML with a style from theme attribute - View(Context
context,AttributeSet atts, int deffStyleAttr)
4. Create from XML with a style from theme attribute or style resource -
View(Context context,AttributeSet atts, int deffStyleAttr, int defStyleRes)
46. Constructors
1. Create new from code - View (Context context)
2. Create from XML - View(Context context, AttributeSet attrs)
3. Create from XML with a style from theme attribute - View(Context
context,AttributeSet atts, int deffStyleAttr)
4. Create from XML with a style from theme attribute or style resource -
View(Context context,AttributeSet atts, int deffStyleAttr, int defStyleRes)
47. Constructors
1. Create new from code - View (Context context)
2. Create from XML - View(Context context, AttributeSet attrs)
3. Create from XML with a style from theme attribute - View(Context
context,AttributeSet atts, int deffStyleAttr)
4. Create from XML with a style from theme attribute or style resource -
View(Context context,AttributeSet atts, int deffStyleAttr, int defStyleRes)
48. Constructors
1. Create new from code - View (Context context)
2. Create from XML - View(Context context, AttributeSet attrs)
3. Create from XML with a style from theme attribute - View(Context
context,AttributeSet atts, int deffStyleAttr)
4. Create from XML with a style from theme attribute or style resource -
View(Context context,AttributeSet atts, int deffStyleAttr, int defStyleRes)
50. Constructors
Don’t chain constructors using this!
public CustomTextView( Context context ) {
this( context, null );
}
public CustomTextView( Context context, AttributeSet attrs ) {
this( context, attrs, 0 );
}
public CustomTextView( Context context, AttributeSet attrs, int defStyleAttr )
{
super( context, attrs, defStyleAttr );
...more code...
}
51. Constructors
Don’t chain constructors using this!
<TextView
android:id="@+id/text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
...
android:text="This is some random irrelevant text."/>
<com.vuki.custom.view.CustomTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/text_view"
...
android:text="This is some random irrelevant text."/>
75. Path
- moveTo()
- lineTo()
- bezier
- quadTo() - quadratic bezier
- cubicTo() - cubic bezier
- clipping path
- op (Path, Op)
- The logical operation that can be performed
when combining two paths.
99. Animations
- ObjectAnimator
- animate with reflection
- ViewPropertyAnimator
- animate some of standard view
properties
- ValueAnimator
- animate some of standar
- AnimatorSet
- chain multiple animators
ObjectAnimator animX =
ObjectAnimator.ofFloat(myView, "x", 50f);
- view must have setter for property
- automatically update the view
100. Animations
- ObjectAnimator
- animate with reflection
- ViewPropertyAnimator
- animate some of standard view
properties
- ValueAnimator
- animate some of standar
- AnimatorSet
- chain multiple animators
someView.animate()
.alpha(1f)
.scaleX(2.f)
.scaleY(1.f);
101. Animations
- ObjectAnimator
- animate with reflection
- ViewPropertyAnimator
- animate some of standard view
properties
- ValueAnimator
- any number of properties
- AnimatorSet
- chain multiple animators
PropertyValuesHolder
- holds information about a property
valueAnimator
.setInterpolator( new LinearInterpolator() )
.setEvaluator( new FloatEvaluator() )
.setDuration( 3000 )
.setRepeatCount( INFINITE )
.setRepeatMode( RESTART)
- must implement update listener
102. Animations
- ObjectAnimator
- animate with reflection
- ViewPropertyAnimator
- animate some of standard view
properties
- ValueAnimator
- any number of properties
- AnimatorSet
- chain multiple animators
PropertyValuesHolder x;
PropertyValuesHolder y;
x=PropertyValuesHolder.ofFloat(“propX”,0,5f)
y=PropertyValuesHolder.ofInt(“propY”,0,30)
valueAnimator.setValues(x,y);
valueAnimator.addUpdateListener({
…..
=animation.getAnimatedValue(“propX”)
=animation.getAnimatedValue(“propY”)
….
})
103. Animations
- ObjectAnimator
- animate with reflection
- ViewPropertyAnimator
- animate some of standard view
properties
- ValueAnimator
- any number of properties
- PropertyValueHolder
- AnimatorSet
- chain multiple animators
animatorSet
.playSequentially( anim1, anim2)
.playTogether( anim1, anim2 )
104. State changed
- requestLayout()
- view update through its lifecycle
- invalidate()
- redraw the view
- invalidate from UI thread
- postInvalidate()
- invalidate from non UI thread