標籤:android style class blog c code
最近由於項目需要,一直在尋找一個快顯視窗,在另一個快顯視窗彈出時,推上去的效果,居然找不到,經過不懈的努力,終於實現了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聯絡。。。