欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Frame Animation 帧动画

程序员文章站 2022-03-01 20:51:45
...

话说锁屏模块已经做完却过去2个月了,期间也出现一些bug,都一一化解了。寻思着该模块现在功能简洁,操作优雅,应该是不用操心了。

但事情往往是不会如人意的,我们某老总用他那粗大的手指划拉几下,就提问题了:我说这个怎么能这么小,说多少遍了你们UI设计要站在用户的角度看问题。。。。

UI设计师b见状立马跳出来接腔:“对啊对啊,我早就想说这个条太小了,我看4.0.的锁屏不错。。。不如。。。巴拉巴拉。”一听这话我当时差点吓尿了,这还两个礼拜就量产的机器,现在整这个不是扯么。

于是我小心翼翼的列举以下事实:“1,4.0锁屏用到了一些3.0之后才有的接口,我们2.3能否做到那种效果还需考究。2,4.0锁屏风格和我们机器并不是很搭,全盘推翻前面的设计方案也不利于UI设计和软件工作的继承和延续;3,离量产只剩下2周,在兼顾其他模块下无法保证完成该锁屏使其效果优秀且稳定没BUG。”领导毕竟以项目进度为重,连忙说,你们这些UI工程师没理解我的意思,原来锁屏还是可以的,只不过还有些缺点,你们在这个基础上做些必要的修改不就更好了吗。。。“听到这些,UI设计师B也只能撇撇嘴了。

既然是领导的意思那自然是要深度贯彻的,UI根据会议精神在尽量不改大框架的前提下重新设计了锁屏,其中有个地方必须要用动画实现。感觉是用frame animation(帧动画)比较简单方便,以前没用过,现学现卖了。累死我了,说这么多来引出本次的主题。

1,写了一个小demo,工程目录如下。

Frame Animation 帧动画

2,就一个类,估计没有比这更简单的逻辑了:

public class FrameAnimationActivity extends Activity {
    /** Called when the activity is first created. */
	private static final boolean DEBUG = true;
	private static final String LOG_TAG = "FrameAnimationActivity "; 
	private AnimationDrawable mAnimationDrawable;
	private Button mButton;
	private ImageView mImageView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        setupView();
    }

	private void setupView() {
		// TODO Auto-generated method stub
		mButton = (Button)findViewById(R.id.bt1);
		mImageView = (ImageView)findViewById(R.id.iv1);
		mAnimationDrawable = (AnimationDrawable)mImageView.getDrawable();
		mButton.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				setUpAnimation();
			}

			private void setUpAnimation() {
				// TODO Auto-generated method stub
				if(mAnimationDrawable.isRunning()){
					if(DEBUG)Log.d(LOG_TAG, "yes is running,but stop soon.");
					mAnimationDrawable.stop();
					mImageView.setVisibility(View.INVISIBLE);
//					setUpAnimation();//加上后,点击按钮会有不一样的惊喜哦。
				}else{
					if(DEBUG)Log.d(LOG_TAG, "no is running,but running soon.");
					mImageView.setVisibility(View.VISIBLE);
					mAnimationDrawable.start();
				}
			}
		});
	}

3,布局文件和动画文件

<animation-list
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:oneshot="true">
 <item android:drawable="@drawable/icon1" android:duration="100"></item>
 <item android:drawable="@drawable/icon2" android:duration="80"></item>
 <item android:drawable="@drawable/icon3" android:duration="60"></item>
 <item android:drawable="@drawable/icon4" android:duration="50"></item>
 </animation-list>

android:oneshot=“true”这句可以设置动画是否循环播放。除了在布局里面定义动画,也可以用代码动态添加帧数和控制。

<nobr><span style="font-family:'Microsoft YaHei'">void</span></nobr> <nobr><span style="font-family:'Microsoft YaHei'"><span class="sympad" style="margin-right:2px"><a href="file:///E:/android-sdk-windows-1.6_r1/docs/reference/android/graphics/drawable/AnimationDrawable.html#addFrame(android.graphics.drawable.Drawable,%20int)" style="text-decoration:none">addFrame</a></span>(<a href="file:///E:/android-sdk-windows-1.6_r1/docs/reference/android/graphics/drawable/Drawable.html" style="text-decoration:none">Drawable</a>frame, int duration)</span></nobr>
Add a frame to the animation
<nobr><span style="font-family:'Microsoft YaHei'">int</span></nobr> <nobr><span style="font-family:'Microsoft YaHei'"><span class="sympad" style="margin-right:2px"><a href="file:///E:/android-sdk-windows-1.6_r1/docs/reference/android/graphics/drawable/AnimationDrawable.html#getDuration(int)" style="text-decoration:none">getDuration</a></span>(int i)</span></nobr>
<nobr><a href="file:///E:/android-sdk-windows-1.6_r1/docs/reference/android/graphics/drawable/Drawable.html" style="text-decoration:none"><span style="font-family:'Microsoft YaHei'">Drawable</span></a></nobr> <nobr><span style="font-family:'Microsoft YaHei'"><span class="sympad" style="margin-right:2px"><a href="file:///E:/android-sdk-windows-1.6_r1/docs/reference/android/graphics/drawable/AnimationDrawable.html#getFrame(int)" style="text-decoration:none">getFrame</a></span>(int index)</span></nobr>
<nobr><span style="font-family:'Microsoft YaHei'">int</span></nobr> <nobr><span style="font-family:'Microsoft YaHei'"><span class="sympad" style="margin-right:2px"><a href="file:///E:/android-sdk-windows-1.6_r1/docs/reference/android/graphics/drawable/AnimationDrawable.html#getNumberOfFrames()" style="text-decoration:none">getNumberOfFrames</a></span>()</span></nobr>
<nobr><span style="font-family:'Microsoft YaHei'">void</span></nobr> <nobr><span style="font-family:'Microsoft YaHei'"><span class="sympad" style="margin-right:2px"><a href="file:///E:/android-sdk-windows-1.6_r1/docs/reference/android/graphics/drawable/AnimationDrawable.html#inflate(android.content.res.Resources,%20org.xmlpull.v1.XmlPullParser,%20android.util.AttributeSet)" style="text-decoration:none">inflate</a></span>(<a href="file:///E:/android-sdk-windows-1.6_r1/docs/reference/android/content/res/Resources.html" style="text-decoration:none">Resources</a>r,<a href="file:///E:/android-sdk-windows-1.6_r1/docs/reference/org/xmlpull/v1/XmlPullParser.html" style="text-decoration:none">XmlPullParser</a>parser,<a href="file:///E:/android-sdk-windows-1.6_r1/docs/reference/android/util/AttributeSet.html" style="text-decoration:none">AttributeSet</a>attrs)</span></nobr>
<nobr><span style="font-family:'Microsoft YaHei'">boolean</span></nobr> <nobr><span style="font-family:'Microsoft YaHei'"><span class="sympad" style="margin-right:2px"><a href="file:///E:/android-sdk-windows-1.6_r1/docs/reference/android/graphics/drawable/AnimationDrawable.html#isOneShot()" style="text-decoration:none">isOneShot</a></span>()</span></nobr>

4,就是下面这张图会转动,有些像大风车。

Frame Animation 帧动画


需要源代码的,请clickMe