为控件添加动画的方法(Alpha透明度,rotate旋转,scale缩放,translate位移)

说明:

在xml文件中静态定义使用
1.在res文件夹下面新建anim文件夹
2.在anim文件夹下面新建资源xxx.xml
3.在xxx.xml中添加动画代码
4.在java代码中加载并使用动画
5在Java代码中动态使用

布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main"
    android:layout_width="match_parent" android:layout_height="match_parent"
android:background="@android:color/holo_green_light"
    tools:context="com.work.cartoon.MainActivity">
    <Button
        android:id="@+id/btn_tmd"
        android:text="透明度"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/btn_turn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/btn_tmd"
        android:text="旋转"/>

    <!--文本最大宽度,超出自动换行-->
    <TextView
        android:id="@+id/tv_content"
         android:inputType="textMultiLine"
        android:paddingTop="10dp"
        android:paddingLeft="10dp"
        android:paddingBottom="10dp"
        android:ellipsize="none"
        android:maxWidth="200dp"
        android:layout_alignParentRight="true"
        android:layout_marginRight="60sp"
        android:layout_below="@id/btn_tmd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!"
        android:background="@drawable/wechat"/>
</RelativeLayout>

anim文件:

Alpha:

set xmlns:android=”http://schemas.android.com/apk/res/android”
android:fillAfter=”true”>
!–动画结束停留在最后一帧
fromAlpha=”0”起始透明度
android:duration=”2000”
android:repeatCount=”infinite”重复次数为无限次
android:repeatMode=”reverse”重复方式为倒退,或者restart重启
android:toAlpha=”1”终止透明度–>

rotate:
 android:interpolator="@android:anim/bounce_interpolator">
    <!--android:fromDegrees="0"起始角度
    android:duration="1000"旋转持续时间
    android:pivotX="0.5"定义旋转圆心位置
    android:pivotY="0.5"
    android:toDegrees="+360"结束时的角度
    android:repeateCount="2"重复次数-->
    <rotate
        android:duration="1000"
        android:fromDegrees="0"
        android:pivotX="0.5"
        android:pivotY="0.5"
        android:repeateCount="2"
        android:toDegrees="+360"></rotate>
scale:
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_decelerate_interpolator">
    <!--起始大小
        fromXScale="0dp"
        fromYScale="0dp"
        toXScale="2.0"
        toYScale="2.0"结束时大小
        duration="1000"动作执行时间1s-->
    <scale android:pivotX="50%"
        android:pivotY="50%"
        android:fromXScale="0dp"
        android:fromYScale="0dp"
        android:duration="1000"
        android:toXScale="2.0"
        android:toYScale="2.0"></scale>
translate (位移动画):


public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Button btn_tmd, btn_turn;
    private TextView tv_content;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn_tmd = (Button) findViewById(R.id.btn_tmd);
        btn_turn = (Button) findViewById(R.id.btn_turn);
        tv_content = (TextView) findViewById(R.id.tv_content);
        btn_tmd.setOnClickListener(this);
        btn_turn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_tmd:
//                setAlphaByXml();
                setAlphaByJava();
                break;
            case R.id.btn_turn:
//                    setRoateByXml();
                setRoateByJava();
                break;
            case R.id.
        }
    }

    /**
     * 给控件静态加载动画效果
     */
    public void setAlphaByXml() {
        Animation animation = AnimationUtils.loadAnimation(this, R.anim.cartoon);
        //给控件开启动画
        tv_content.startAnimation(animation);
    }

    /**
     * 动态加载Alpha透明度动画的方法
     */
    public void setAlphaByJava() {
        //新建透明度动画,起始透明度为0,结束为1
        AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1);
        alphaAnimation.setDuration(2000);//持续时间2s
//        alphaAnimation.setRepeatCount(2);//重复次数2次
        alphaAnimation.setRepeatMode(Animation.REVERSE);//重复模式为倒退
        tv_content.startAnimation(alphaAnimation);//开启透明度动画
    }

    /**
     * 静态加载旋转的动画方法
     */
    public void setRoateByXml() {
        Animation animation = AnimationUtils.loadAnimation(this, R.anim.roater);
        tv_content.startAnimation(animation);
    }

    /**
     * 动态加载旋转动画的方法
     */
    public void setRoateByJava() {
        //定义起始角度0,终止角度360,以自身尺寸的50%出作为圆心旋转
        RotateAnimation rotateAnimation = new RotateAnimation(0, 360,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        rotateAnimation.setDuration(2000);//持续时间2S
        //设置系统差速器,定义旋转的速度
        rotateAnimation.setInterpolator(this, android.R.anim.bounce_interpolator);
        tv_content.startAnimation(rotateAnimation);
    }

    /**
     * 静态加载scale动画
     * 通过尺寸动画可以实现页面缩放等动画效果
     */
    public void setScaleByXml() {
        Animation animation = AnimationUtils.loadAnimation(this, R.anim.scale);
        tv_content.startAnimation(animation);
    }
    /**
     * 动态加载scale动画
     */
    public void setScaleByJava(){
        ScaleAnimation scaleAnimation=new ScaleAnimation(0,2,0,2,
                Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
        scaleAnimation.setDuration(1000);//设定时间1s
        tv_content.startAnimation(scaleAnimation);
    }
    /**
     * 静态加载位移动画的方法
     */
Animation animation = AnimationUtils.loadAnimation(this, R.anim.translate);  
        imageView1.startAnimation(animation);//开始动画

     /**
     * 动态加载位移动画的方法
     */
     /创建渐变动画   
        Animation animation = new TranslateAnimation(0, 0, 300, 300);  
        animation.setDuration(1500);  
        animation.setRepeatCount(1);//动画的重复次数  
        animation.setFillAfter(true);//设置为true,动画转化结束后被应用  
        imageView1.startAnimation(animation);//开始动画  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值