欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android Animation之TranslateAnimation(平移动画)

程序员文章站 2023-12-18 18:05:22
translateanimation(平移动画)的意思无非就是一张图片或其他从一个位置到达另外一个位置。直接代码分析,相关重要属性参数解释都在代码中。 1、首先编写...

translateanimation(平移动画)的意思无非就是一张图片或其他从一个位置到达另外一个位置。直接代码分析,相关重要属性参数解释都在代码中。

1、首先编写main.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: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:id="@+id/image" 
    android:text="@string/hello_world" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/car_one1"/> 

</relativelayout> 

2、接下来编写mainactivity.java文件。

package com.example.dell.bitmapproject; 

import android.support.v7.app.appcompatactivity; 
import android.os.bundle; 
import android.view.view; 
import android.view.animation.animation; 
import android.view.animation.animationset; 
import android.view.animation.translateanimation; 
import android.widget.imageview; 

public class mainactivity extends appcompatactivity { 
  private imageview image; 
  @override 
  protected void oncreate(bundle savedinstancestate) { 
    super.oncreate(savedinstancestate); 
    setcontentview(r.layout.main); 
    image =(imageview)findviewbyid(r.id.image); 
    image.setonclicklistener(new onclicklistenerimpl()); 
  } 
  private class onclicklistenerimpl implements view.onclicklistener { 
    @override 
    public void onclick(view v) { 
      /* 
        animationset相当于一个动画的集合,true表示使用animation的interpolator 
        false则是使用自己的。 
        interpolator 被用来修饰动画效果,定义动画的变化率,可以使存在的动画效果 
        accelerated(加速),decelerated(减速),repeated(重复),bounced(弹跳)等。 
       */ 

      animationset animationset = new animationset(true); 
      /* 
          animation还有几个方法 
          setfillafter(boolean fillafter) 
          如果fillafter的值为真的话,动画结束后,控件停留在执行后的状态 
          setfillbefore(boolean fillbefore) 
          如果fillbefore的值为真的话,动画结束后,控件停留在动画开始的状态 
          setstartoffset(long startoffset) 
          设置动画控件执行动画之前等待的时间 
          setrepeatcount(int repeatcount) 
          设置动画重复执行的次数 
       */ 
      translateanimation translateanimation = new translateanimation( 
          //x轴初始位置 
          animation.relative_to_self, 0.0f, 
          //x轴移动的结束位置 
          animation.relative_to_self,0.5f, 
          //y轴开始位置 
          animation.relative_to_self,0.0f, 
          //y轴移动后的结束位置 
          animation.relative_to_self,1.5f); 

      //3秒完成动画 
      translateanimation.setduration(2000); 
      //如果fillafter的值为真的话,动画结束后,控件停留在执行后的状态 
      animationset.setfillafter(true); 
      //将alphaanimation这个已经设置好的动画添加到 animationset中 
      animationset.addanimation(translateanimation); 
      //启动动画 
      mainactivity.this.image.startanimation(animationset); 

    } 
  } 
} 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

上一篇:

下一篇: