补间动画的使用

点击不同按钮实现效果如下图:这里写图片描述
具体代码如下:
布局文件:

 <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.xiaocai.com.MainActivity" >
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="alpha"
        android:text="Alpha" />

     <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
       android:layout_weight="1"
        android:onClick="translate"
        android:text="Tran" />
     <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
       android:layout_weight="1"
        android:onClick="scale"
        android:text="Scale" />
     <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
       android:layout_weight="1"
        android:onClick="rotate"
        android:text="Rotat" />
          android:text="Scale" />
     <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
       android:layout_weight="1"
        android:onClick="sets"
        android:text="Sets" />
</LinearLayout>

<ImageView
    android:id="@+id/iv"
    android:layout_width="130dp"
    android:layout_height="130dp"
    android:layout_centerInParent="true"
    android:src="@+drawable/qingchun" />
    < /RelativeLayout>

资源文件:这里写图片描述

MainActivity.java中的代码:
package com.xiaocai.com;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;

public class MainActivity extends Activity {
private ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv=(ImageView) findViewById(R.id.iv);
}
//透明动画
public void alpha(View v){
//得到透明动画对象,并初始化;fromAlpha:其始透明度,toAlpha:结束透明度
AlphaAnimation aa=new AlphaAnimation(0.0f, 1.0f);
//设置动画时间
aa.setDuration(3000);
//设置播放重复的次数
aa.setRepeatCount(3);
//设置播放的模式:RESTART:从新开始 REVERSE:反转
aa.setRepeatMode(AlphaAnimation.RESTART);
//开始执行动画;View中执行动画的方法
iv.startAnimation(aa);
}

//平移动画
public void translate(View v){
//fromXDelta:x轴上的起点
//toXDelta:x轴上的终点
TranslateAnimation ta=new TranslateAnimation(0, 100.0f, 0, 100.0f);
ta.setDuration(3000);
ta.setRepeatCount(3);
iv.startAnimation(ta);
}
//缩放动画
public void scale(View v){
//缩放两倍大
ScaleAnimation sa=new ScaleAnimation(0, 2, 0, 2);
sa.setDuration(3000);
iv.startAnimation(sa);

}
//旋转动画
public void rotate(View v){
//从0角度旋转到360角度
RotateAnimation ra=new RotateAnimation(0, 90);
ra.setDuration(3000);
iv.startAnimation(ra);
}
//组合播放
public void sets(View v){
AnimationSet as=new AnimationSet(true);
AlphaAnimation aa=new AlphaAnimation(0.0f, 1.0f);
TranslateAnimation ta=new TranslateAnimation(0, 100.0f, 0, 100.0f);
as.addAnimation(aa);
as.addAnimation(ta);
as.setDuration(3000);
iv.startAnimation(as);
}
}
小编微信:这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值