private void toggleFullscreen(boolean isFullscreen) {
if (isFullscreen) {
// 从缩放状态恢复
animateToOriginalSize();
} else {
// 切换到缩放状态
animateToFullscreenOrScaled();
}
}
/**
* 缩放比例
*/
/**
*缩放往哪缩放还是看自己的需求
ObjectAnimator translationX = ObjectAnimator.ofFloat(mLivePlayer, "translationX", 0f, -targetX); ObjectAnimator translationY = ObjectAnimator.ofFloat(mLivePlayer, "translationY", 0f, 0);
我这里y轴不动,x轴居中,所以translationY 中的参数是0,只修改translationX 就可以,根据自己的需求修改这里的参数就行
*/
private float scaleNumber = 0.6f;
private void animateToFullscreenOrScaled() {
// 创建动画集合
AnimatorSet animatorSet = new AnimatorSet();
// 缩放动画
ObjectAnimator scaleX = ObjectAnimator.ofFloat(mLivePlayer, "scaleX", 1f, scaleNumber);
ObjectAnimator scaleY = ObjectAnimator.ofFloat(mLivePlayer, "scaleY", 1f, scaleNumber);
// 计算居左居中的位置
// 缩放后宽度 = 原宽度 * 缩放比例
float scaledWidth = mLivePlayer.getWidth() * scaleNumber;
float targetX = scaledWidth*(1-scaleNumber);
// 位置动画
ObjectAnimator translationX = ObjectAnimator.ofFloat(mLivePlayer, "translationX", 0f, -targetX);
ObjectAnimator translationY = ObjectAnimator.ofFloat(mLivePlayer, "translationY", 0f, 0);
animatorSet.playTogether(scaleX, scaleY, translationX, translationY);
animatorSet.setDuration(500);
animatorSet.start();
}
/**
*动画还原方法基本没什么修改,可以直接用
/
private void animateToOriginalSize() {
// 创建动画集合
AnimatorSet animatorSet = new AnimatorSet();
// 恢复原始大小(1f)
ObjectAnimator scaleX = ObjectAnimator.ofFloat(mLivePlayer, "scaleX", scaleNumber, 1f);
ObjectAnimator scaleY = ObjectAnimator.ofFloat(mLivePlayer, "scaleY", scaleNumber, 1f);
// 恢复原始位置(回到 translationX=0, translationY=0)
ObjectAnimator translationX = ObjectAnimator.ofFloat(mLivePlayer, "translationX", mLivePlayer.getTranslationX(), 0f);
ObjectAnimator translationY = ObjectAnimator.ofFloat(mLivePlayer, "translationY", mLivePlayer.getTranslationY(), 0f);
animatorSet.playTogether(scaleX, scaleY, translationX, translationY);
animatorSet.setDuration(500);
animatorSet.start();
}
控制View缩放与还原
最新推荐文章于 2025-11-24 15:06:53 发布
1万+

被折叠的 条评论
为什么被折叠?



