KEMBAR78
Creating custom views | PDF
Creating Custom Views

Kewang
A custom view should be...


                         2
A custom view should be...

   Conformity
   XMLify
   Accessibility
   Compatibility
                             3
Let's implement

              4
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);      Constructors
    private void init(Context context, AttributeSet attrs) {
    }
}
                                                           5
<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
                                                    6
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/androi
d"
xmlns:custom="http://schemas.android.com/apk/res/com.exa
mple.customview" >

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

</LinearLayout>
                    /res/layout/main.xml
                                                       7
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
                                                       8
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
                                                       9
onDraw()

           10
What to draw? Canvas
How to draw? Paint


                       11
before onDraw(),
should onMeasure()


                     12
@Override
protected 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
                                               13
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()

                                                         14
then invalidate()


                    15
Interactive

              16
View
     boolean onTouchEvent()

ACTION_DOWN
 All TouchEvent: return true;
 onClick(): return false;

                                17
ViewGroup
 boolean onInterceptTouchEvent()

 ACTION_DOWN
  handle self: return true;
  pass to child: return false;

                                   18
Derive from onTouchEvent()


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

                                 19
Optimizing

             20
Do Less, Less Frequently


     onDraw()
     invalidate()
     requestLayout()


                           21
onDraw()



eliminate allocations


                        22
invalidate()


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

                             23
requestLayout()


if has complex UI
  custom ViewGroup

                     24
References


Creating a View Class
Custom Drawing
Making the View Interactive
Optimizing the View

                              25
26

Creating custom views