转自:http://www.itivy.com/android/archive/2011/6/21/android-image-rotate.html
Android动画有2种,一种是Tween Animation,另一种是Frame Animation,先说说Tween动画吧。
Tween动画是对视图对象中的内容进行一系列简单的转换,比如位置的移动,大小的缩放,旋转,透明度得变化等等。Tween动画可以写到一个xml文件中,就像定义布局文件一样,当然,也可以写到android代码中,不过推荐写到xml文件中,因为它具备的阅读性,可重用性大大超过了硬编码。xml文件放在工程的res/anim目录中,这个目录中要包含一个根元素,可以是<scale>,<translate>,<alpha>或者<rotate>,当然,这些元素都可以放到一个动画集合<set>中,默认情况下,所有的动画指令都是同时发生的,为了让他们按顺序发生,需要设置一个特殊的属性 startOffset。下面定义了一个动画的集合:
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
|
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
set
xmlns:android
=
"http://schemas.android.com/apk/res/android"
android:shareInterpolator
=
"false"
>
<
scale
android:interpolator
=
"@android:anim/accelerate_decelerate_interpolator"
android:fromXScale
=
"1.0"
android:toXScale
=
"1.4"
android:fromYScale
=
"1.0"
android:toYScale
=
"0.6"
android:pivotX
=
"50%"
android:pivotY
=
"50%"
android:fillAfter
=
"false"
android:duration
=
"700"
/>
<
set
android:interpolator
=
"@android:anim/decelerate_interpolator"
>
<
scale
android:fromXScale
=
"1.4"
android:toXScale
=
"0.0"
android:fromYScale
=
"0.6"
android:toYScale
=
"0.0"
android:pivotX
=
"50%"
android:pivotY
=
"50%"
android:startOffset
=
"700"
android:duration
=
"400"
android:fillBefore
=
"false"
/>
<
rotate
android:fromDegrees
=
"0"
android:toDegrees
=
"-45"
android:toYScale
=
"0.0"
android:pivotX
=
"50%"
android:pivotY
=
"50%"
android:startOffset
=
"700"
android:duration
=
"400"
/>
</
set
>
</
set
>
|
1
2
3
4
5
6
7
8
9
10
11
|
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
LinearLayout
xmlns:android
=
"http://schemas.android.com/apk/res/android"
android:layout_width
=
"match_parent"
android:layout_height
=
"match_parent"
>
<
ImageView
android:id
=
"@+id/imageView1"
android:src
=
"@drawable/duola"
android:layout_width
=
"match_parent"
android:layout_height
=
"match_parent"
></
ImageView
>
</
LinearLayout
>
|
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
|
package com.test.shang;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.TransitionDrawable;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
public
class
TestStyle extends Activity {
AnimationDrawable animationDrawable;
@Override
protected
void
onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
ImageView iv = (ImageView) findViewById(R.id.imageView1);
Animation animation = (AnimationSet) AnimationUtils.loadAnimation(
this
, R.anim.anim_set);
iv.setAnimation(animation);
animation.start();
}
}
|
1
2
3
4
5
6
7
|
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
animation-list
xmlns:android
=
"http://schemas.android.com/apk/res/android"
android:oneshot
=
"true"
>
<
item
android:drawable
=
"@drawable/register"
android:duration
=
"500"
/>
<
item
android:drawable
=
"@drawable/duola"
android:duration
=
"500"
/>
<
item
android:drawable
=
"@drawable/icon"
android:duration
=
"500"
/>
</
animation-list
>
|
1
2
3
4
5
6
7
8
9
10
|
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
LinearLayout
xmlns:android
=
"http://schemas.android.com/apk/res/android"
android:layout_width
=
"match_parent"
android:layout_height
=
"match_parent"
>
<
ImageView
android:id
=
"@+id/imageView1"
android:layout_width
=
"match_parent"
android:layout_height
=
"match_parent"
></
ImageView
>
</
LinearLayout
>
|
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
|
package com.test.shang;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.TransitionDrawable;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
public
class
TestStyle extends Activity {
AnimationDrawable animationDrawable;
@Override
protected
void
onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
ImageView iv = (ImageView) findViewById(R.id.imageView1);
iv.setBackgroundResource(R.anim.anim_list);
animationDrawable = (AnimationDrawable) iv.getBackground();
}
@Override
public
boolean onTouchEvent (MotionEvent
event
) {
if
(
event
.getAction() == MotionEvent.ACTION_DOWN) {
animationDrawable.start();
return
true
;
}
return
super.onTouchEvent(
event
);
}
}
|
这里需要注意的 是:AnimationDrawable在调用OnCreate的过程中不能调用start(),这是因为AnimationDrawable不能在不完 全的窗口上运行,需要一个操作来触发,如果你想立即播放动画,没有必要的交互,你可以在onWindowFocusChanged()方法中调用它,这样 它将会成为窗口焦点。
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
package com.test.shang;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
public
class
BitmapTest extends Activity {
@Override
protected
void
onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle(
"测试图片的缩放和旋转"
);
LinearLayout layout =
new
LinearLayout(
this
);
//加载需要操作的图片,这里是机器猫的图片
Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(), R.drawable.duola);
//获取这个图片的宽和高
int
width = bitmapOrg.getWidth();
int
height = bitmapOrg.getHeight();
//定义预转换成的图片的宽和高
int
newWidth = 200;
int
newHight = 200;
//计算缩放率,新尺寸除原尺寸
float
scaleWidth = (
float
)newWidth/width;
float
scaleHeight = (
float
)newHight/height;
//创建操作图片用的matrix对象
Matrix matrix =
new
Matrix();
//缩放图片动作
matrix.postScale(scaleWidth, scaleHeight);
//旋转图片动作
matrix.postRotate(45);
//创建新的图片
Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, width, height, matrix,
true
);
|