您的位置:首页 > 运维架构

andorid popupwindow 更新时动画的实现,可实现一个窗口被一个窗口推上去的效果

2014-05-24 18:14 483 查看
最近由于项目需要,一直在寻找一个弹出窗口,在另一个弹出窗口弹出时,推上去的效果,居然找不到,经过不懈的努力,终于实现了popupwindow在更新时的动画。

先上代码:

import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.PopupWindow;
import android.widget.TextView;

public class NotePopWindow extends PopupWindow {
private TextView mNodeTextView;
private Context mContext;
private ViewWrapper mWrapper;

public NotePopWindow(Context context, int width, int height) {
super(LayoutInflater.from(context).inflate(R.layout.fullscreen_view_note_popwindow, null), width, height);
mContext = context;
setBackgroundDrawable(new BitmapDrawable());
setAnimationStyle(R.style.anim_note_bottombar);
initViews();
}

public NotePopWindow(Context context) {
super(context);

}

public NotePopWindow(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
}

private void initViews() {
mNodeTextView = (TextView) getContentView().findViewById(R.id.note_view);
mWrapper = new ViewWrapper(getContentView());
}

@SuppressLint("NewApi")
public void startUpAnimation() {
ObjectAnimator translationRight = ObjectAnimator.ofInt(mWrapper, "Y", (int) mContext.getResources()
.getDimension(R.dimen.bottom_menu_window_height));
translationRight.setDuration(540);
translationRight.start();
}

@SuppressLint("NewApi")
public void startDownAnimation() {
ObjectAnimator translationRight = ObjectAnimator.ofInt(mWrapper, "Y", 0);
translationRight.setDuration(360);
translationRight.start();
}

private class ViewWrapper {
private View mTarget;
private boolean isUp = true;

public ViewWrapper(View target) {
setmTarget(target);
}

@SuppressWarnings("unused")
public int getY() {
if (isUp) {
isUp = false;
return 0;

} else {
isUp = true;
return (int) mContext.getResources().getDimension(R.dimen.bottom_menu_window_height);
}

}

@SuppressWarnings("unused")
public void setY(int height) {
update(0, height, -1, -1);
}

@SuppressWarnings("unused")
public View getmTarget() {
return mTarget;
}

public void setmTarget(View mTarget) {
this.mTarget = mTarget;
}
}
}


实现原理其实就是依靠属性动画,但是属性动画只能作用于有set和get方法的属性,所以关键就是写一个包装类,提供属性的set和get方法,在set方法中调用popwindow的update方法,即可在update时实现动画。

讲的不是很清楚,代码如上,如果实在看不懂可以邮件、qq联系。。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: