一个可以在界面顶部展现的自定义 View
tusp0079
8年前
<p>这个源码十分简单,没啥可说的.只有一点提一下,看到有些控件达到背景色的效果,是动态填加一个带背景色父布局实现的.</p> <p>我这里是在画布上动态绘制的.</p> <p>GitHub 地址: <a href="/misc/goto?guid=4959737232061564444" rel="nofollow,noindex">TranslationView</a></p> <p>源码如下:</p> <pre> public class TranslationView extends FrameLayout { private static final String TAG = "TranslationView"; private static final int DEFAULT_COLOR = 0x50000000; private int mShadowColor = DEFAULT_COLOR; private boolean mIsShow = false; private View mTranslationView; public TranslationView(Context context) { super(context, null); } public TranslationView(Context context, AttributeSet attrs) { super(context, attrs, 0); } public TranslationView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onFinishInflate() { if (getChildCount() != 2) { throw new IllegalStateException("only and should contain two child view"); } mTranslationView = getChildAt(1); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); Log.d(TAG, "onMeasure"); } @Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { super.onLayout(changed, left, top, right, bottom); mTranslationView.layout(0, -mTranslationView.getHeight(), mTranslationView.getWidth(), 0); } @Override protected boolean drawChild(Canvas canvas, View child, long drawingTime) { if (mIsShow && child == mTranslationView) { canvas.drawColor(mShadowColor); } return super.drawChild(canvas, child, drawingTime); } @Override public boolean onInterceptTouchEvent(MotionEvent ev) { final int action = MotionEventCompat.getActionMasked(ev); switch (action){ case MotionEvent.ACTION_DOWN:{ if(mIsShow&&inShadow(ev)){ hide(); return true; } } } return super.onInterceptTouchEvent(ev); } private boolean inShadow(MotionEvent ev) { float x = ev.getX(); float y = ev.getY(); final float leftEdge = mTranslationView.getX(); final float rightEdge = leftEdge+mTranslationView.getWidth(); final float topEdge =mTranslationView.getHeight(); final float bottomEdge = getHeight()+topEdge; return x > leftEdge && x < rightEdge && y > topEdge && y < bottomEdge; } public void show() { if (!mIsShow) { mIsShow = true; ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(mTranslationView, "translationY", mTranslationView.getTranslationY(), mTranslationView.getHeight()); objectAnimator.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationStart(Animator animation) { super.onAnimationStart(animation); invalidate(); } }); objectAnimator.start(); } } public void hide() { if (mIsShow) { mIsShow = false; ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(mTranslationView, "translationY", mTranslationView.getTranslationY(), -mTranslationView.getHeight()); objectAnimator.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { super.onAnimationEnd(animation); invalidate(); } }); objectAnimator.start(); } } public void setShadowColor(@ColorInt int color) { mShadowColor = color; } }</pre> <p>效果图如下:</p> <p style="text-align: center;"><img src="https://simg.open-open.com/show/5dad3a86e7af2756bab378e16ab39b00.gif"></p> <p style="text-align: center;">TranslationView.gif</p> <p> </p> <p>来自:http://www.jianshu.com/p/aa4d3b4b4a04</p> <p> </p>