Android三种动画介绍(property动画,Tween动画、Frame动画)

 

Animation

The Android framework provides two animation systems: property animation (introduced in Android 3.0) and view animation. Both animation systems are viable options, but the property animation system, in general, is the preferred method to use, because it is more flexible and offers more features. In addition to these two systems, you can utilize Drawable animation, which allows you to load drawable resources and display them one frame after another.

Property Animation
(可以对view或non-view的任意属性变化来形成动画)
(对于view来说只是一二个属性的动画可以用ObjectAnimator,但是如果是多个属性同时动画或者为了使用方便可以使用ViewPropertyAnimator)
http://blog.csdn.net/xushuaic/article/details/40424379
http://blog.csdn.net/jjwwmlp456/article/details/38518657
Introduced in Android 3.0 (API level 11), the property animation system lets you animate properties of any object, including ones that are not rendered to the screen. The system is extensible and lets you animate properties of custom types as well.
View Animation
(补间动画-Tween动画 是一种老式系统动画,只是简单的位移,缩放,透明变化等等)
View Animation is the older system and can only be used for Views. It is relatively easy to setup and offers enough capabilities to meet many application’s needs.
Drawable Animation
(帧动画-frame动画,是利用多张图片连续播放形成动画)
Drawable animation involves displaying Drawable resources one after another, like a roll of film. This method of animation is useful if you want to animate things that are easier to represent with Drawable resources, such as a progression of bitmaps.

 

 

http://developer.android.com/guide/topics/graphics/view-animation.html

View Animation

You can use the view animation system to perform tweened animation on Views. Tween animation calculates the animation with information such as the start point, end point, size, rotation, and other common aspects of an animation.

A tween animation can perform a series of simple transformations (position, size, rotation, and transparency) on the contents of a View object. So, if you have a TextView object, you can move, rotate, grow, or shrink the text. If it has a background image, the background image will be transformed along with the text. The animation package provides all the classes used in a tween animation.

A sequence of animation instructions defines the tween animation, defined by either XML or Android code. As with defining a layout, an XML file is recommended because it’s more readable, reusable, and swappable than hard-coding the animation. In the example below, we use XML. (To learn more about defining an animation in your application code, instead of XML, refer to the AnimationSet class and other Animation subclasses.)

The animation instructions define the transformations that you want to occur, when they will occur, and how long they should take to apply. Transformations can be sequential or simultaneous – for example, you can have the contents of a TextView move from left to right, and then rotate 180 degrees, or you can have the text move and rotate simultaneously. Each transformation takes a set of parameters specific for that transformation (starting size and ending size for size change, starting angle and ending angle for rotation, and so on), and also a set of common parameters (for instance, start time and duration). To make several transformations happen simultaneously, give them the same start time; to make them sequential, calculate the start time plus the duration of the preceding transformation.

The animation XML file belongs in the res/anim/ directory of your Android project. The file must have a single root element: this will be either a single <alpha>, <scale>, <translate>, <rotate>, interpolator element, or <set> element that holds groups of these elements (which may include another <set>). By default, all animation instructions are applied simultaneously. To make them occur sequentially, you must specify the startOffset attribute, as shown in the example below.

The following XML from one of the ApiDemos is used to stretch, then simultaneously spin and rotate a View object.

<set 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>

Screen coordinates (not used in this example) are (0,0) at the upper left hand corner, and increase as you go down and to the right.

Some values, such as pivotX, can be specified relative to the object itself or relative to the parent. Be sure to use the proper format for what you want (“50” for 50% relative to the parent, or “50%” for 50% relative to itself).

You can determine how a transformation is applied over time by assigning an Interpolator. Android includes several Interpolator subclasses that specify various speed curves: for instance, AccelerateInterpolator tells a transformation to start slow and speed up. Each one has an attribute value that can be applied in the XML.

With this XML saved as hyperspace_jump.xml in the res/anim/ directory of the project, the following code will reference it and apply it to an ImageView object from the layout.

ImageView spaceshipImage = (ImageView) findViewById(R.id.spaceshipImage);
Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(this, R.anim.hyperspace_jump);
spaceshipImage.startAnimation(hyperspaceJumpAnimation);

As an alternative to startAnimation(), you can define a starting time for the animation with Animation.setStartTime(), then assign the animation to the View with View.setAnimation().

For more information on the XML syntax, available tags and attributes, see Animation Resources.

Note: Regardless of how your animation may move or resize, the bounds of the View that holds your animation will not automatically adjust to accommodate it. Even so, the animation will still be drawn beyond the bounds of its View and will not be clipped. However, clipping will occur if the animation exceeds the bounds of the parent View.

 

http://developer.android.com/guide/topics/graphics/drawable-animation.html

Drawable Animation

Drawable animation lets you load a series of Drawable resources one after another to create an animation. This is a traditional animation in the sense that it is created with a sequence of different images, played in order, like a roll of film. The AnimationDrawable class is the basis for Drawable animations.

While you can define the frames of an animation in your code, using the AnimationDrawable class API, it’s more simply accomplished with a single XML file that lists the frames that compose the animation. The XML file for this kind of animation belongs in the res/drawable/ directory of your Android project. In this case, the instructions are the order and duration for each frame of the animation.

The XML file consists of an <animation-list> element as the root node and a series of child <item> nodes that each define a frame: a drawable resource for the frame and the frame duration. Here’s an example XML file for a Drawable animation:

<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="true">
    <item android:drawable="@drawable/rocket_thrust1" android:duration="200" />
    <item android:drawable="@drawable/rocket_thrust2" android:duration="200" />
    <item android:drawable="@drawable/rocket_thrust3" android:duration="200" />
</animation-list>

This animation runs for just three frames. By setting the android:oneshot attribute of the list to true, it will cycle just once then stop and hold on the last frame. If it is set false then the animation will loop. With this XML saved as rocket_thrust.xml in the res/drawable/ directory of the project, it can be added as the background image to a View and then called to play. Here’s an example Activity, in which the animation is added to an ImageView and then animated when the screen is touched:

AnimationDrawable rocketAnimation;

public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  ImageView rocketImage = (ImageView) findViewById(R.id.rocket_image);
  rocketImage.setBackgroundResource(R.drawable.rocket_thrust);
  rocketAnimation = (AnimationDrawable) rocketImage.getBackground();
}

public boolean onTouchEvent(MotionEvent event) {
  if (event.getAction() == MotionEvent.ACTION_DOWN) {
    rocketAnimation.start();
    return true;
  }
  return super.onTouchEvent(event);
}

It’s important to note that the start() method called on the AnimationDrawable cannot be called during the onCreate() method of your Activity, because the AnimationDrawable is not yet fully attached to the window. If you want to play the animation immediately, without requiring interaction, then you might want to call it from the onWindowFocusChanged() method in your Activity, which will get called when Android brings your window into focus.

For more information on the XML syntax, available tags and attributes, see Animation Resources.

Android中的动画类型有两种,一种是Tween动画、还有一种是Frame动画。Tween动画,这种实现方式可以使视图组件移动、放大、缩小以及产生透明度的变化;另一种Frame动画,传统的动画方法,通过顺序的播放排列好的图片来实现,类似电影。下面将一一详细介绍:

本博客参考自网络,结合自己一点理解,实为学习之用,不为其他目的。

一,Tween动画

又称“补间动画”、“中间动画”,最早接触Tween类是在学习Flash时候,使用ActionScript做动画的时候,使用过类Tween。

Tween动画主要的功能是在绘制动画前设置动画绘制的轨迹,包括时间, 位置 ,等等。但是Tween动画的缺点是它只能设置起始点与结束点的两帧,中间过程全部由系统帮我们完成。所以在帧数比较多的游戏开发中是不太会用到它的。
Tween一共提供了4中动画的效果

Scale:缩放动画
Rotate:旋转动画
Translate:移动动画
Alpha::透明渐变动画

Tween与Frame动画类似都需要在res\anim路径下创建动画的 布局文件

1)Scale动画

  1. import android.app.Activity;
  2. import android.os.Bundle;
  3. import android.view.View;
  4. import android.view.View.OnClickListener;
  5. import android.view.animation.Animation;
  6. import android.view.animation.AnimationUtils;
  7. import android.widget.Button;
  8. import android.widget.ImageView;
  9. public class ScaleActivity extends Activity {
  10.     Button mButton0 = null;//缩小动画
  11.     Button mButton1 = null;//放大动画
  12.     ImageView mImageView = null; //显示动画
  13.     Animation mLitteAnimation = null; //缩小动画
  14.     Animation mBigAnimation = null; //放大动画
  15.     @Override
  16.     public void onCreate(Bundle savedInstanceState) {
  17.     super.onCreate(savedInstanceState);
  18.     setContentView(R.layout.scale);
  19.     mImageView = (ImageView)findViewById(R.id.imageView);
  20.     /**加载缩小与放大动画**/
  21.     mLitteAnimation = AnimationUtils.loadAnimation(this, R.anim.scalelitte);
  22.     mBigAnimation = AnimationUtils.loadAnimation(this, R.anim.scalebig);
  23.     mButton0 = (Button)findViewById(R.id.button0);
  24.     mButton0.setOnClickListener(new OnClickListener() {
  25.         @Override
  26.         public void onClick(View arg0) {
  27.         /**播放缩小动画**/
  28.         mImageView.startAnimation(mLitteAnimation);
  29.         }
  30.     });
  31.     mButton1 = (Button)findViewById(R.id.button1);
  32.     mButton1.setOnClickListener(new OnClickListener() {
  33.         @Override
  34.         public void onClick(View arg0) {
  35.         /**播放放大动画**/
  36.         mImageView.startAnimation(mBigAnimation);
  37.         }
  38.     });
  39.     }
  40. }
  1. <scale>标签为缩放节点
  2. android:fromXscale=“1.0” 表示开始时X轴缩放比例为 1.0 (原图大小 * 1.0 为原图大小)
  3. android:toXscale=“0.0”   表示结束时X轴缩放比例为0.0(原图大小 *0.0 为缩小到看不见)
  4. android:fromYscale=“1.0” 表示开始时Y轴缩放比例为 1.0 (原图大小 * 1.0 为原图大小)
  5. android:toYscale=“0.0”   表示结束时Y轴缩放比例为0.0(原图大小 *0.0 为缩小的看不到了)
  6. android:pivotX=“50%”     X轴缩放的位置为中心点
  7. android:pivotY=“50%”     Y轴缩放的位置为中心点
  8. android:duration=“2000”  动画播放时间 这里是2000毫秒也就是2秒

/anim/scalelitte.xml

  1. <?xml version=“1.0” encoding=“utf-8”?>
  2. <scale
  3.       xmlns:android=“http://schemas.android.com/apk/res/android”
  4.       android:fromXScale=“0.0”
  5.       android:toXScale=“1.0”
  6.       android:fromYScale=“0.0”
  7.       android:toYScale=“1.0”
  8.       android:pivotX=“50%”
  9.       android:pivotY=“50%”
  10.       android:duration=“2000”>
  11. </scale>

/anim/scalebig.xml

  1. <?xml version=“1.0” encoding=“utf-8”?>
  2. <scale xmlns:android=“http://schemas.android.com/apk/res/android”
  3.             android:fromXScale=“1.0”
  4.             android:toXScale=“0.0”
  5.             android:fromYScale=“1.0”
  6.             android:toYScale=“0.0”
  7.             android:pivotX=“50%”
  8.             android:pivotY=“50%”
  9.             android:duration=“2000”>
  10. </scale>

如果在代码中,加载动画,而不用xml配置动画

  1. mLitteAnimation =  new ScaleAnimation(0.0f, 1.0f, 0.0f,  1.0f,
  2.                   Animation.RELATIVE_TO_SELF, 0.5f,
  3.                   Animation.RELATIVE_TO_SELF, 0.5f);
  4.               mLitteAnimation.setDuration(2000);

2)Rotate旋转动画


  1. import android.app.Activity;
  2. import android.os.Bundle;
  3. import android.view.View;
  4. import android.view.View.OnClickListener;
  5. import android.view.animation.Animation;
  6. import android.view.animation.AnimationUtils;
  7. import android.widget.Button;
  8. import android.widget.ImageView;
  9. public class RotateActivity extends Activity {
  10.     /**向左旋转动画按钮**/
  11.     Button mButton0 = null;
  12.     /**向右旋转动画按钮**/
  13.     Button mButton1 = null;
  14.     /**显示动画的ImageView**/
  15.     ImageView mImageView = null;
  16.     /**向左旋转动画**/
  17.     Animation mLeftAnimation = null;
  18.     /**向右旋转动画**/
  19.     Animation mRightAnimation = null;
  20.     @Override
  21.     public void onCreate(Bundle savedInstanceState) {
  22.     super.onCreate(savedInstanceState);
  23.     setContentView(R.layout.retate);
  24.     /**拿到ImageView对象**/
  25.     mImageView = (ImageView)findViewById(R.id.imageView);
  26.     /**加载向左与向右旋转动画**/
  27.     mLeftAnimation = AnimationUtils.loadAnimation(this, R.anim.retateleft);
  28.     mRightAnimation = AnimationUtils.loadAnimation(this, R.anim.retateright);
  29.     mButton0 = (Button)findViewById(R.id.button0);
  30.     mButton0.setOnClickListener(new OnClickListener() {
  31.         @Override
  32.         public void onClick(View arg0) {
  33.         /**播放向左旋转动画**/
  34.         mImageView.startAnimation(mLeftAnimation);
  35.         }
  36.     });
  37.     mButton1 = (Button)findViewById(R.id.button1);
  38.     mButton1.setOnClickListener(new OnClickListener() {
  39.         @Override
  40.         public void onClick(View arg0) {
  41.         /**播放向右旋转动画**/
  42.         mImageView.startAnimation(mRightAnimation);
  43.         }
  44.     });
  45.     }
  46. }
  1. <rotate>标签为旋转节点
  2. Tween一共为我们提供了3种动画渲染模式。
  3. android:interpolator=“@android:anim/accelerate_interpolator” 设置动画渲染器为加速动画(动画播放中越来越快)
  4. android:interpolator=“@android:anim/decelerate_interpolator” 设置动画渲染器为减速动画(动画播放中越来越慢)
  5. android:interpolator=“@android:anim/accelerate_decelerate_interpolator” 设置动画渲染器为先加速在减速(开始速度最快 逐渐减慢)
  6. 如果不写的话 默认为匀速运动
  7. android:fromDegrees=“+360”设置动画开始的角度
  8. android:toDegrees=“0”设置动画结束的角度
  9. 这个动画布局设置动画将向左做360度旋转加速运动。

/anim/retateleft.xml

  1. <?xml version=“1.0” encoding=“utf-8”?>
  2. <rotate xmlns:android=“http://schemas.android.com/apk/res/android”
  3.         android:interpolator=“@android:anim/accelerate_interpolator”
  4.         android:fromDegrees=“+360”
  5.         android:toDegrees=“0”
  6.         android:pivotX=“50%”
  7.         android:pivotY=“50%”
  8.         android:duration=“2000”
  9. />

/anim/retateright.xml

  1. <?xml version=“1.0” encoding=“utf-8”?>
  2. <rotate  xmlns:android=“http://schemas.android.com/apk/res/android”
  3.         android:interpolator=“@android:anim/decelerate_interpolator”
  4.         android:fromDegrees=“0”
  5.         android:toDegrees=“+360”
  6.         android:pivotX=“50%”
  7.         android:pivotY=“50%”
  8.         android:duration=“2000”
  9. />

如果在代码中加载动画,而不用xml配置,代码如下

  1. mLeftAnimation = new RotateAnimation(360.0f, 0.0f,
  2.         Animation.RELATIVE_TO_SELF, 0.5f,
  3.         Animation.RELATIVE_TO_SELF, 0.5f);
  4.              mLeftAnimation.setDuration(2000);

3)Translate移动动画

  1. import android.app.Activity;
  2. import android.os.Bundle;
  3. import android.view.animation.Animation;
  4. import android.view.animation.AnimationUtils;
  5. import android.widget.ImageView;
  6. public class TranslateActivity extends Activity {
  7.     /**显示动画的ImageView**/
  8.     ImageView mImageView = null;
  9.     /**移动动画**/
  10.     Animation mAnimation = null;
  11.     @Override
  12.     public void onCreate(Bundle savedInstanceState) {
  13.     super.onCreate(savedInstanceState);
  14.     setContentView(R.layout.translate);
  15.     /**拿到ImageView对象**/
  16.     mImageView = (ImageView)findViewById(R.id.imageView);
  17.     /**加载移动动画**/
  18.     mAnimation = AnimationUtils.loadAnimation(this, R.anim.translate);
  19.     /**播放移动动画**/
  20.     mImageView.startAnimation(mAnimation);
  21.     }
  22. }

/layout/translate.xml

  1. <?xml version=“1.0” encoding=“utf-8”?>
  2. <LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
  3.     android:orientation=“vertical”
  4.     android:layout_width=“fill_parent”
  5.     android:layout_height=“fill_parent”
  6.     >
  7.   <ImageView
  8.    android:id=“@+id/imageView”
  9.    android:src=“@drawable/images”
  10.    android:layout_width=“wrap_content”
  11.    android:layout_height=“wrap_content”
  12.  />
  13. </LinearLayout>

/anim/translate.xml

  1. <?xml version=“1.0” encoding=“utf-8”?>
  2. <translate  xmlns:android=“http://schemas.android.com/apk/res/android”
  3.     android:fromXDelta=“0”
  4.     android:toXDelta=“320”
  5.     android:fromYDelta=“0”
  6.     android:toYDelta=“480”
  7.     android:duration=“2000”
  8.     android:repeatCount=“infinite”
  9. />

说明:

  1. <?xml version=“1.0” encoding=“utf-8”?>
  2. <translate  xmlns:android=“http://schemas.android.com/apk/res/android”
  3.     android:fromXDelta=“0”
  4.     android:toXDelta=“320”
  5.     android:fromYDelta=“0”
  6.     android:toYDelta=“480”
  7.     android:duration=“2000”
  8.     android:repeatCount=“infinite”
  9. />

代码中加载动画:

  1. mAnimation = new TranslateAnimation(0, 320, 0, 480);
  2. mAnimation.setDuration(2000);

4 )Alpha:透明渐变动画

  1. import android.app.Activity;
  2. import android.os.Bundle;
  3. import android.view.animation.Animation;
  4. import android.view.animation.AnimationUtils;
  5. import android.widget.ImageView;
  6. public class AlphaActivity extends Activity {
  7.     /**显示动画的ImageView**/
  8.     ImageView mImageView = null;
  9.     /**透明动画**/
  10.     Animation mAnimation = null;
  11.     @Override
  12.     public void onCreate(Bundle savedInstanceState) {
  13.     super.onCreate(savedInstanceState);
  14.     setContentView(R.layout.translate);
  15.     /**拿到ImageView对象**/
  16.     mImageView = (ImageView)findViewById(R.id.imageView);
  17.     /**加载透明动画**/
  18.     mAnimation = AnimationUtils.loadAnimation(this, R.anim.alpha);
  19.     /**播放透明动画**/
  20.     mImageView.startAnimation(mAnimation);
  21.     }
  22. }

/anim/alpha.xml

  1. <?xml version=“1.0” encoding=“utf-8”?>
  2. <alpha  xmlns:android=“http://schemas.android.com/apk/res/android”
  3.     android:fromAlpha=“1.0”
  4.     android:toAlpha=“0.0”
  5.     android:repeatCount=“infinite”
  6.     android:duration=“2000”>
  7. </alpha>

说明:

  1. <alpha>标签为alpha透明度节点
  2. android:fromAlpha=“1.0” 设置动画起始透明度为1.0 表示完全不透明
  3. android:toAlpha=“0.0”设置动画结束透明度为0.0 表示完全透明
  4. 也就是说alpha的取值范围为0.0 – 1.0 之间

手动加载动画:

  1. mAnimation = new AlphaAnimation(1.0f, 0.0f);
  2. mAnimation.setDuration(2000);

5)综合动画

可以将上面介绍的4种动画设置在一起同时进行播放,那么就须要使用<set>标签将所有须要播放的动画放在一起。

这个动画布局设置动画同时播放移动、渐变、旋转。

  1. import android.app.Activity;
  2. import android.os.Bundle;
  3. import android.view.animation.Animation;
  4. import android.view.animation.AnimationUtils;
  5. import android.widget.ImageView;
  6. public class AllActivity extends Activity {
  7.     ImageView mImageView = null;
  8.     Animation mAnimation = null;
  9.     @Override
  10.     public void onCreate(Bundle savedInstanceState) {
  11.     super.onCreate(savedInstanceState);
  12.     setContentView(R.layout.translate);
  13.     mImageView = (ImageView)findViewById(R.id.imageView);
  14.     mAnimation = AnimationUtils.loadAnimation(this, R.anim.all);
  15.     mImageView.startAnimation(mAnimation);
  16.     }
  17. }

/anim/all.xml

  1. <?xml version=“1.0” encoding=“utf-8”?>
  2. <set xmlns:android=“http://schemas.android.com/apk/res/android”>
  3.     <rotate
  4.         android:interpolator=“@android:anim/accelerate_interpolator”
  5.         android:fromDegrees=“+360”
  6.         android:toDegrees=“0”
  7.         android:pivotX=“50%”
  8.         android:pivotY=“50%”
  9.         android:duration=“2000”
  10.         android:repeatCount=“infinite”
  11.     />
  12.     <alpha  android:fromAlpha=“1.0”
  13.     android:toAlpha=“0.0”
  14.     android:repeatCount=“infinite”
  15.     android:duration=“2000”>
  16.     </alpha>
  17.  <translate
  18.     android:fromXDelta=“0”
  19.     android:toXDelta=“320”
  20.     android:fromYDelta=“0”
  21.     android:toYDelta=“480”
  22.     android:duration=“2000”
  23.     android:repeatCount=“infinite”
  24. />
  25. </set>

二,AnimationDrable实现Frame动画(设计游戏专用,嘎嘎嘎)感谢宣教主分享

  1. import android.app.Activity;
  2. import android.graphics.drawable.AnimationDrawable;
  3. import android.os.Bundle;
  4. import android.util.Log;
  5. import android.view.View;
  6. import android.view.View.OnClickListener;
  7. import android.widget.Button;
  8. import android.widget.ImageView;
  9. import android.widget.RadioButton;
  10. import android.widget.RadioGroup;
  11. import android.widget.SeekBar;
  12. import android.widget.SeekBar.OnSeekBarChangeListener;
  13. public class SimpleActivity extends Activity {
  14.     /**播放动画按钮**/
  15.     Button button0 = null;
  16.     /**停止动画按钮**/
  17.     Button button1 = null;
  18.     /**设置动画循环选择框**/
  19.     RadioButton radioButton0null;
  20.     RadioButton radioButton1null;
  21.     RadioGroup  radioGroup = null;
  22.     /**拖动图片修改Alpha值**/
  23.     SeekBar seekbar = null;
  24.     /**绘制动画View**/
  25.     ImageView imageView = null;
  26.     /**绘制动画对象**/
  27.     AnimationDrawable animationDrawable = null;
  28.     @Override
  29.     public void onCreate(Bundle savedInstanceState) {
  30.     super.onCreate(savedInstanceState);
  31.     setContentView(R.layout.simple);
  32.     /**拿到ImageView对象**/
  33.     imageView = (ImageView)findViewById(R.id.imageView);
  34.     /**通过ImageView对象拿到背景显示的AnimationDrawable**/
  35.     animationDrawable = (AnimationDrawable) imageView.getBackground();
  36.     /**开始播放动画**/
  37.     button0 = (Button)findViewById(R.id.button0);
  38.     button0.setOnClickListener(new OnClickListener() {
  39.         @Override
  40.         public void onClick(View arg0) {
  41.         /**播放动画**/
  42.         if(!animationDrawable.isRunning()) {
  43.             animationDrawable.start();
  44.         }
  45.         }
  46.     });
  47.     /**停止播放动画**/
  48.     button1 = (Button)findViewById(R.id.button1);
  49.     button1.setOnClickListener(new OnClickListener() {
  50.         @Override
  51.         public void onClick(View arg0) {
  52.         /**停止动画**/
  53.         if(animationDrawable.isRunning()) {
  54.             animationDrawable.stop();
  55.         }
  56.         }
  57.     });
  58.     /**单次播放**/
  59.     radioButton0 = (RadioButton)findViewById(R.id.checkbox0);
  60.     /**循环播放**/
  61.     radioButton1 = (RadioButton)findViewById(R.id.checkbox1);
  62.     /**单选列表组**/
  63.     radioGroup = (RadioGroup)findViewById(R.id.radiogroup);
  64.     radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
  65.         @Override
  66.         public void onCheckedChanged(RadioGroup radioGroup, int checkID) {
  67.         if(checkID == radioButton0.getId()) {
  68.             //设置单次播放
  69.             animationDrawable.setOneShot(true);
  70.         }else if (checkID == radioButton1.getId()) {
  71.             //设置循环播放
  72.             animationDrawable.setOneShot(false);
  73.         }
  74.         //发生改变后让动画重新播放
  75.         animationDrawable.stop();
  76.         animationDrawable.start();
  77.         }
  78.     });
  79.     /**监听的进度条修改透明度**/
  80.     seekbar = (SeekBar)findViewById(R.id.seekBar);
  81.     seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
  82.         @Override
  83.         public void onStopTrackingTouch(SeekBar seekBar) {
  84.         }
  85.         @Override
  86.         public void onStartTrackingTouch(SeekBar seekBar) {
  87.         }
  88.         @Override
  89.         public void onProgressChanged(SeekBar seekBar, int progress, boolean frameTouch) {
  90.         /**设置动画Alpha值**/
  91.         animationDrawable.setAlpha(progress);
  92.         /**通知imageView 刷新屏幕**/
  93.         imageView.postInvalidate();
  94.         }
  95.     });
  96.     }
  97. }

/layout/simple.xml

  1. <?xml version=“1.0” encoding=“utf-8”?>
  2. <LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
  3.     android:orientation=“vertical”
  4.     android:layout_width=“fill_parent”
  5.     android:layout_height=“fill_parent”
  6.     >
  7. <LinearLayout
  8.     android:orientation=“horizontal”
  9.     android:layout_width=“wrap_content”
  10.     android:layout_height=“wrap_content”
  11.     >
  12.  <Button
  13.    android:id=“@+id/button0”
  14.    android:layout_width=“wrap_content”
  15.    android:layout_height=“wrap_content”
  16.    android:text=“播放动画”
  17.  />
  18.   <Button
  19.    android:id=“@+id/button1”
  20.    android:layout_width=“wrap_content”
  21.    android:layout_height=“wrap_content”
  22.    android:text=“停止动画”
  23.  />
  24.  </LinearLayout>
  25.  <RadioGroup android:id=“@+id/radiogroup”
  26.      android:layout_width=“wrap_content”
  27.      android:layout_height=“wrap_content”
  28.      android:orientation=“horizontal”>
  29.    <RadioButton
  30.      android:id=“@+id/checkbox0”
  31.      android:layout_width=“wrap_content”
  32.      android:layout_height=“wrap_content”
  33.      android:checked=“true”
  34.      android:text=“单次播放”
  35.    />
  36.   <RadioButton
  37.     android:id=“@+id/checkbox1”
  38.     android:layout_width=“wrap_content”
  39.     android:layout_height=“wrap_content”
  40.     android:text=“循环播放”
  41.    />
  42.    </RadioGroup>
  43.     <TextView
  44.     android:layout_width=“wrap_content”
  45.     android:layout_height=“wrap_content”
  46.     android:text=“拖动进度条修改透明度(0 – 255)之间”
  47.     />
  48.   <SeekBar
  49.     android:id=“@+id/seekBar”
  50.     android:layout_width=“fill_parent”
  51.     android:layout_height=“wrap_content”
  52.     android:max=“256”
  53.     android:progress=“256”/>
  54.   <ImageView
  55.    android:id=“@+id/imageView”
  56.    android:background=“@anim/animation”
  57.    android:layout_width=“wrap_content”
  58.    android:layout_height=“wrap_content”
  59.  />
  60. </LinearLayout>

控制帧播放的/anim/animation.xml

  1. <animation-list xmlns:android=“http://schemas.android.com/apk/res/android” android:oneshot=“false”>
  2.  <item android:drawable=“@drawable/a” android:duration=“100” />
  3.  <item android:drawable=“@drawable/b” android:duration=“100” />
  4.  <item android:drawable=“@drawable/c” android:duration=“100” />
  5.  <item android:drawable=“@drawable/d” android:duration=“100” />
  6.  <item android:drawable=“@drawable/e” android:duration=“100” />
  7.  <item android:drawable=“@drawable/f” android:duration=“100” />
  8.  <item android:drawable=“@drawable/g” android:duration=“100” />
  9.  <item android:drawable=“@drawable/h” android:duration=“100” />
  10.  <item android:drawable=“@drawable/i” android:duration=“100” />
  11.  <item android:drawable=“@drawable/j” android:duration=“100” />
  12.  </animation-list>

      看看内容应该是很好理解的,<animation-list>为动画的总标签,这里面放着帧动画 <item>标签,也就是说若干<item>标签的帧 组合在一起就是帧动画了。<animation-list > 标签中android:oneshot=”false” 这是一个非常重要的属性,默认为false 表示 动画循环播放, 如果这里写true 则表示动画只播发一次。 <item>标签中记录着每一帧的信息android:drawable=”@drawable/a”表示这一帧用的图片为”a”,下面以此类推。  android:duration=”100″ 表示这一帧持续100毫秒,可以根据这个值来调节动画播放的速度。

这是一个比较简单的布局文件,应该都能看懂吧。  我主要说一下 最后的这个 ImageView, 它就是用来显示我们的动画。 这里使用android:background=”@anim/animation”设置这个ImageView现实的背景为一个动画,动画资源的路径为res/anim/animation.xml   ,当然 设置background同样也可以在代码中设置。

  1. imageView.setBackgroundResource(R.anim.animation);


通过getBackground方法就可以拿到这个animationDrawable对象。

  1. /**拿到ImageView对象**/
  2. imageView = (ImageView)findViewById(R.id.imageView);
  3. /**通过ImageView对象拿到背景显示的AnimationDrawable**/
  4. animationDrawable = (AnimationDrawable) imageView.getBackground();


AnimationDrawable 就是用来控制这个帧动画,这个类中提供了很多方法。

animationDrawable.start(); 开始这个动画
animationDrawable.stop(); 结束这个动画
animationDrawable.setAlpha(100);设置动画的透明度, 取值范围(0 – 255)
animationDrawable.setOneShot(true); 设置单次播放
animationDrawable.setOneShot(false); 设置循环播放
animationDrawable.isRunning(); 判断动画是否正在播放
animationDrawable.getNumberOfFrames(); 得到动画的帧数。

宣教主警戒:拖动进度条设置Alpha值的时候 一定要使用     imageView.postInvalidate(); 方法来通知UI线程重绘屏幕中的imageView  否则会看不到透明的效果 。这里切记切记~~

谢谢CSDN博主宣雨松,牛人一枚。以后还会多多向他学习。

——————————————————————————————————————————-

 

http://blog.csdn.net/imdxt1986/article/details/7023868

在Android SDK中有两种Camera classes,一个是 android.hardware.Camera,这是用来操控相机功能的类。另一个是 android.graphics.Camera,这个类别能帮我们做些什么事呢?

Camera就像一个摄像机,一个物体在原地不动,然后我们带着这个摄像机四处移动,在摄像机里面呈现出来的画面,就会有立体感,就可以从各个角度观看这个物体。
它有旋转、平移的一系列方法,实际上都是在改变一个Matrix对象,一系列操作完毕之后,我们得到这个Matrix,然后画我们的物体,就可以了。

android.graphics.Camera 是一个可以让你将 2D 物件在 3D 空间中移动,并将在其移动后的结果画在屏幕上的类别。

打开api说明文件

1. //Public Constructors

2.   Camera()

创建一个新的摄像头,空的转换

3. //Public Methods

4.   void  applyToCanvas(Canvas canvas)

计算对应当前转换矩阵,并将其应用到指定的画布上。

5.   float  dotWithNormal(float dx, float dy, float dz)

6.   void  getMatrix(Matrix matrix)

计算对应当前转换矩阵,并将其复制到所提供的矩阵对象。

7.   void  restore()

Restores the saved state, if any.
恢复保存的状态,如果有的话。

8.  void rotate(float x, float y, float z)

Applies a rotation transform around all three axis.
适用于所有三个轴旋转变换。

8.   void  rotateX(float deg)

Applies a rotation transform around the X axis.
适用于绕X轴旋转变换。

9.   void  rotateY(float deg)

Applies a rotation transform around the Y axis.

适用于绕Y轴旋转变换。

10.   void  rotateZ(float deg)

Applies a rotation transform around the Z axis.

适用于绕Z轴旋转变换。

11.   void  save()

Saves the camera state.

12. setLocation(float x, float y, float z)

Sets the location of the camera.

设置摄像头的位置。
13.   void  translate(float x, float y, float z)

Applies a translation transform on all three axis.

适用于所有三个轴的翻译转换。

在 Android ,要做出 3D 的效果,你当然可以用 OpenGL 的函式。不过,如果你需要更快的显示速度,或者只是要做个简单的 3D 特效,

那千万不要忘了这个 android.graphics.Camera。

  1. package com.xxx;
  2. import android.content.Context;
  3. import android.graphics.Camera;
  4. import android.graphics.Matrix;
  5. import android.util.AttributeSet;
  6. import android.util.Log;
  7. import android.view.View;
  8. import android.view.animation.Transformation;
  9. import android.widget.Gallery;
  10. import android.widget.ImageView;
  11. public class GalleryFlow extends Gallery {
  12.     private Camera mCamera = new Camera();//相机类
  13.     private int mMaxRotationAngle = 80;//最大转动角度
  14.     private int mMaxZoom = –300;////最大缩放值
  15.     private int mCoveflowCenter;//半径值
  16.     public GalleryFlow(Context context) {
  17.         super(context);
  18.         Log.d(“tag”“1”);
  19.         //支持转换 ,执行getChildStaticTransformation方法
  20.         this.setStaticTransformationsEnabled(true);
  21.     }
  22.     public GalleryFlow(Context context, AttributeSet attrs) {
  23.         super(context, attrs);
  24.         Log.d(“tag”“2”);
  25.         this.setStaticTransformationsEnabled(true);
  26.     }
  27.     public GalleryFlow(Context context, AttributeSet attrs, int defStyle) {
  28.         super(context, attrs, defStyle);
  29.         Log.d(“tag”“3”);
  30.         this.setStaticTransformationsEnabled(true);
  31.     }
  32.     public int getMaxRotationAngle() {
  33.         return mMaxRotationAngle;
  34.     }
  35.     public void setMaxRotationAngle(int maxRotationAngle) {
  36.         mMaxRotationAngle = maxRotationAngle;
  37.     }
  38.     public int getMaxZoom() {
  39.         return mMaxZoom;
  40.     }
  41.     public void setMaxZoom(int maxZoom) {
  42.         mMaxZoom = maxZoom;
  43.     }
  44.     private int getCenterOfCoverflow() {
  45.         return (getWidth() – getPaddingLeft() – getPaddingRight()) / 2
  46.                         + getPaddingLeft();
  47.     }
  48.     private static int getCenterOfView(View view) {
  49.         Log.d(“tag”“view left :”+view.getLeft());
  50.         Log.d(“tag”“view width :”+view.getWidth());
  51.         return view.getLeft() + view.getWidth() / 2;
  52.     }
  53.    //控制gallery中每个图片的旋转(重写的gallery中方法)
  54.     @Override
  55.     protected boolean getChildStaticTransformation(View child, Transformation t) {
  56.         //取得当前子view的半径值
  57.         final int childCenter = getCenterOfView(child);
  58.         Log.d(“tag”“childCenter:”+childCenter);
  59.         final int childWidth = child.getWidth();
  60.         //旋转角度
  61.         int rotationAngle = 0;
  62.         //重置转换状态
  63.         t.clear();
  64.         //设置转换类型
  65.         t.setTransformationType(Transformation.TYPE_MATRIX);
  66.         //如果图片位于中心位置不需要进行旋转
  67.         if (childCenter == mCoveflowCenter) {
  68.             transformImageBitmap((ImageView) child, t, 0);
  69.         } else {
  70.             //根据图片在gallery中的位置来计算图片的旋转角度
  71.             rotationAngle = (int) (((float) (mCoveflowCenter – childCenter) / childWidth) * mMaxRotationAngle);
  72.             Log.d(“tag”“rotationAngle:”+rotationAngle);
  73.             //如果旋转角度绝对值大于最大旋转角度返回(-mMaxRotationAngle或mMaxRotationAngle;)
  74.             if (Math.abs(rotationAngle) > mMaxRotationAngle) {
  75.                 rotationAngle = (rotationAngle < 0) ? -mMaxRotationAngle : mMaxRotationAngle;
  76.             }
  77.             transformImageBitmap((ImageView) child, t, rotationAngle);
  78.         }
  79.         return true;
  80.     }
  81.     @Override
  82.     protected void onSizeChanged(int w, int h, int oldw, int oldh) {
  83.         mCoveflowCenter = getCenterOfCoverflow();
  84.         super.onSizeChanged(w, h, oldw, oldh);
  85.     }
  86.     private void transformImageBitmap(ImageView child, Transformation t,
  87.                     int rotationAngle) {
  88.         //对效果进行保存
  89.         mCamera.save();
  90.         final Matrix imageMatrix = t.getMatrix();
  91.         //图片高度
  92.         final int imageHeight = child.getLayoutParams().height;
  93.         //图片宽度
  94.         final int imageWidth = child.getLayoutParams().width;
  95.         //返回旋转角度的绝对值
  96.         final int rotation = Math.abs(rotationAngle);
  97.         // 在Z轴上正向移动camera的视角,实际效果为放大图片。
  98.         // 如果在Y轴上移动,则图片上下移动;X轴上对应图片左右移动。
  99.         mCamera.translate(0.0f, 10.0f, 200.0f);// X值越大图片越靠右Y值越大是图片位置越高Z值越高图片越缩小
  100.         // As the angle of the view gets less, zoom in
  101.         if (rotation < mMaxRotationAngle) {
  102.             float zoomAmount = (float) (mMaxZoom + (rotation * 1.5));
  103.             mCamera.translate(0.0f, 0.0f, zoomAmount);
  104.         }
  105.         // 在Y轴上旋转,对应图片竖向向里翻转。
  106.         // 如果在X轴上旋转,则对应图片横向向里翻转。
  107.         mCamera.rotateY(rotationAngle);
  108.         mCamera.getMatrix(imageMatrix);
  109.       //以图片的中心点为旋转中心,如果不加这两句,就是以(0,0)点为旋转中心
  110.         imageMatrix.preTranslate(-(imageWidth / 2), -(imageHeight / 2));
  111.         imageMatrix.postTranslate((imageWidth / 2), (imageHeight / 2));
  112.         mCamera.restore();//恢复
  113.     }
  114. }