【原文:http://blog.csdn.net/corder_raine/article/details/8317407】
目录
第十二天.Android动画技术
12.1 Tween动画
12.1.1 动画实现
Ø Tween动画
u 对场景中的对象不断进行图像变换,如平移、缩放、旋转。
u Frame帧动画
u 顺序播放事先做好的图像,如电影。
u GIF动画
12.1.2 代码实现Tween动画1
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
/* 装载资源 */
Bitmap mBitQQ mBitQQ = ((BitmapDrawable) getResources().getDrawable(R.drawable.qq)).getBitmap();
/* 绘制图片 */
canvas.drawBitmap(mBitQQ, 0, 0, null);
/* 创建Alpha动画 */
private Animation mAnimationAlpha = newAlphaAnimation(0.1f, 1.0f);
/* 设置动画的时间 */
mAnimationAlpha.setDuration(3000);
/* 开始播放动画 */
this.startAnimation(mAnimationAlpha);
/* 创建Scale动画 */
private Animation mAnimationScale =newScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f,Animation.RELATIVE_TO_SELF, 0.5f,Animation.RELATIVE_TO_SELF, 0.5f);
/* 设置动画的时间 */
mAnimationScale.setDuration(500);
/* 开始播放动画 */
this.startAnimation(mAnimationScale);
12.1.3 代码实现Tween动画2/* 创建Translate动画 */
private Animation mAnimationTranslate =new TranslateAnimation(10, 100,10, 100);
/* 设置动画的时间 */
mAnimationTranslate.setDuration(1000);
/* 开始播放动画 */
this.startAnimation(mAnimationTranslate);
/* 创建Rotate动画 */
private Animation mAnimationRotate=newRotateAnimation(0.0f, +360.0f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF, 0.5f);
/* 设置动画的时间 */
mAnimationRotate.setDuration(1000);
/* 开始播放动画 */
this
.startAnimation(mAnimationRotate);
|
12.2.4 代码实现Tween动画:main.xml
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
|
android:orientation
=
"vertical"
android:layout_width
=
"fill_parent"
android:layout_height
=
"fill_parent"
>
<
TextView
android:layout_width
=
"fill_parent"
android:layout_height
=
"wrap_content"
android:text
=
"@string/hello"
/>
<
Button
android:id
=
"@+id/AlphaAnimation"
android:layout_width
=
"fill_parent"
android:layout_height
=
"wrap_content"
android:text
=
"Alpha动画"
/>
<
Button
android:id
=
"@+id/ScaleAnimation"
android:layout_width
=
"fill_parent"
android:layout_height
=
"wrap_content"
android:text
=
"Scale动画"
/>
<
Button
android:id
=
"@+id/TranslateAnimation"
android:layout_width
=
"fill_parent"
android:layout_height
=
"wrap_content"
android:text
=
"Translate动画"
/>
<
Button
android:id
=
"@+id/RotateAnimation"
android:layout_width
=
"fill_parent"
android:layout_height
=
"wrap_content"
android:text
=
"Rotate动画"
/>
</
LinearLayout
>
|
12.2.5 XML布局实现Tween动画
01
02
03
04
05
06
07
08
09
10
11
12
13
|
/* 装载动画布局 */
mAnimationAlpha =AnimationUtils.loadAnimation(mContext,R.anim.alpha_animation);
/* 开始播放动画 */
this.startAnimation(mAnimationAlpha);
/* 装载动画布局 */
mAnimationScale =AnimationUtils.loadAnimation(mContext,R.anim.scale_animation);
this.startAnimation(mAnimationScale);
/* 装载动画布局 */
mAnimationTranslate =AnimationUtils.loadAnimation(mContext,R.anim.translate_animation);
this.startAnimation(mAnimationTranslate);
/* 装载动画布局 */
mAnimationRotate =AnimationUtils.loadAnimation(mContext,R.anim.rotate_animation);
this.startAnimation(mAnimationRotate);
|
R.anim.alpha_animation
1
2
3
4
5
6
7
|
<
alpha
android:fromAlpha
=
"0.1"
android:toAlpha
=
"1.0"
android:duration
=
"2000"
/>
</
set
>
|
R.anim.scale_animation
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
|
<
scale
android:interpolator
=
"@android:anim/accelerate_decelerate_interpolator"
android:fromXScale
=
"0.0"
android:toXScale
=
"1.0"
android:fromYScale
=
"0.0"
android:toYScale
=
"1.0"
android:pivotX
=
"50%"
android:pivotY
=
"50%"
android:fillAfter
=
"false"
android:duration
=
"500"
/>
</
set
>
|
R.anim.translate_animation
1
2
3
4
5
6
7
8
9
|
<
translate
android:fromXDelta
=
"10"
android:toXDelta
=
"100"
android:fromYDelta
=
"10"
android:toYDelta
=
"100"
android:duration
=
"1000"
/>
</
set
>
|
R.anim.rotate_animation
01
02
03
04
05
06
07
08
09
10
11
|
<
rotate
android:interpolator
=
"@android:anim/accelerate_decelerate_interpolator"
android:fromDegrees
=
"0"
android:toDegrees
=
"+360"
android:pivotX
=
"50%"
android:pivotY
=
"50%"
android:duration
=
"1000"
/>
</
set
>
|
** 案例AnimationDemo2
12.2 Frame帧动画
12.2.1 代码实现Frame动画
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
|
/* 实例化AnimationDrawable对象 */
private AnimationDrawable frameAnimation = newAnimationDrawable();
/*装载资源 */
//这里用一个循环了装载所有名字类似的资源,如“a1.......15.png”的图片
for(int i = 1; i <= 15; i++){
intid = getResources().getIdentifier("a" + i, "drawable", mContext.getPackageName());
Drawable mBitAnimation =getResources().getDrawable(id);
/*为动画添加一帧 */
//参数mBitAnimation是该帧的图片
//参数500是该帧显示的时间,按毫秒计算
frameAnimation.addFrame(mBitAnimation,500);
}
/*设置播放模式是否循环false表示循环而true表示不循环 */
frameAnimation.setOneShot(false );
/*设置本类将要显示这个动画 */
this.setBackgroundDrawable(frameAnimation);
/*开始播放动画 */
frameAnimation.start();
|
** 案例AnimationDrawableDemo
12.2.2 XML实现Frame动画
01
02
03
04
05
06
07
08
09
10
11
12
13
14
|
/* 定义AnimationDrawable动画对象 */
private AnimationDrawable frameAnimation= null;
/* 定义一个ImageView用来显示动画 */
ImageView img = new ImageView(mContext);
/* 装载动画布局文件 */
img.setBackgroundResource(R.anim.frameanimation);
/* 构建动画 */
private AnimationDrawable frameAnimation= (AnimationDrawable) img.getBackground();
/* 设置是否循环 */
frameAnimation.setOneShot( false );
/* 设置该类显示的动画 */
this.setBackgroundDrawable(frameAnimation);
/* 开始播放动画 */
frameAnimation.start();
|
frameanimation.xml
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
|
android:oneshot
=
"false"
>
<
item
android:drawable
=
"@drawable/a1"
android:duration
=
"500"
/>
<
item
android:drawable
=
"@drawable/a2"
android:duration
=
"500"
/>
<
item
android:drawable
=
"@drawable/a3"
android:duration
=
"500"
/>
<
item
android:drawable
=
"@drawable/a4"
android:duration
=
"500"
/>
<
item
android:drawable
=
"@drawable/a5"
android:duration
=
"500"
/>
<
item
android:drawable
=
"@drawable/a6"
android:duration
=
"500"
/>
<
item
android:drawable
=
"@drawable/a7"
android:duration
=
"500"
/>
<
item
android:drawable
=
"@drawable/a8"
android:duration
=
"500"
/>
<
item
android:drawable
=
"@drawable/a9"
android:duration
=
"500"
/>
<
item
android:drawable
=
"@drawable/a10"
android:duration
=
"500"
/>
<
item
android:drawable
=
"@drawable/a11"
android:duration
=
"500"
/>
<
item
android:drawable
=
"@drawable/a12"
android:duration
=
"500"
/>
<
item
android:drawable
=
"@drawable/a13"
android:duration
=
"500"
/>
<
item
android:drawable
=
"@drawable/a14"
android:duration
=
"500"
/>
<
item
android:drawable
=
"@drawable/a15"
android:duration
=
"500"
/>
</
animation-list
>
|
** 案例AnimationDrawableDemo2
12.3 GIF动画
Ø 简单介绍案例GifAnimationDemo
12.4 全屏与横屏技术
01
02
03
04
05
06
07
08
09
10
11
12
13
|
public
void
onCreate(BundlesavedInstanceState){
super
.onCreate(savedInstanceState);
/*设置为无标题栏 */
requestWindowFeature(Window.FEATURE_NO_TITLE);
/*设置为全屏模式 */
getWindow().setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
/*设置为横屏 */
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setContentView(R.layout.main);
}
|
** 全屏技术在拍照、录制视频、游戏中很常用
12.5 获取屏幕属性
01
02
03
04
05
06
07
08
09
10
11
12
13
14
|
public
void
onCreate(BundlesavedInstanceState){
super
.onCreate(savedInstanceState);
setContentView(R.layout.main);
/*定义DisplayMetrics对象 */
DisplayMetricsdm = new DisplayMetrics();
/*取得窗口属性 */
getWindowManager().getDefaultDisplay().getMetrics(dm);
/*窗口的宽度 */
intscreenWidth = dm.widthPixels;
/*窗口的高度 */
intscreenHeight = dm.heightPixels;
mTextView= (TextView) findViewById(R.id.TextView01);
mTextView.setText(
"屏幕宽度:"
+ screenWidth +
"\n屏幕高度:"
+ screenHeight);
}
|
示例下载