【AndroidUI设计】实现渐变、缩放、旋转、平移的效果

  • 描述:使用Animation动画,实现渐变、缩放、旋转、平移的效果
  • 难度:初级
  • 知识点:Animation

使用Animation实现渐变、缩放、旋转、平移的效果

一、渐变

关于xml-set-translate的知识,需要学习上一篇博客(http://t.csdn.cn/OJYan)

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

        <alpha
            android:interpolator="@android:anim/linear_interpolator"
            android:repeatMode="reverse"
            android:repeatCount="infinite"
            android:duration="1000"
            android:fromAlpha="1.0"
            android:toAlpha="0.0"/>
            
</set>

二、缩放

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <scale
        android:repeatMode="reverse"
        android:repeatCount="infinite"
        android:duration="3000"
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:toXScale="0.5"
        android:toYScale="0.5"
        android:pivotX="50%"
        android:pivotY="50%"/>
        
</set>

三、旋转

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <rotate
        android:fromDegrees="0"
        android:toDegrees="360"
        android:pivotX="50%"
        android:pivotY="50%"
        android:repeatCount="infinite"
        android:repeatMode="reverse"
        android:duration="1000"/>
        
</set>

四、平移

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <translate
        android:fromXDelta="0.0"
        android:fromYDelta="0.0"
        android:toXDelta="0.0"
        android:toYDelta="-100"
        android:repeatCount="infinite"
        android:repeatMode="reverse"
        android:duration="4000"/>
        
</set>

五、详细使用

1、UI界面设计

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/image_xm"
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:src="@drawable/xiongmao"
        android:layout_marginTop="100dp"
        android:layout_marginLeft="150dp"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="100dp"
        android:layout_below="@id/image_xm"
        android:gravity="center">

        <Button
            android:id="@+id/bin_alpha"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="渐变"/>

        <Button
            android:id="@+id/bin_rotate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="旋转"/>

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

        <Button
            android:id="@+id/bin_translate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="平移"/>

    </LinearLayout>

</RelativeLayout>

2、Activity编码

    ImageView XioMao;
    Button alpha,rotate,scale,translate;

	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
		// 初始化
        init();
    }
    
    private void init(){
        XioMao = findViewById(R.id.image_xm);
        alpha = findViewById(R.id.bin_alpha);
        rotate = findViewById(R.id.bin_rotate);
        scale = findViewById(R.id.bin_scale);
        translate = findViewById(R.id.bin_translate);

        alpha.setOnClickListener(this);
        rotate.setOnClickListener(this);
        scale.setOnClickListener(this);
        translate.setOnClickListener(this);
    }
    
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bin_alpha:
            	// 方法一
                // Animation alpha = AnimationUtils.loadAnimation(this,R.anim.alpha_anim);
                // XioMao.startAnimation(alpha);
                // 方法二
                AlphaAnimation alphaAnimation = new AlphaAnimation(0.0f,1.0f);
                alphaAnimation.setRepeatMode(AlphaAnimation.REVERSE);
                alphaAnimation.setDuration(5000);
                alphaAnimation.setRepeatCount(AlphaAnimation.INFINITE);
                XioMao.startAnimation(alphaAnimation);
                break;
            case R.id.bin_rotate:
                Animation rotate = AnimationUtils.loadAnimation(this,R.anim.rotate_anim);
                XioMao.startAnimation(rotate);
                break;
            case R.id.bin_scale:
                Animation scale = AnimationUtils.loadAnimation(this,R.anim.scale_anim);
                XioMao.startAnimation(scale);
                break;
            case R.id.bin_translate:
                Animation translate = AnimationUtils.loadAnimation(this,R.anim.translate_anim);
                XioMao.startAnimation(translate);
                break;
        }
    }
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

云端new守夜人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值