public class MainActivity extends AppCompatActivity {
@Bind(R.id.main_fl_card_back)
FrameLayout mFlCardBack;
@Bind(R.id.main_fl_card_front)
FrameLayout mFlCardFront;
@Bind(R.id.main_fl_container)
FrameLayout mFlContainer;
private AnimatorSet mLeftInSet;
private AnimatorSet mRightOutSet;
private boolean mIsShowBack;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
setAnimation();//设置动画
setCameraDistance();//设置镜头距离,在这里不是太懂
}
private void setAnimation() {
//mLeftInSet是左边进入的动画
mLeftInSet = (AnimatorSet) AnimatorInflater.loadAnimator(this, R.animator.anim_in);
//mRightOutSet是右边出去的动画
mRightOutSet = (AnimatorSet) AnimatorInflater.loadAnimator(this, R.animator.anim_out);
//点击事件
//通过ListenerAdapter就不需重写所有方法,只需写需要写的方法
mRightOutSet.addListener(new AnimatorListenerAdapter() {
//动画开始时候
@Override
public void onAnimationStart(Animator animation) {
super.onAnimationStart(animation);
mFlContainer.setClickable(false);
}
});
//动画结束的时候
mLeftInSet.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
mFlContainer.setClickable(true);//主布局中framelayouy的就允许你去点击了
}
});
}
//一直不是很懂的设置镜头距离,
//帖子上的注释写着:改变视角距离,贴近屏幕
private void setCameraDistance() {
int distance = 16000;
float scale = getResources().getDisplayMetrics().density*distance;
mFlCardFront.setCameraDistance(scale);//设置距离
mFlCardBack.setCameraDistance(scale);//设置距离
}
//这是主Framelayout的点击方法
public void flipCard(View view){
//mIsShowBack可以理解为互斥,所以为boolean
if(!mIsShowBack){
//右出动画设置在正面卡片界面
mRightOutSet.setTarget(mFlCardFront);
//左入动画设置在反面卡片界面
mLeftInSet.setTarget(mFlCardBack);
//开始动画
mRightOutSet.start();
mLeftInSet.start();
mIsShowBack = true;
}else {
//右出动画设置在卡片背面界面
mRightOutSet.setTarget(mFlCardBack);
//左入动画设置在卡片正面界面
mLeftInSet.setTarget(mFlCardFront);
mRightOutSet.start();
mLeftInSet.start();
mIsShowBack = false;
}
}
//这一点我一直没想到,还可以在onDestroy方法中解绑ButterKnife
protected void onDestroy(){
super.onDestroy();
ButterKnife.unbind(this);
}
}
|