对于重载的方法,可以使用参数少的去调用参数多的(多出来的参数,使用默认值代替)。
- 代码的重用性,减少了冗余的code
- 单需要改动的时候,只需要改动参数最多的实现,而不是改动每一个方法实现。
- 无论从代码规模还是清晰度,都有很大提高。
一个例子:
public class CellLayout extends ViewGroup { public CellLayout(Context context) { this(context, null); } public CellLayout(Context context, AttributeSet attrs) { this(context, attrs, 0); } public CellLayout(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show // the user where a dragged item will land when dropped. setWillNotDraw(false); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0); mOriginalCellWidth = mCellWidth = a.getDimensionPixelSize(R.styleable.CellLayout_cellWidth, 10); mOriginalCellHeight = mCellHeight = a.getDimensionPixelSize(R.styleable.CellLayout_cellHeight, 10); mWidthGap = mOriginalWidthGap = a.getDimensionPixelSize(R.styleable.CellLayout_widthGap, 0); mHeightGap = mOriginalHeightGap = a.getDimensionPixelSize(R.styleable.CellLayout_heightGap, 0); mMaxGap = a.getDimensionPixelSize(R.styleable.CellLayout_maxGap, 0); mCountX = LauncherModel.getCellCountX(); mCountY = LauncherModel.getCellCountY(); mOccupied = new boolean[mCountX][mCountY]; a.recycle(); setAlwaysDrawnWithCacheEnabled(false); final Resources res = getResources(); mNormalBackground = res.getDrawable(R.drawable.homescreen_blue_normal_holo); mActiveGlowBackground = res.getDrawable(R.drawable.homescreen_blue_strong_holo); mOverScrollLeft = res.getDrawable(R.drawable.overscroll_glow_left); mOverScrollRight = res.getDrawable(R.drawable.overscroll_glow_right); mForegroundPadding = res.getDimensionPixelSize(R.dimen.workspace_overscroll_drawable_padding); mNormalBackground.setFilterBitmap(true); mActiveGlowBackground.setFilterBitmap(true); // Initialize the data structures used for the drag visualization. mCrosshairsDrawable = res.getDrawable(R.drawable.gardening_crosshairs); mEaseOutInterpolator = new DecelerateInterpolator(2.5f); // Quint ease out // Set up the animation for fading the crosshairs in and out int animDuration = res.getInteger(R.integer.config_crosshairsFadeInTime); mCrosshairsAnimator = new InterruptibleInOutAnimator(animDuration, 0.0f, 1.0f); mCrosshairsAnimator.getAnimator().addUpdateListener(new AnimatorUpdateListener() { public void onAnimationUpdate(ValueAnimator animation) { mCrosshairsVisibility = ((Float) animation.getAnimatedValue()).floatValue(); invalidate(); } }); mCrosshairsAnimator.getAnimator().setInterpolator(mEaseOutInterpolator); mDragCell[0] = mDragCell[1] = -1; for (int i = 0; i < mDragOutlines.length; i++) { mDragOutlines[i] = new Point(-1, -1); } // When dragging things around the home screens, we show a green outline of // where the item will land. The outlines gradually fade out, leaving a trail // behind the drag path. // Set up all the animations that are used to implement this fading. final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime); final float fromAlphaValue = 0; final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha); Arrays.fill(mDragOutlineAlphas, fromAlphaValue); for (int i = 0; i < mDragOutlineAnims.length; i++) { final InterruptibleInOutAnimator anim = new InterruptibleInOutAnimator(duration, fromAlphaValue, toAlphaValue); anim.getAnimator().setInterpolator(mEaseOutInterpolator); final int thisIndex = i; anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() { public void onAnimationUpdate(ValueAnimator animation) { final Bitmap outline = (Bitmap)anim.getTag(); // If an animation is started and then stopped very quickly, we can still // get spurious updates we've cleared the tag. Guard against this. if (outline == null) { if (false) { Object val = animation.getAnimatedValue(); Log.d(TAG, "anim " + thisIndex + " update: " + val + ", isStopped " + anim.isStopped()); } // Try to prevent it from continuing to run animation.cancel(); } else { mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue(); final int left = mDragOutlines[thisIndex].x; final int top = mDragOutlines[thisIndex].y; CellLayout.this.invalidate(left, top, left + outline.getWidth(), top + outline.getHeight()); } } }); // The animation holds a reference to the drag outline bitmap as long is it's // running. This way the bitmap can be GCed when the animations are complete. anim.getAnimator().addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) { anim.setTag(null); } } }); mDragOutlineAnims[i] = anim; } mBackgroundRect = new Rect(); mForegroundRect = new Rect(); mChildren = new CellLayoutChildren(context); mChildren.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap); addView(mChildren); } .... }