creating custom views

26
Creating Custom Views Kewang

Upload: mu-chun-wang

Post on 31-May-2015

776 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Creating custom views

Creating Custom Views

Kewang

Page 2: Creating custom views

2

A custom view should be...

Page 3: Creating custom views

3

A custom view should be...

ConformityXMLifyAccessibilityCompatibility

Page 4: Creating custom views

4

Let's implement

Page 5: Creating custom views

5

public class PieChart extends View { public PieChart(Context context, AttributeSet attrs) { super(context, attrs);

init(context, attrs); }

public PieChart(Context context) { super(context);

init(context, null); }

private void init(Context context, AttributeSet attrs) { }}

Constructors

Page 6: Creating custom views

6

<resources>

<declare-styleable name="PieChart"> <attr name="showText" format="boolean" /> <attr name="labelPosition" format="enum"> <enum name="left" value="0" /> <enum name="right" value="1" /> </attr> </declare-styleable>

</resources>

/res/values/attrs.xml

Page 7: Creating custom views

7

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:custom="http://schemas.android.com/apk/res/com.example.customview" >

<com.example.customview.PieChart custom:labelPosition="left" custom:showText="true" />

</LinearLayout>

/res/layout/main.xml

Page 8: Creating custom views

8

private void init(Context context, AttributeSet attrs) { TypedArray a = context.getTheme().obtainStyledAttributes(attrs,R.styleable.PieChart, 0, 0);

try { mShowText = a.getBoolean(R.styleable.PieChart_showText, false); mTextPos = a.getInteger(R.styleable.PieChart_labelPosition, 0); } finally { a.recycle(); }} The author's initialization

Page 9: Creating custom views

9

private void init(Context context, AttributeSet attrs) { if (attrs != null) { TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.PieChart);

mShowText = a.getBoolean(R.styleable.PieChart_showText, false); mTextPos = a.getInteger(R.styleable.PieChart_labelPosition, 0);

a.recycle(); }} My initialization

Page 10: Creating custom views

10

onDraw()

Page 11: Creating custom views

11

What to draw? Canvas

How to draw? Paint

Page 12: Creating custom views

12

before onDraw(), should onMeasure()

Page 13: Creating custom views

13

@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int minw = getPaddingLeft() + getPaddingRight()+ getSuggestedMinimumWidth(); int w = resolveSize(minw, widthMeasureSpec); int h = resolveSize(MeasureSpec.getSize(w), heightMeasureSpec);

setMeasuredDimension(w, h);}

MUST setMeasuredDimension

Page 14: Creating custom views

14

float start = 0.f;for (int i = 0; i < series.size(); i++) { float sweep = (float) (series.get(i) / sum * 360.f);

paint.setColor(Color.rgb(0xff, 0xff, 0xff));

canvas.drawArc(oval, start, sweep, true, paint);

start += sweep;} onDraw()

Page 15: Creating custom views

15

then invalidate()

Page 16: Creating custom views

16

Interactive

Page 17: Creating custom views

17

View

boolean onTouchEvent()

ACTION_DOWNAll TouchEvent: return true;onClick(): return false;

Page 18: Creating custom views

18

ViewGroup

boolean onInterceptTouchEvent()

ACTION_DOWNhandle self: return true;pass to child: return false;

Page 19: Creating custom views

19

onClick(), onLongClick()onDown(), onFling()onLongPress,() onScroll()onShowPress(), onSingleTapUp()

Derive from onTouchEvent()

Page 20: Creating custom views

20

Optimizing

Page 21: Creating custom views

21

Do Less, Less Frequently

onDraw()invalidate()requestLayout()

Page 22: Creating custom views

22

onDraw()

eliminate allocations

Page 23: Creating custom views

23

invalidate()

invalidate(Rect)invalidate(int, int, int, int)

Page 24: Creating custom views

24

requestLayout()

if has complex UI custom ViewGroup

Page 26: Creating custom views

26