android 啦窗帘的动画,android 下拉窗帘效果

【实例简介】

【实例截图】

62dfb60e7189db8d72c69dc03b1e4142.gif

【核心代码】

public class CurtainView extends RelativeLayout implements OnTouchListener{

private static String TAG = "CurtainView";

private Context mContext;

/** Scroller 拖动类 */

private Scroller mScroller;

/** 屏幕高度 */

private int mScreenHeigh = 0;

/** 屏幕宽度 */

private int mScreenWidth = 0;

/** 点击时候Y的坐标*/

private int downY = 0;

/** 拖动时候Y的坐标*/

private int moveY = 0;

/** 拖动时候Y的方向距离*/

private int scrollY = 0;

/** 松开时候Y的坐标*/

private int upY = 0;

/** 广告幕布的高度*/

private int curtainHeigh = 0;

/** 是否 打开*/

private boolean isOpen = false;

/** 是否在动画 */

private boolean isMove = false;

/** 绳子的图片*/

private ImageView img_curtain_rope;

/** 广告的图片*/

private ImageView img_curtain_ad;

/** 上升动画时间 */

private int upDuration = 1000;

/** 下落动画时间 */

private int downDuration = 500;

public CurtainView(Context context) {

super(context);

init(context);

}

public CurtainView(Context context, AttributeSet attrs, int defStyle) {

super(context, attrs, defStyle);

init(context);

}

public CurtainView(Context context, AttributeSet attrs) {

super(context, attrs);

init(context);

}

/** 初始化 */

private void init(Context context) {

this.mContext = context;

//Interpolator 设置为有反弹效果的 (Bounce:反弹)

Interpolator interpolator = new BounceInterpolator();

mScroller = new Scroller(context, interpolator);

mScreenHeigh = BaseTools.getWindowHeigh(context);

mScreenWidth = BaseTools.getWindowWidth(context);

// 背景设置成透明

this.setBackgroundColor(Color.argb(0, 0, 0, 0));

final View view = LayoutInflater.from(mContext).inflate(R.layout.curtain, null);

img_curtain_ad = (ImageView)view.findViewById(R.id.img_curtain_ad);

img_curtain_rope = (ImageView)view.findViewById(R.id.img_curtain_rope);

addView(view);

img_curtain_ad.post(new Runnable() {

@Override

public void run() {

// TODO Auto-generated method stub

curtainHeigh = img_curtain_ad.getHeight();

Log.d(TAG, "curtainHeigh= " curtainHeigh);

CurtainView.this.scrollTo(0, curtainHeigh);

//注意scrollBy和scrollTo的区别

}

});

img_curtain_rope.setOnTouchListener(this);

}

/**

* 拖动动画

* @param startY

* @param dy 垂直距离, 滚动的y距离

* @param duration 时间

*/

public void startMoveAnim(int startY, int dy, int duration) {

isMove = true;

mScroller.startScroll(0, startY, 0, dy, duration);

invalidate();//通知UI线程的更新

}

@Override

protected void onLayout(boolean changed, int l, int t, int r, int b) {

// TODO Auto-generated method stub

super.onLayout(changed, l, t, r, b);

}

@Override

public void computeScroll() {

//判断是否还在滚动,还在滚动为true

if (mScroller.computeScrollOffset()) {

scrollTo(mScroller.getCurrX(), mScroller.getCurrY());

//更新界面

postInvalidate();

isMove = true;

} else {

isMove = false;

}

super.computeScroll();

}

@Override

public boolean onTouch(View v, MotionEvent event) {

// TODO Auto-generated method stub

if (!isMove) {

int offViewY = 0;//屏幕顶部和该布局顶部的距离

switch (event.getAction()) {

case MotionEvent.ACTION_DOWN:

downY = (int) event.getRawY();

offViewY = downY - (int)event.getX();

return true;

case MotionEvent.ACTION_MOVE:

moveY = (int) event.getRawY();

scrollY = moveY - downY;

if (scrollY < 0) {

// 向上滑动

if(isOpen){

if(Math.abs(scrollY) <= img_curtain_ad.getBottom() - offViewY){

scrollTo(0, -scrollY);

}

}

} else {

// 向下滑动

if(!isOpen){

if (scrollY <= curtainHeigh) {

scrollTo(0, curtainHeigh - scrollY);

}

}

}

break;

case MotionEvent.ACTION_UP:

upY = (int) event.getRawY();

if(Math.abs(upY - downY) < 10){

onRopeClick();

break;

}

if (downY > upY) {

// 向上滑动

if(isOpen){

if (Math.abs(scrollY) > curtainHeigh / 2) {

// 向上滑动超过半个屏幕高的时候 开启向上消失动画

startMoveAnim(this.getScrollY(),

(curtainHeigh - this.getScrollY()), upDuration);

isOpen = false;

} else {

startMoveAnim(this.getScrollY(), -this.getScrollY(),upDuration);

isOpen = true;

}

}

} else {

// 向下滑动

if (scrollY > curtainHeigh / 2) {

// 向上滑动超过半个屏幕高的时候 开启向上消失动画

startMoveAnim(this.getScrollY(), -this.getScrollY(),upDuration);

isOpen = true;

} else {

startMoveAnim(this.getScrollY(),(curtainHeigh - this.getScrollY()), upDuration);

isOpen = false;

}

}

break;

default:

break;

}

}

return false;

}

/**

* 点击绳索开关,会展开关闭

* 在onToch中使用这个中的方法来当点击事件,避免了点击时候响应onTouch的衔接不完美的影响

*/

public void onRopeClick(){

if(isOpen){

CurtainView.this.startMoveAnim(0, curtainHeigh, upDuration);

}else{

CurtainView.this.startMoveAnim(curtainHeigh,-curtainHeigh, downDuration);

}

isOpen = !isOpen;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值