Upgrade to Pro — share decks privately, control downloads, hide ads and more …

View into the abyss

Mori Masaki
February 19, 2016

View into the abyss

Mori Masaki

February 19, 2016
Tweet

Other Decks in Technology

Transcript

  1. VIEW “A View occupies a rectangular area on the screen

    and is responsible for drawing and event handling” http://developer.android.com/intl/ja/ reference/android/view/View.html
  2. FrameLayout LinearLayout Button ImageView TextView RelativeLayout Button ViewRootImpl measure, measure,

    measure, measure… measure, measure measure, measure measure measure, measure Determine sizes of all Views
  3. HOW TO CUSTOM @Override void onMeasure(int widthMeasureSpec, int heightMeasureSpec) @Override

    void onLayout(boolean changed, int l, int t, int r, int b) (for ViewGroup)
  4. public class AdFooter extends LinearLayout { @Override protected void onMeasure(int

    widthMeasureSpec, int heightMeasureSpec) { switchLayoutHorizontal(); super.onMeasure(widthMeasureSpec, heightMeasureSpec); if (isOverflowed()) { switchLayoutVertical(); super.onMeasure(widthMeasureSpec, heightMeasureSpec); } } } Responsive LinearLayout
  5. removeView(oldView); addView(view, index, params); removeViewInLayout(oldView); addViewInLayout(view, index, params, true); if

    (!isLayoutRequested()) { view.measure(widthMeasureSpec, heightMeasureSpec); view.layout(left, top, right, bottom); } SLOW FAST Replace child w/o layout
  6. public class LayoutBlockingImageView extends ImageView { private boolean preventLayout; @Override

    public void setImageDrawable(Drawable drawable) { preventLayout = true; super.setImageDrawable(drawable); preventLayout = false; } @Override public void requestLayout() { if (!preventLayout) super.requestLayout(); } } Needless for fixed ImageView
  7. @Override protected boolean drawChild(Canvas canvas, View child, long drawingTime) {

    canvas.save(Canvas.CLIP_SAVE_FLAG); try { if (child == leftView) { canvas.clipRect(0, 0, splitPositionLeft, height); } else if (child == rightView) { canvas.clipRect(splitPositionRight, 0, width, height); } return super.drawChild(canvas, child, drawingTime); } finally { canvas.restore(); } } Clipping child Views