android Animation 动画。淡出动画

本文详细介绍了Android中各种视图动画的实现方式,包括透明、缩放、旋转和平移等基本动画,以及从屏幕外侧进入和退出的动画效果。通过代码示例展示了如何创建和应用这些动画。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

淡出动画

protected void startHotelNearByIconAnim() {
    AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);//初始化操作,参数传入0和1,即由透明度0变化到透明度为1
    alphaAnimation.setFillAfter(true);//动画结束后保持状态
    alphaAnimation.setDuration(2000);//动画持续时间,单位为毫秒
    alphaAnimation.setAnimationListener(new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {
        }
        @Override
        public void onAnimationRepeat(Animation animation) {
        }
        @Override
        public void onAnimationEnd(Animation animation) {
            loglinearLayout.setVisibility(View.GONE);

        }
    });
    loglinearLayout.startAnimation(alphaAnimation);//开始动画
}

 

基础动画:

view引用动画方法:

Animation animation = AnimationUtils.loadAnimation(this, R.anim.alpha);
iv.startAnimation(animation);

透明动画:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha
        android:duration="1000"
        android:fromAlpha="0.0"
        android:toAlpha="1.0"
        />
</set>

缩放动画2:

/**
 * 第一个参数fromAlpha为 动画开始时候透明度
 *第二个参数toAlpha为 动画结束时候透明度
 */
Animation animation = new AlphaAnimation(0, 1);
animation.setDuration(1000);

缩放动画:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale
        android:duration="1000"
        android:fromXScale="0"
        android:fromYScale="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="1"
        android:toYScale="1" />
</set>

缩放动画2:

/**
 * 第一个参数fromX为动画起始时 X坐标上的伸缩尺寸
 * 第二个参数toX为动画结束时 X坐标上的伸缩尺寸
 * 第三个参数fromY为动画起始时Y坐标上的伸缩尺寸
 * 第四个参数toY为动画结束时Y坐标上的伸缩尺寸
 * 说明: 0.0表示收缩到没有;1.0表示正常无伸缩;值小于1.0表示收缩;值大于1.0表示放大

 * 第五个参数pivotXType为动画在X轴相对于物件位置类型
 * 第六个参数pivotXValue为动画相对于物件的X坐标的开始位置
 * 第七个参数pivotXType为动画在Y轴相对于物件位置类型
 * 第八个参数pivotYValue为动画相对于物件的Y坐标的开始位置
 */
Animation animation = new ScaleAnimation(0, 1, 0, 1, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
animation.setDuration(1000);

旋转动画:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <rotate
        android:duration="1000"
        android:fromDegrees="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toDegrees="360" />
</set>

旋转动画2:

/**
 * 第一个参数fromDegrees为动画起始时角度
 * 第二个参数toDegrees为动画结束角度
 * 第三个参数pivotXType为动画在X轴相对于物件位置类型
 * 第四个参数pivotXValue为动画相对于物件的X坐标的开始位置
 * 第五个参数pivotXType为动画在Y轴相对于物件位置类型
 * 第六个参数pivotYValue为动画相对于物件的Y坐标的开始位置
 */
Animation animation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
animation.setDuration(1000);

位移动画:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="2000"
        android:fromXDelta="0"
        android:fromYDelta="0"
        android:toXDelta="500"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:toYDelta="0" />
</set>

位移动画2:

/**
 * 第一个参数fromXDelta为动画起始时的x坐标
 * 第二个参数toXDelta为动画结束时的x坐标
 * 第三个参数fromYDelta为动画起始时的y坐标
 * 第四个参数toYDelta为动画结束时的y坐标
 */
Animation animation = new TranslateAnimation(0, 500, 0, 0);
animation.setDuration(2000);
/**设置插值器:先加速,后减速**/
animation.setInterpolator(new AccelerateDecelerateInterpolator());
iv.startAnimation(animation);

附:位移动画完成后位置恢复到初始位置的问题

  1. @Override  
  2. protected void onAnimationEnd() {  
  3.     super.onAnimationEnd();  
  4.     FrameLayout.LayoutParams ll = new FrameLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.FILL_PARENT);  
  5.     ll.setMargins(-50, 0, 0, 0);  
  6.     main_layout.setLayoutParams(ll);  
  7.  }  

 

 

位移动画

从左向右进入的动画 left_to_right.xml

 

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <!-- 定义从左向右进入的动画 -->
    <translate
        android:duration="500"
        android:fromXDelta="-250%"
        android:toXDelta="0%" />

</set>

从右向左退出的动画 right_to_left.xml

 

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <!-- 定义从右向左动画退出动画 -->
    <translate
        android:duration="500"
        android:fromXDelta="0%"
        android:toXDelta="-250%" />

</set>

从上向下进入的动画 top_to_down.xml

 

 

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <!-- 定义从上向下进入的动画 -->
    <translate
        android:duration="500"
        android:fromYDelta="-250%"
        android:toYDelta="0%" />

</set>

从下向上退出的动画 down_to_top.xml

 

 

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <!-- 定义从下向上动画退出动画 -->
    <translate
        android:duration="500"
        android:fromYDelta="0%"
        android:toYDelta="-250%" />

</set>

从右向左进入的动画 right_to_left.xml

 

 

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <!-- 定义从右向左进入的动画 -->
    <translate
        android:duration="500"
        android:fromXDelta="250%"
        android:toXDelta="0%" />

</set>

从左向右退出的动画 left_to_right.xml

 

 

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <!-- 定义从左向右动画退出动画 -->
    <translate
        android:duration="500"
        android:fromXDelta="0%"
        android:toXDelta="250%" />

</set>

从下向上进入的动画 down_to_top.xml

 

 

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <!-- 定义从下向上进入的动画 -->
    <translate
        android:duration="500"
        android:fromYDelta="250%"
        android:toYDelta="0%" />

</set>

从上向下退出的动画 top_to_down.xml

 

 

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <!-- 定义从上向下动画退出动画 -->
    <translate
        android:duration="500"
        android:fromYDelta="0%"
        android:toYDelta="250%" />

</set>

创建style

 

 

<!-- 分享Dialog -->
<style name="dialog_share" parent="android:Animation">
    <item name="@android:windowEnterAnimation">@anim/down_to_top</item>
    <item name="@android:windowExitAnimation">@anim/top_to_dow</item>
</style>

进出引用

 

 

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.dialog_scroll_chose);
    Window window = getWindow();
    window.setGravity(Gravity.BOTTOM);//设置Dialog在底部显示
    window.setBackgroundDrawableResource(android.R.color.transparent);//设置背景透明
    window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);//设置横向全屏
    window.setWindowAnimations(R.style.dialog_share);//引用animation

    init();//初始化
}

单引用

 

Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.alpha);
AnimationSet animationSet2 = new AnimationSet(true);
animationSet2.addAnimation(animation);
imageView.startAnimation(animationSet2);

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值