属性アニメーションを使用して、コントロールのレイアウト位置を動的に変更します.
7612 ワード
AnimatorSet set = new AnimatorSet();
set.playTogether(
ObjectAnimator.ofFloat(mButton , "scaleX" , 1f , 5f ),
ObjectAnimator.ofFloat(mButton , "scaleY" , 1f , 5f )
);
set.setDuration(2000).start();
public class ViewWrapper {
private View mTarget ;
// params params!!
// params, , params mParams.leftMargin 。。 !!
private RelativeLayout.LayoutParams mParams ;
private int mLeftMargin ;
public ViewWrapper(View target) {
mTarget = target;
mParams = (RelativeLayout.LayoutParams) mTarget.getLayoutParams();
}
//
public int getWidth(){
return mTarget.getLayoutParams().width;
}
public int getHeight(){
return mTarget.getLayoutParams().height;
}
public void setWidth(int width){
mTarget.getLayoutParams().width = width ;
mTarget.requestLayout();
}
public void setHeight(int height){
mTarget.getLayoutParams().height = height ;
mTarget.requestLayout();
}
//
public int getLeftMargin(){
return mParams.leftMargin ;
}
// ,
public void setLeftMargin(int leftMargin ){
mLeftMargin = leftMargin;
}
public int getTopMargin(){
return mParams.topMargin ;
}
// 。 , !!
public void setTopMargin(int topMargin){
mParams.setMargins( mLeftMargin , topMargin , 0 , 0 );
mTarget.setLayoutParams(mParams);
mTarget.requestLayout();
}
}
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mButton.post(new Runnable() {
@Override
public void run() {
mWidth = mButton.getWidth();
mHeight = mButton.getHeight();
changeBigSmall(mButton);
}
});
}
});
//
// ObjectAnimator.ofInt(wrapper, "width", mWidth , mWidth + 60 ), ! viewWrap get 、 set !( , )
private void changeBigSmall(View view) {
if (view == null) return;
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) view.getLayoutParams();
int leftMargin = params.leftMargin;
int topMargin = params.topMargin;
AnimatorSet set = new AnimatorSet();
ViewWrapper wrapper = new ViewWrapper(view);
set.playTogether(
ObjectAnimator.ofInt(wrapper, "width", mWidth , mWidth + 60 ),
ObjectAnimator.ofInt(wrapper, "height", mHeight , mHeight + 60),
ObjectAnimator.ofInt(wrapper, "leftMargin", leftMargin , leftMargin + 50 ),
ObjectAnimator.ofInt(wrapper, "topMargin", topMargin , topMargin + 50)// , viewWrap
);
set.setDuration(2000).start();
}