android开发固定在顶端,Android应用开发之在界面顶部滑动展开的自定义FrameLayout...

本文将带你了解Android应用开发之在界面顶部滑动展开的自定义FrameLayout,希望本文对大家学Android有所帮助

用法:

Java代码    translationView.show();  translationView.hide();  translationView.setShadowColor(ActivityCompat.getColor(MainActivity.this, R.color.blue));//设置背景颜色  translationView.show();

translationView.hide();

translationView.setShadowColor(ActivityCompat.getColor(MainActivity.this,   R.color.blue));//设置背景颜色

源码:

Java代码    import android.animation.Animator;  import android.animation.AnimatorListenerAdapter;  import android.animation.ObjectAnimator;  import android.content.Context;  import android.graphics.Canvas;  import android.support.annotation.ColorInt;  import android.support.v4.view.MotionEventCompat;  import android.util.AttributeSet;  import android.util.Log;  import android.view.MotionEvent;  import android.view.View;  import android.widget.FrameLayout;    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  topEdge && y 

import android.animation.AnimatorListenerAdapter;

import android.animation.ObjectAnimator;

import android.content.Context;

import android.graphics.Canvas;

import android.support.annotation.ColorInt;

import android.support.v4.view.MotionEventCompat;

import android.util.AttributeSet;

import android.util.Log;

import android.view.MotionEvent;

import android.view.View;

import android.widget.FrameLayout;

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;

}

}

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标移动开发之Android频道!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值