Android动画一

Android动画的两种形式

布局文件

<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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <ImageView
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:id="@+id/imageView"
        android:onClick="click"
        android:src="@android:drawable/ic_lock_idle_alarm" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="move"
        android:onClick="move"
        android:id="@+id/button"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="102dp" />

</RelativeLayout>

代码文件:

package com.androidbase.zjn.androidbase;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void click(View view){
        Toast.makeText(this,"clicked",Toast.LENGTH_LONG).show();
    }
    public void move(View view){
        TranslateAnimation  animation = new TranslateAnimation(0,200,0,0);
        animation.setDuration(1000);//运动1秒钟
        animation.setFillAfter(true);//动画结束后果停留到原来的位置
        ImageView imageView =(ImageView) findViewById(R.id.imageView);
        imageView.startAnimation(animation);//imageView绑定动画
    }

}

使用Animator的效果:

ImageView imageView =(ImageView) findViewById(R.id.imageView);
        ObjectAnimator.ofFloat(imageView,"translationX",0,200F).setDuration(1000).start();
        ObjectAnimator.ofFloat(imageView,"translationY",0,200F).setDuration(1000).start();
        ObjectAnimator.ofFloat(imageView,"rotation",0,360F).setDuration(1000).start();

使用几个一起懂:

        ImageView imageView =(ImageView) findViewById(R.id.imageView);
        PropertyValuesHolder p1 = PropertyValuesHolder.ofFloat("rotation",0,360F);
        PropertyValuesHolder p2 = PropertyValuesHolder.ofFloat("translationX",0,200F);
        PropertyValuesHolder p3 = PropertyValuesHolder.ofFloat("translationY",0,200F);
        ObjectAnimator.ofPropertyValuesHolder(imageView,p1,p2,p3).setDuration(1000).start();
    //几个动画一起显示
        ImageView imageView =(ImageView) findViewById(R.id.imageView);
        ObjectAnimator animator1=ObjectAnimator.ofFloat(imageView,"translationX",0,200F);
        ObjectAnimator animator2=ObjectAnimator.ofFloat(imageView,"translationY",0,200F);
        ObjectAnimator animator3=ObjectAnimator.ofFloat(imageView,"rotation",0,360F);
        AnimatorSet set = new AnimatorSet();
        set.playTogether(animator1,animator2,animator3);
        set.setDuration(1000);
        set.start();

完整的代码:

package com.androidbase.zjn.androidbase;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void click(View view){
        Toast.makeText(this,"clicked",Toast.LENGTH_LONG).show();
    }
    public void move(View view){
        //TranslateAnimation只是简单的移动
//        TranslateAnimation  animation = new TranslateAnimation(0,200,0,0);
//        animation.setDuration(1000);//运动1秒钟
//        animation.setFillAfter(true);//动画结束后果停留到原来的位置
//        ImageView imageView =(ImageView) findViewById(R.id.imageView);
//        imageView.startAnimation(animation);//imageView绑定动画

//        ImageView imageView =(ImageView) findViewById(R.id.imageView);
//        ObjectAnimator.ofFloat(imageView,"translationX",0,200F).setDuration(1000).start();
//        ObjectAnimator.ofFloat(imageView,"translationY",0,200F).setDuration(1000).start();
//        ObjectAnimator.ofFloat(imageView,"rotation",0,360F).setDuration(1000).start();

//        上面的优化
//        ImageView imageView =(ImageView) findViewById(R.id.imageView);
//        PropertyValuesHolder p1 = PropertyValuesHolder.ofFloat("rotation",0,360F);
//        PropertyValuesHolder p2 = PropertyValuesHolder.ofFloat("translationX",0,200F);
//        PropertyValuesHolder p3 = PropertyValuesHolder.ofFloat("translationY",0,200F);
//        ObjectAnimator.ofPropertyValuesHolder(imageView,p1,p2,p3).setDuration(1000).start();

        //几个动画一起显示
        ImageView imageView =(ImageView) findViewById(R.id.imageView);
        ObjectAnimator animator1=ObjectAnimator.ofFloat(imageView,"translationX",0,200F);
        ObjectAnimator animator2=ObjectAnimator.ofFloat(imageView,"translationY",0,200F);
        ObjectAnimator animator3=ObjectAnimator.ofFloat(imageView,"rotation",0,360F);
        AnimatorSet set = new AnimatorSet();
        //set.playTogether(animator1,animator2,animator3);
        //set.playSequentially(animator1,animator2,animator3);//按照顺序动画
        set.play(animator1).with(animator2).before(animator3);//动画1和动画2一块进行,让后再进行动画3
        set.setDuration(1000);
        set.start();

    }

}
时间: 2024-08-14 15:03:17

Android动画一的相关文章

android动画具体解释六 XML中定义动画

动画View 属性动画系统同意动画View对象并提供非常多比view动画系统更高级的功能.view动画系统通过改变绘制方式来变换View对象,view动画是被view的容器所处理的,由于View本身没有要操控的属性.结果就是View被动画了.但View对象本身并没有变化. 在Android3.0中,新的属性和对应的getter和setter方法被增加以克服此缺点. 属性动画系统能够通过改变View对象的真实属性来动画Views. 并且.View也会在其属性改变时自己主动调用invalidate(

Android开发艺术探索——第七章:Android动画深入分析

Android开发艺术探索--第七章:Android动画深入分析 Android的动画可以分成三种,view动画,帧动画,还有属性动画,其实帧动画也是属于view动画的一种,,只不过他和传统的平移之类的动画不太一样的是表现形式上有点不一样,view动画是通过对场景的不断图像交换而产生的动画效果,而帧动画就是播放一大段图片,很显然,图片多了会OOM,属性动画通过动态的改变对象的属性达到动画效果,也是api11的新特性,在低版本无法使用属性动画,但是我们依旧有一些兼容库,OK,我们还是继续来看下详细

android 动画(1) 补间动画

android动画: 3.0以前,android支持两种动画模式,tween animation,frame animation, 3.0中又引入了一个新的动画系统:property animation, 这三种动画模式在SDK中被称为 property animation,        属性动画: view animation,   补间动画:  给出两个关键帧,通过一些算法将给定属性值在给定的时间内在两个关键帧间渐变. (Tween animation) drawable animatio

android动画详解三 动画API概述

· 属性动画与view动画的不同之处 view动画系统提供了仅动画View 对象的能力,所以如果你想动画非View 对象,你就要自己实现代码. view动画系统实际上还被强制仅能对 View 的少数属性进行动画,比如缩放和旋转,而不能对背景色进行. view动画的另一个坏处是它仅修改View的绘制位置,而不是View的实际位置.例如,如果你动画一个移动穿越屏幕,button的绘制位置是正确的,但实际你可以点击它的位置却没有变,所以你必须去实现你自己的逻辑来处理它. 使用属性动画系统时,这个限制被

Android动画基础

Android动画主要有三种: 1> 视图动画,也叫Tween(补间)动画可以在一个视图容器内执行一系列简单变换(位置.大小.旋转.透明度).譬如,如果你有一个TextView对象,您可以移动.旋转.缩放.透明度设置其文本,当然,如果它有一个背景图像,背景图像会随着文本变化. 补间动画通过XML或Android代码定义,建议使用XML文件定义,因为它更具可读性.可重用性. 2> Drawable动画其实就是Frame动画(帧动画),它允许你实现像播放幻灯片一样的效果,这种动画的实质其实是Dra

Android动画效果——1.帧动画2.补间动画3.跳转画面(三)

Android--动画效果1.帧动画2.补间动画3.跳转画面 插值器类 xml属性值 说明 LinearInterpolator @android:anim/linear_interpolatorr 动画以均匀的速度改变. AccelerateInterpolator @android:anim/accelerate_interpolator 在动画开始时改变速度较慢,然后开始加速. AccelerateDecelerateInterpolator @android:anim/accelerat

Android动画AnimationSet遇到的问题。

之前对Android动画这块一直是一知半解,知道个大概,并不会使用.刚好这几天没有太多的任务要做,可以梳理一下Android动画的一些知识.Android Animation的基础用法就不说了,这里主要记录下简单实用中遇到的问题. 1.XML中AnimationSet的某些属性有些问题. 主要就是android:repeatCount,android:repeatMode无效.这个问题据说是Google的工程师刻意为之.[参考:http://stackoverflow.com/questions

Android 动画详解

这次主要就介绍android动画,android动画目前分为三种形式,Tween Animation 这个只能应用于view对象上面的,Drawable Animation这个是帧动画,就是类似我们有一些列的图片依次播放图片时出现的动画,Property Animation 这个是属性动画,这也是在android3.0之后引进的动画,在手机的版本上是android4.0就可以使用这个动画,下面我们主要就是针对这三种情况进行介绍. Tween Animation 这个动画在Property Ani

【Android动画】之Tween动画 (渐变、缩放、位移、旋转)

Android 平台提供了两类动画. 一类是Tween动画,就是对场景里的对象不断的进行图像变化来产生动画效果(旋转.平移.放缩和渐变). 第二类就是 Frame动画,即顺序的播放事先做好的图像,与gif图片原理类似. 下面就讲一下Tweene Animations. 主要类: Animation  动画 AlphaAnimation 渐变透明度 RotateAnimation 画面旋转 ScaleAnimation 渐变尺寸缩放 TranslateAnimation 位置移动 Animatio

Android动画学习(缓动动画与属性动画的区别)

前言: 在 Android动画学习(概述)中,如果你看过这篇帖子,你应该会对缓动动画和属性动画之间的区别产生疑问,因为在它们的应用中,你会感觉这两种动画有着一些相似的地方,为此,我打算把这两种动画之间的区别做一下说明 区别: 在这里先附上官方文档对于这两种动画的区别说明(我所说的缓动动画对应在下文中的英文为:View Animation,属性动画对应在下文中的英文为:Property Animation): How Property Animation Differs from View Ani