分享

android 补间动画的实现

 greenyun588 2013-09-15

为实现android的补间动画,我们需要定义一个描述动画动作的xml文件


该文件建议放在res/anim下


需自己创建建文件夹


代码如下


复制代码

<?xml version="1.0" encoding="utf-8"?>
<set
    xmlns:android="http://schemas./apk/res/android"
    android:interpolator="@android:anim/linear_interpolator">
<!--      透明度变化 -->
     <alpha 
         android:fromAlpha="1"
         android:toAlpha="0"
         android:duration="2000"/>
<!--      缩放与扩大 -->
     <scale android:fromXScale="1.0"
        android:toXScale="0"
        android:fromYScale="1.0"
        android:toYScale="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:fillAfter="true"
        android:duration="2000"/>
<!--     水平与垂直位移 -->
    <translate 
        android:fromXDelta="0"
        android:toXDelta="130"
        android:fromYDelta="0"
        android:toYDelta="-80"
        android:duration="2000"/>
<!--     旋转 -->
    <rotate 
        android:fromDegrees="0"
        android:toDegrees="360"
        android:pivotX="50%"
        android:pivotY="50%"
         android:duration="2000"/>
    

</set>

复制代码

我们的布局文件很简单,只有一个按钮和一个ImangView


代码:


复制代码

<RelativeLayout xmlns:android="http://schemas./apk/res/android"
    xmlns:tools="http://schemas./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=".MainActivity" >

<ImageView 
    android:src="@drawable/test"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/image"/>
<Button 
    android:layout_below="@id/image"
    android:text="start"
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

</RelativeLayout>

复制代码

下面就要用到java文件来处理图片的动画了


需要用到AnimationUtils中的loadAnimation方法来调用我们的动画资源


具体代码如下


复制代码

package com.happyrxk.returnhome;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {

    private ImageView image;
    private Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final Animation anim =AnimationUtils.loadAnimation(this, R.anim.my_anim);
        
        button = (Button)findViewById(R.id.button);
        image = (ImageView)findViewById(R.id.image);
        button.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                image.startAnimation(anim);
            }
        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
}

复制代码

 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约