您的位置:首页 > 移动开发 > Android开发

android基础学习之动画基础

2016-03-29 12:44 309 查看
在现在这个世界,交互是必不可少的,而好的东西直接就增加了好的用户体验,今天我在这里举了两个简单的例子作为动画的入门,反正动画的规范大概就是这样一个样子,还需要提出的就是,补间动画在进行动画时,点击以前的位置依旧有效,而跟随动画去点却没有什么效果。而属性动画的监听随着动画位置的改变而改变。下面我就贴上两个简单动画的例子。对了还有个值得注意的是在选在动画的ResouceType的时候,补间动画为Tween Animation 而属性动画为Property Animation

主函数

package com.example.anim_demo;

import android.R.animator;
import android.os.Bundle;
import android.animation.Animator;
import android.animation.AnimatorInflater;
import android.app.Activity;
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.Toast;

public class MainActivity extends Activity {
// 申明2个按钮
Button btn_animation, btn_animator;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_animation = (Button) findViewById(R.id.Animation_start);// 初始化一个btn
// 设置监听事件
btn_animation.setOnClickListener(new OnClickListener() {
// 重写onClick方法
@Override
public void onClick(View arg0) {
// 获得补间动画对象
Animation animation = AnimationUtils.loadAnimation(
MainActivity.this, R.anim.animtion_demo);
// 启动补间动画
btn_animation.startAnimation(animation);

}
});
// 初始化按钮
btn_animator = (Button) findViewById(R.id.animator_start);
// 设置监听事件
btn_animator.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
// 获得属性动画
Animator animator = AnimatorInflater.loadAnimator(
MainActivity.this, R.animator.animitor_demo);
// 设置属性动画
animator.setTarget(btn_animator);
// 启动动画
animator.start();

}
});

}

}


布局文件

<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />

<Button
android:id="@+id/Animation_start"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Animation_start"/>
<Button
android:id="@+id/animator_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="animator_start"/>

</LinearLayout>


补间动画

<?xml version="1.0" encoding="utf-8"?>
<!-- 定义属性动画 -->
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="500"
android:pivotY="500"
android:duration="2000"
android:repeatCount="infinite"
>

</rotate>


属性动画

<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="2000"
android:repeatCount="infinite"
android:propertyName="rotation"
android:valueFrom="20"
android:valueTo="360">

</objectAnimator>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: