随笔 - 632  文章 - 17  评论 - 54  阅读 - 93万

Android Frame动画demo

Android动画介绍:Android为我们提供了两种动画实现,Frame和Tween。

两者之间的区别:

  1.Frame动画:就像放电影一样,是通过预先做好的图片进行连续播放从而形成动画效果

  2.Tween动画:通过对图片设置平移、缩放、旋转、改变透明度等方式来显示动画效果

本节仅讲Frame动画,

Frame动画是通过AnimationDrawable来实现的,它提供了start()和stop()两个方法,对播放的动画进行控制,一般通过XML文件配置,在工程的res/anim目录下创建一个XML配置文件,该配置文件有一个<animation-list>根元素和若干个<item>子元素。

废话就不说了,下面将贴出该例子的完整代码,供大家测试使用:

一、FrameActivity

复制代码
package com.yw.myapiupdate.frame;

import com.yw.myapiupdate.R;

import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

/**
 * 
 * 实现动画轮询播放
 * @author yw-tony
 *
 */
public class FrameActivity extends Activity implements OnClickListener{
    private Button btn_start;
    private Button btn_end;
    private ImageView iv;
    private AnimationDrawable ad;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.frame_layout);
        initViews();
    }
    private void initViews(){
        btn_start = (Button)findViewById(R.id.frame_btn_start);
        btn_end = (Button)findViewById(R.id.frame_btn_end);
        iv = (ImageView)findViewById(R.id.frame_iv);
        btn_start.setOnClickListener(this);
        btn_end.setOnClickListener(this);
        this.ad = (AnimationDrawable)iv.getBackground();
    }
    private void startAnimation(){
        this.ad.start();
    }
    private void stopAnimation(){
        this.ad.stop();
    }
    @Override
    public void onClick(View v) {
        switch(v.getId()){
        case R.id.frame_btn_start:
            startAnimation();
            break;
        case R.id.frame_btn_end:
            stopAnimation();
            break;
        }
    }
}
复制代码

与之对应的xml文件

复制代码
<?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"
    android:gravity="center"
    android:orientation="vertical" >
    <ImageView 
        android:id="@+id/frame_iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@anim/framedrawable"/>
    <Button 
        android:id="@+id/frame_btn_start"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="start"/>
    <Button 
        android:id="@+id/frame_btn_end"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="end"/>
</LinearLayout>
复制代码

二、设置动画的xml文件

复制代码
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:apk="http://schemas.android.com/apk/res/android" apk:oneshot="false">
    <item apk:drawable="@drawable/anim_1" apk:duration="200" />
    <item apk:drawable="@drawable/anim_2" apk:duration="200" />
    <item apk:drawable="@drawable/anim_3" apk:duration="200" />
    <item apk:drawable="@drawable/anim_4" apk:duration="200" />
    <item apk:drawable="@drawable/anim_5" apk:duration="200" />
    <item apk:drawable="@drawable/anim_6" apk:duration="200" />
</animation-list>
复制代码

 

下面是源代码以及资源文件的下载地址:

 https://files.cnblogs.com/tony-yang-flutter/anni.zip

 

 

 

 

posted on   飘杨......  阅读(538)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

点击右上角即可分享
微信分享提示