淡出动画
protected void startHotelNearByIconAnim() {AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);//初始化操作,参数传入0和1,即由透明度0变化到透明度为1alphaAnimation.setFillAfter(true);//动画结束后保持状态alphaAnimation.setDuration(2000);//动画持续时间,单位为毫秒alphaAnimation.setAnimationListener(new Animation.AnimationListener() {@Overridepublic void onAnimationStart(Animation animation) {}@Overridepublic void onAnimationRepeat(Animation animation) {}@Overridepublic 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"><alphaandroid: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"><scaleandroid: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"><rotateandroid: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"><translateandroid: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);
附:位移动画完成后位置恢复到初始位置的问题
- @Override
- protected void onAnimationEnd() {
- super.onAnimationEnd();
- FrameLayout.LayoutParams ll = new FrameLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.FILL_PARENT);
- ll.setMargins(-50, 0, 0, 0);
- main_layout.setLayoutParams(ll);
- }
位移动画
从左向右进入的动画 left_to_right.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" ><!-- 定义从左向右进入的动画 --><translateandroid: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" ><!-- 定义从右向左动画退出动画 --><translateandroid: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" ><!-- 定义从上向下进入的动画 --><translateandroid: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" ><!-- 定义从下向上动画退出动画 --><translateandroid: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" ><!-- 定义从右向左进入的动画 --><translateandroid: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" ><!-- 定义从左向右动画退出动画 --><translateandroid: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" ><!-- 定义从下向上进入的动画 --><translateandroid: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" ><!-- 定义从上向下动画退出动画 --><translateandroid: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);//引用animationinit();//初始化
}
单引用
Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.alpha);
AnimationSet animationSet2 = new AnimationSet(true);
animationSet2.addAnimation(animation);
imageView.startAnimation(animationSet2);