Android动画--自己体验animotion

Android的tween动画有四种(旋转,缩放,移动,淡入淡出),都是animotion的子类,AnimationSet也是animotion的子类。
  • 要理解四种动画的参数的意思,自己修改体验下参数即可。本文不做解释,提供代码给各位直接拿去测试看效果吧。、

  • activity类
public class TweenAnimation extends AppCompatActivity implements View.OnClickListener{

    private Button button,button2,button3,button4;
    private ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tween_animation);

        button = (Button) findViewById(R.id.button);
        button2 = (Button) findViewById(R.id.button2);
        button3 = (Button) findViewById(R.id.button3);
        button4 = (Button) findViewById(R.id.button4);
        imageView = (ImageView) findViewById(R.id.imageView);

        button.setOnClickListener(this);
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);
        button4.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {

        AnimationSet set = new AnimationSet(true);

        /**
         * 动画执行后都会返回最初的形态
         *相对物件的X坐标的开始位置的意思是:从物件左上角画坐标系,横线为Y坐标,竖线为X坐标,取值为0-1
         *ScaleAnimation(float fromX, float toX, float fromY, float toY,
         int pivotXType, float pivotXValue, int pivotYType, float pivotYValue);的参数:
              第一个参数fromX为动画起始时 X坐标上的伸缩尺寸  0.0表示收缩到没有
            第二个参数toX为动画结束时 X坐标上的伸缩尺寸   1.0表示正常无伸缩
            第三个参数fromY为动画起始时Y坐标上的伸缩尺寸  值小于1.0表示收缩
            第四个参数toY为动画结束时Y坐标上的伸缩尺寸   值大于1.0表示放大
         *AlphaAnimation的属性:1.0完全透明,0.0完全不透明
         *TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue, int fromYType, float fromYValue, int toYType, float toYValue)的参数:
              理解了相对物件的X坐标的开始位置的意思,就知道了具体的用法了,只要修改下参数自己调试下,就明白了。
         */
        switch (view.getId()){
            //旋转按钮
            case R.id.button:
                RotateAnimation rotateAnimation = new RotateAnimation(0,200,
                        Animation.RELATIVE_TO_SELF,0.5f,
                        Animation.RELATIVE_TO_SELF,0.5f);
                rotateAnimation.setDuration(500);
                set.addAnimation(rotateAnimation);
                imageView.startAnimation(set);
                break;
            //缩放按钮
            case R.id.button2:
                ScaleAnimation scaleAnimation = new ScaleAnimation(0,0.5f,0,0.8f,
                        Animation.RELATIVE_TO_SELF,0.8f,
                        Animation.RELATIVE_TO_SELF,0.8f);
                scaleAnimation.setDuration(500);
                set.addAnimation(scaleAnimation);
                imageView.startAnimation(set);
                break;
            //淡入淡出按钮
            case R.id.button3:
                AlphaAnimation alphaAnimation = new AlphaAnimation(1,0.5f);
                alphaAnimation.setDuration(500);
                set.addAnimation(alphaAnimation);
                imageView.startAnimation(set);
                break;
            //移动按钮
            case R.id.button4:
                TranslateAnimation translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,0f,
                        Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,1f);
                translateAnimation.setDuration(500);
                set.addAnimation(translateAnimation);
                imageView.startAnimation(set);
                break;
        }
    }
}

  • xml文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_tween_animation"
    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.flw.myhello.TweenAnimation">
<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/linearLayout">
    <Button
        android:layout_weight="1"
        android:text="旋转"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button" />

    <Button
        android:layout_weight="1"
        android:text="缩放"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button2" />

    <Button
        android:layout_weight="1"
        android:text="淡入淡出"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:id="@+id/button3" />

    <Button
        android:layout_weight="1"
        android:text="移动"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button4" />

</LinearLayout>

    <ImageView
        android:layout_width="200dp"
        android:layout_height="200dp"
        app:srcCompat="@mipmap/ic_launcher"
        android:layout_centerInParent="true"
        android:layout_centerVertical="true"
        android:id="@+id/imageView" />


</RelativeLayout>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值