最近由于项目需要,一直在寻找一个弹出窗口,在另一个弹出窗口弹出时,推上去的效果,居然找不到,经过不懈的努力,终于实现了popupwindow在更新时的动画。
先上代码:
1 import android.animation.ObjectAnimator; 2 import android.annotation.SuppressLint; 3 import android.content.Context; 4 import android.graphics.drawable.BitmapDrawable; 5 import android.util.AttributeSet; 6 import android.view.LayoutInflater; 7 import android.view.View; 8 import android.widget.PopupWindow; 9 import android.widget.TextView; 10 11 public class NotePopWindow extends PopupWindow { 12 private TextView mNodeTextView; 13 private Context mContext; 14 private ViewWrapper mWrapper; 15 16 public NotePopWindow(Context context, int width, int height) { 17 super(LayoutInflater.from(context).inflate(R.layout.fullscreen_view_note_popwindow, null), width, height); 18 mContext = context; 19 setBackgroundDrawable(new BitmapDrawable()); 20 setAnimationStyle(R.style.anim_note_bottombar); 21 initViews(); 22 } 23 24 public NotePopWindow(Context context) { 25 super(context); 26 27 } 28 29 public NotePopWindow(Context context, AttributeSet attributeSet) { 30 super(context, attributeSet); 31 } 32 33 private void initViews() { 34 mNodeTextView = (TextView) getContentView().findViewById(R.id.note_view); 35 mWrapper = new ViewWrapper(getContentView()); 36 } 37 38 @SuppressLint("NewApi") 39 public void startUpAnimation() { 40 ObjectAnimator translationRight = ObjectAnimator.ofInt(mWrapper, "Y", (int) mContext.getResources() 41 .getDimension(R.dimen.bottom_menu_window_height)); 42 translationRight.setDuration(540); 43 translationRight.start(); 44 } 45 46 @SuppressLint("NewApi") 47 public void startDownAnimation() { 48 ObjectAnimator translationRight = ObjectAnimator.ofInt(mWrapper, "Y", 0); 49 translationRight.setDuration(360); 50 translationRight.start(); 51 } 52 53 private class ViewWrapper { 54 private View mTarget; 55 private boolean isUp = true; 56 57 public ViewWrapper(View target) { 58 setmTarget(target); 59 } 60 61 @SuppressWarnings("unused") 62 public int getY() { 63 if (isUp) { 64 isUp = false; 65 return 0; 66 67 } else { 68 isUp = true; 69 return (int) mContext.getResources().getDimension(R.dimen.bottom_menu_window_height); 70 } 71 72 } 73 74 @SuppressWarnings("unused") 75 public void setY(int height) { 76 update(0, height, -1, -1); 77 } 78 79 @SuppressWarnings("unused") 80 public View getmTarget() { 81 return mTarget; 82 } 83 84 public void setmTarget(View mTarget) { 85 this.mTarget = mTarget; 86 } 87 } 88 }
实现原理其实就是依靠属性动画,但是属性动画只能作用于有set和get方法的属性,所以关键就是写一个包装类,提供属性的set和get方法,在set方法中调用popwindow的update方法,即可在update时实现动画。
讲的不是很清楚,代码如上,如果实在看不懂可以邮件、qq联系。。。