创建Drawable动画(二)

作者:追风剑情 发布于:2016-5-7 11:49 分类:Android

一、创建动画文件并放到res/drawable目录下

22222.jpg

animation_circle.xml

<?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/white_circle" android:duration="250" />
	<item android:drawable="@drawable/gray_circle" android:duration="250" />
	<item android:drawable="@drawable/black_circle" android:duration="250" />
</animation-list>

white_circle.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="oval">
    <solid android:color="#FFFFFF"/>
	<size android:height="100dp" android:width="100dp"/>
</shape>

gray_circle.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="oval">"
    <solid android:color="#555555"/>
	<size android:height="100dp" android:width="100dp"/>
</shape>

black_circle.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="oval">"
    <solid android:color="#000000"/>
	<size android:height="100dp" android:width="100dp"/>
</shape>

二、创建布局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_circle" />

</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;
			}
		});
	}
}

运行效果

11111.jpg

标签: Android

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号