创建Drawable动画

作者:追风剑情 发布于:2016-4-17 20:45 分类:Android

Android上最简单的动画是顺序展示一系列的drawable。这被称为drawable动画。

一、创建动画文件animation.xml同时准备3张图片并放到res/drawable-hdpi目录下


<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" 
    android:visible="true" android:oneshot="true">
    <item android:drawable="@drawable/img0" android:duration="250" />
	<item android:drawable="@drawable/img1" android:duration="250" />
	<item android:drawable="@drawable/img2" android:duration="250" />
</animation-list>


二、创建布局xml


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.androidtest.DrawableAnimationActivity" >

    <ImageView
        android:id="@+id/image_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="center"
        android:src="@drawable/animation" />

</RelativeLayout>


三、创建活动


package com.example.androidtest;

import android.support.v7.app.ActionBarActivity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;

public class DrawableAnimationActivity extends ActionBarActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_drawable_animation);
		ImageView iv = (ImageView)findViewById(R.id.image_view);
		iv.setOnTouchListener(new OnTouchListener(){
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				ImageView iv = (ImageView)v;
				AnimationDrawable ad = (AnimationDrawable)iv.getDrawable();
				ad.stop();
				ad.start();
				return true;
			}
		});
	}
}


运行效果

11111111111.png

标签: Android

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号