Android动画知识汇总
来源:互联网 发布:openwrt mac地址过滤 编辑:程序博客网 时间:2024/06/03 23:39
1
2
3
4
5
//初始化
Animation alphaAnimation =
new
AlphaAnimation(
0
.1f,
1
.0f);
//设置动画时间
alphaAnimation.setDuration(
3000
);
this
.startAnimation(alphaAnimation);
1
2
3
Animation rotateAnimation =
new
RotateAnimation(0f, 360f);
rotateAnimation.setDuration(
1000
);
this
.startAnimation(rotateAnimation);
1
2
3
4
5
//初始化
Animation scaleAnimation =
new
ScaleAnimation(
0
.1f,
1
.0f,
0
.1f,
1
.0f);
//设置动画时间
scaleAnimation.setDuration(
500
);
this
.startAnimation(scaleAnimation);
1
2
3
4
5
//初始化
nimation translateAnimation =
new
TranslateAnimation(
0
.1f,
100
.0f,
0
.1f,
100
.0f);
//设置动画时间
translateAnimation.setDuration(
1000
);
this
.startAnimation(translateAnimation);
1
2
3
4
5
6
7
8
9
10
11
12
13
//初始化 Translate动画
translateAnimation =
new
TranslateAnimation(
0
.1f,
100
.0f,
0
.1f,
100
.0f);
//初始化 Alpha动画
alphaAnimation =
new
AlphaAnimation(
0
.1f,
1
.0f);
//动画集
AnimationSet set =
new
AnimationSet(
true
);
set.addAnimation(translateAnimation);
set.addAnimation(alphaAnimation);
//设置动画时间 (作用到每个动画)
set.setDuration(
1000
);
this
.startAnimation(set);
1
2
<
set
android:interpolator
=
"@android:anim/accelerate_interpolator"
>
</
set
>
1
<
span
style
=
"font-family:'宋体', SimSun;"
> <?
xml
version
=
"1.0"
encoding
=
"utf-8"
?><
br
> <
InterpolatorName
xmlns:android
=
"http://schemas.android.com/apk/res/android"
<br> android:attribute_name="value"<
br
> /><
br
></
span
>
1
<
span
style
=
"font-family:'宋体', SimSun;"
> <?
xml
version
=
"1.0"
encoding
=
"utf-8"
?><
br
> <
overshootInterpolator
xmlns:android
=
"http://schemas.android.com/apk/res/android"
<br> android:tension="7.0"/></
span
>
1
<
br
><
span
style
=
"font-family:'宋体', SimSun;"
> <
scale
xmlns:android
=
"http://schemas.android.com/apk/res/android"
<br> android:interpolator="@anim/my_overshoot_interpolator"<
br
> android:fromXScale="1.0"<
br
> android:toXScale="3.0"<
br
> android:fromYScale="1.0"<
br
> android:toYScale="3.0<
br
> android:pivotX="50%"<
br
> android:pivotY="50%"<
br
> android:duration="700" /></
span
>
1
2
3
public
float
getInterpolation(
float
input) {
return
input;
}
1
2
3
public float getInterpolation(float input) {
return (float)(Math.cos((input + 1) * Math.PI) / 2.0f) + 0.5f;
}
1) 计算属性值
核心类:
1
2
3
4
5
6
7
8
9
10
ValueAnimator animation = ValueAnimator.ofFloat(0f, 1f);
animation.setDuration(
1000
);
animation.addUpdateListener(
new
AnimatorUpdateListener() {
@Override
public
void
onAnimationUpdate(ValueAnimator animation) {
Log.i(
"update"
, ((Float) animation.getAnimatedValue()).toString());
}
});
animation.setInterpolator(
new
CycleInterpolator(
3
));
animation.start();
1
2
3
4
5
ObjectAnimator animX = ObjectAnimator.ofFloat(myView,
"x"
, 50f);
ObjectAnimator animY = ObjectAnimator.ofFloat(myView,
"y"
, 100f);
AnimatorSet animSetXY =
new
AnimatorSet();
animSetXY.playTogether(animX, animY);
animSetXY.start();
1
2
3
ropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat(
"x"
, 50f);
PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat(
"y"
, 100f);
ObjectAnimator.ofPropertyValuesHolder(myView, pvhX, pvyY).start();
1
myView.animate().x(50f).y(100f);
//myView.animate()直接返回一个ViewPropertyAnimator对象
1
2
3
4
5
6
Keyframe kf0 = Keyframe.ofFloat(0f, 0f);
Keyframe kf1 = Keyframe.ofFloat(.5f, 360f);
Keyframe kf2 = Keyframe.ofFloat(1f, 0f);
PropertyValuesHolder pvhRotation = PropertyValuesHolder.ofKeyframe(
"rotation"
, kf0, kf1, kf2);
//动画属性名,可变参数
ObjectAnimator rotationAnim = ObjectAnimator.ofPropertyValuesHolder(target, pvhRotation)
rotationAnim.setDuration(
5000
);
1
2
3
4
5
6
7
8
9
10
11
12
13
public
static
KeyframeSet ofFloat(
float
... values) {
int
numKeyframes = values.length;
FloatKeyframe keyframes[] =
new
FloatKeyframe[Math.max(numKeyframes,
2
)];
if
(numKeyframes ==
1
) {
keyframes[
0
] = (FloatKeyframe) Keyframe.ofFloat(0f);
keyframes[
1
] = (FloatKeyframe) Keyframe.ofFloat(1f, values[
0
]);
}
else
{
keyframes[
0
] = (FloatKeyframe) Keyframe.ofFloat(0f, values[
0
]);
for
(
int
i =
1
; i < numKeyframes; ++i) {
keyframes[i] = (FloatKeyframe) Keyframe.ofFloat((
float
) i / (numKeyframes -
1
), values[i]);
//这里是关键
}
}
return
new
FloatKeyframeSet(keyframes);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<
set
android:ordering
=
"sequentially"
>
<
set
>
<
objectAnimator
android:propertyName
=
"x"
android:duration
=
"500"
android:valueTo
=
"400"
android:valueType
=
"intType"
/>
<
objectAnimator
android:propertyName
=
"y"
android:duration
=
"500"
android:valueTo
=
"300"
android:valueType
=
"intType"
/>
</
set
>
<
objectAnimator
android:propertyName
=
"alpha"
android:duration
=
"500"
android:valueTo
=
"1f"
/>
</
set
>
1
2
3
4
AnimatorSet set = (AnimatorSet) AnimatorInflater.loadAnimator(myContext,
R.anim.property_animator);
set.setTarget(myObject);
set.start();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<
set
android:ordering=["together" | "sequentially"]>
<
objectAnimator
android:propertyName
=
"string"
android:duration
=
"int"
android:valueFrom
=
"float | int | color"
android:valueTo
=
"float | int | color"
android:startOffset
=
"int"
android:repeatCount
=
"int"
android:repeatMode=["repeat" | "reverse"]
android:valueType=["intType" | "floatType"]/>
<
animator
android:duration
=
"int"
android:valueFrom
=
"float | int | color"
android:valueTo
=
"float | int | color"
android:startOffset
=
"int"
android:repeatCount
=
"int"
android:repeatMode=["repeat" | "reverse"]
android:valueType=["intType" | "floatType"]/>
<
set
>
...
</
set
>
</
set
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<
Button
android:id
=
"@+id/btn_1"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:text
=
"@string/btn_1"
android:padding
=
"10px"
android:layout_below
=
"@id/touch_feedback_ripple_textview"
/>
<
Button
android:id
=
"@+id/btn_2"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:text
=
"@string/btn_2"
android:layout_below
=
"@id/btn_1"
android:padding
=
"10px"
android:background
=
"?android:attr/selectableItemBackground"
/>
<
Button
android:id
=
"@+id/btn_3"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:text
=
"@string/btn_3"
android:layout_below
=
"@id/btn_2"
android:padding
=
"10px"
android:background
=
"?android:attr/selectableItemBackgroundBorderless"
/>
1
2
3
public
static
Animator createCircularReveal(View view,nt centerX,
int
centerY,
float
startRadius,
float
endRadius) {
return
new
RevealAnimator(view, centerX, centerY, startRadius, endRadius);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
final
View oval =
this
.findViewById(R.id.oval);
oval.setOnClickListener(
new
View.OnClickListener() {
@Override
public
void
onClick(View v) {
Animator animator = ViewAnimationUtils.createCircularReveal(
oval,oval.getWidth()/
2
,oval.getHeight()/
2
,oval.getWidth(),
0
);
animator.setInterpolator(
new
AccelerateDecelerateInterpolator());
animator.setDuration(
2000
);
animator.start();
}
});
final
View rect =
this
.findViewById(R.id.rect);
rect.setOnClickListener(
new
View.OnClickListener() {
@Override
public
void
onClick(View v) {
Animator animator = ViewAnimationUtils.createCircularReveal(
rect,
0
,
0
,
0
,(
float
) Math.hypot(rect.getWidth(), rect.getHeight()));
animator.setInterpolator(
new
AccelerateInterpolator());
animator.setDuration(
2000
);
animator.start();
}
});
1
2
3
4
5
6
7
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
fade
xmlns:android
=
"http://schemas.android.com/apk/res/"
android:duration
=
"1000"
/>
res/transition/activity_slide.xml
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
slide
xmlns:android
=
"http://schemas.android.com/apk/res/"
android:duration
=
"1000"
/>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_transition);
setupWindowAnimations();
}
private
void
setupWindowAnimations() {
Slide slide = TransitionInflater.from(
this
).inflateTransition(R.transition.activity_slide);
getWindow().setExitTransition(slide);
}
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_transition);
setupWindowAnimations();
}
private
void
setupWindowAnimations() {
Fade fade = TransitionInflater.from(
this
).inflateTransition(R.transition.activity_fade);
getWindow().setEnterTransition(fade);
}
1
2
3
4
5
6
7
8
9
10
11
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_transition);
setupWindowAnimations();
}
private
void
setupWindowAnimations() {
Slide slide =
new
Slide();
slide.setDuration(
1000
);
getWindow().setExitTransition(slide);
}
1
2
3
4
5
6
7
8
9
10
11
12
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_transition);
setupWindowAnimations();
}
private
void
setupWindowAnimations() {
Fade fade =
new
Fade();
fade.setDuration(
1000
);
getWindow().setEnterTransition(fade);
}
1
2
3
4
5
6
7
8
9
10
11
12
<
resources
>
<
style
name
=
"AppTheme"
parent
=
"Theme.AppCompat.Light.DarkActionBar"
>
<
item
name
=
"colorPrimary"
>@color/colorPrimary</
item
>
<
item
name
=
"colorPrimaryDark"
>@color/colorPrimaryDark</
item
>
<
item
name
=
"colorAccent"
>@color/colorAccent</
item
>
<!--设置选中的颜色-->
<!-- 允许使用transitions -->
<
item
name
=
"android:windowContentTransitions"
>true</
item
>
<!--是否覆盖执行,其实可以理解成是否同步执行还是顺序执行-->
<
item
name
=
"android:windowAllowEnterTransitionOverlap"
>false</
item
>
<
item
name
=
"android:windowAllowReturnTransitionOverlap"
>false</
item
>
</
style
>
</
resources
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
@Override
public
void
onClick(View v) {
ArrayList<Pair<View,String>> arrayList =
new
ArrayList<Pair<View,String>>();
switch
(v.getId()) {
case
R.id.explode_btn:
setExplodeTransition();
flag = AnimConstant.EXPLODE_FLAG;
break
;
case
R.id.slide_btn:
setSlideTransition();
flag = AnimConstant.SLIDE_FLAG;
break
;
case
R.id.pade_in_out_btn:
setFadeTransition();
flag = AnimConstant.PADE_IN_OUT_FLAG;
break
;
case
R.id.shared_element_btn:
arrayList.add(
new
Pair<View, String>(shareElementBtn,
"shared_name_btn"
));
flag = AnimConstant.SHARED_ELEMENTS_FLAG;
break
;
}
startActivity(arrayList);
}
private
void
setFadeTransition() {
Fade fadeTransition =
new
Fade();
fadeTransition.setDuration(
1000
);
getWindow().setReenterTransition(fadeTransition);
getWindow().setExitTransition(fadeTransition);
}
private
void
startActivity(ArrayList<Pair<View, String>> arrayList) {
Intent intent =
new
Intent();
intent.setClass(
this
,SecondActivity.
class
);
intent.putExtra(
"transition_flag"
,flag);
ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(
this
, arrayList.toArray(
new
Pair[arrayList.size()]));
startActivity(intent, options.toBundle());
}
private
void
setExplodeTransition() {
Explode explode =
new
Explode();
explode.setDuration(
2000
);
getWindow().setReenterTransition(explode);
getWindow().setExitTransition(explode);
}
private
void
setSlideTransition() {
Slide slideTransition =
new
Slide();
slideTransition.setSlideEdge(Gravity.LEFT);
slideTransition.setDuration(
1000
);
getWindow().setReenterTransition(slideTransition);
getWindow().setExitTransition(slideTransition);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
private
Button returnBtn =
null
;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Intent intent = getIntent();
int
flag = intent.getIntExtra(
"transition_flag"
,-
1
);
if
(flag != AnimConstant.SHARED_ELEMENTS_FLAG) {
setupWindowAnimations();
}
returnBtn = (Button)
this
.findViewById(R.id.return_btn);
returnBtn.setOnClickListener(
new
View.OnClickListener() {
@Override
public
void
onClick(View v) {
finishAfterTransition();
}
});
}
private
void
setupWindowAnimations() {
Explode explode =
new
Explode();
explode.setDuration(
2000
);
getWindow().setEnterTransition(explode);
}
1
2
3
4
5
<
pathInterpolator
xmlns:android
=
"http://schemas.android.com/apk/res/android"
android:controlX1
=
"0.4"
android:controlY1
=
"0"
android:controlX2
=
"1"
android:controlY2
=
"1"
/>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public
class
SCPahtInterpolator
extends
PathInterpolator {
public
static
final
float
DEFALUT_CONTROL_1_X =
0
.5f;
public
static
final
float
DEFALUT_CONTROL_1_Y = 0f;
public
static
final
float
DEFALUT_CONTROL_2_X = 0f;
public
static
final
float
DEFALUT_CONTROL_2_Y = 1f;
public
SCPahtInterpolator() {
super
(DEFALUT_CONTROL_1_X, DEFALUT_CONTROL_1_Y, DEFALUT_CONTROL_2_X, DEFALUT_CONTROL_2_Y);
}
public
SCPahtInterpolator(Path path) {
super
(path);
}
public
SCPahtInterpolator(
float
controlX,
float
controlY) {
super
(controlX, controlY);
}
public
SCPahtInterpolator(
float
controlX1,
float
controlY1,
float
controlX2,
float
controlY2) {
super
(controlX1, controlY1, controlX2, controlY2);
}
public
SCPahtInterpolator(Context context, AttributeSet attrs) {
super
(context, attrs);
}
}
1
2
3
4
ObjectAnimator mAnimator;
mAnimator = ObjectAnimator.ofFloat(view, View.X, View.Y, path);
...
mAnimator.start();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<!-- animate the translationZ property of a view when pressed -->
<
selector
xmlns:android
=
"http://schemas.android.com/apk/res/android"
>
<
item
android:state_pressed
=
"true"
>
<
set
>
<
objectAnimator
android:propertyName
=
"translationZ"
android:duration
=
"@android:integer/config_shortAnimTime"
android:valueTo
=
"2dp"
android:valueType
=
"floatType"
/>
<!-- you could have other objectAnimator elements
here for "x" and "y", or other properties -->
</
set
>
</
item
>
<
item
android:state_enabled
=
"true"
android:state_pressed
=
"false"
android:state_focused
=
"true"
>
<
set
>
<
objectAnimator
android:propertyName
=
"translationZ"
android:duration
=
"100"
android:valueTo
=
"0"
android:valueType
=
"floatType"
/>
</
set
>
</
item
>
</
selector
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<!-- res/drawable/myanimstatedrawable.xml -->
<
animated-selector
xmlns:android
=
"http://schemas.android.com/apk/res/android"
>
<!-- provide a different drawable for each state-->
<
item
android:id
=
"@+id/pressed"
android:drawable
=
"@drawable/drawableP"
android:state_pressed
=
"true"
/>
<
item
android:id
=
"@+id/focused"
android:drawable
=
"@drawable/drawableF"
android:state_focused
=
"true"
/>
<
item
android:id
=
"@id/default"
android:drawable
=
"@drawable/drawableD"
/>
<!-- specify a transition -->
<
transition
android:fromId
=
"@+id/default"
android:toId
=
"@+id/pressed"
>
<
animation-list
>
<
item
android:duration
=
"15"
android:drawable
=
"@drawable/dt1"
/>
<
item
android:duration
=
"15"
android:drawable
=
"@drawable/dt2"
/>
...
</
animation-list
>
</
transition
>
...
</
animated-selector
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<!-- res/drawable/vectordrawable.xml -->
<
vector
xmlns:android
=
"http://schemas.android.com/apk/res/android"
android:height
=
"64dp"
android:width
=
"64dp"
android:viewportHeight
=
"600"
android:viewportWidth
=
"600"
>
<
group
android:name
=
"rotationGroup"
android:pivotX
=
"300.0"
android:pivotY
=
"300.0"
android:rotation
=
"45.0"
>
<
path
android:name
=
"v"
android:fillColor
=
"#000000"
android:pathData
=
"M300,70 l 0,-70 70,70 0,0 -70,70z"
/>
</
group
>
</
vector
>
1
2
3
4
5
6
7
8
9
10
<!-- res/drawable/animvectordrawable.xml -->
<
animated-vector
xmlns:android
=
"http://schemas.android.com/apk/res/android"
android:drawable
=
"@drawable/vectordrawable"
>
<
target
android:name
=
"rotationGroup"
android:animation
=
"@anim/rotation"
/>
<
target
android:name
=
"v"
android:animation
=
"@anim/path_morph"
/>
</
animated-vector
>
1
2
3
4
5
6
<!-- res/anim/rotation.xml -->
<
objectAnimator
android:duration
=
"6000"
android:propertyName
=
"rotation"
android:valueFrom
=
"0"
android:valueTo
=
"360"
/>
1
2
3
4
5
6
7
8
9
<!-- res/anim/path_morph.xml -->
<
set
xmlns:android
=
"http://schemas.android.com/apk/res/android"
>
<
objectAnimator
android:duration
=
"3000"
android:propertyName
=
"pathData"
android:valueFrom
=
"M300,70 l 0,-70 70,70 0,0 -70,70z"
android:valueTo
=
"M300,70 l 0,-70 70,0 0,140 -70,0 z"
android:valueType
=
"pathType"
/>
</
set
>
阅读全文
0 0
- Android动画知识汇总
- Android动画知识汇总
- Android动画知识汇总(初 xml篇)
- Android小知识汇总
- Android开发知识汇总
- android知识链接汇总
- android知识链接汇总
- android知识链接汇总
- android ListView 知识汇总
- android知识大汇总
- android知识汇总
- android开发知识汇总
- eoe android 知识汇总
- Android开发知识汇总
- android NDK 知识汇总
- Android json知识汇总
- android ndk知识汇总
- android开发知识汇总
- SSM框架整合技术
- SSH与SSM学习之hibernate08——hibernate中的实体规则
- 一首词——雨霖铃
- Dubbo : ZooKeeper安装配置(一)
- builder模式
- Android动画知识汇总
- Android动画Animator开发问题
- 嵌入式常用的加密芯片
- Python排序字典
- Android 开源框架Universal-Image-Loader完全解析(一)--- 基本介绍及使用
- Android 开源框架Universal-Image-Loader完全解析(二)--- 图片缓存策略详解
- 帮助文档 | JDK 1.8 中的 Timer 类
- Android 开源框架Universal-Image-Loader完全解析(三)---源代码解读
- ShaderLab: SubShader Tags