Android控件显示、隐藏时,增加动画效果

简介: Android控件显示、隐藏时,增加动画效果

Android控件显示、隐藏时,增加动画效果


首先还是看一下演示效果吧,不然凭什么相信我的帖子能解决你的问题呢?

效果GIF如下

image.gif

动画效果就是这样,如果不符合你的要求,就不浪费你宝贵的时间了,如果是你想要的效果就请往下看。


话不多说,我直接贴代码,有不明白的可以在评论区问我:


activity_main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    android:gravity="center_horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <!--普通显示隐藏-->
    <ImageView
        android:layout_centerHorizontal="true"
        android:visibility="invisible"
        android:layout_marginTop="20dp"
        android:id="@+id/iv_logo"
        android:background="@mipmap/ic_launcher"
        android:layout_width="100dp"
        android:layout_height="100dp"/>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <Button
            android:text="普通显示"
            android:id="@+id/btn_show"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <Button
            android:text="普通隐藏"
            android:id="@+id/btn_hide"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </LinearLayout>
    <!--透明度显示隐藏-->
    <ImageView
        android:visibility="invisible"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:id="@+id/iv_alpha_logo"
        android:background="@mipmap/ic_launcher"
        android:layout_width="100dp"
        android:layout_height="100dp"/>
    <LinearLayout
        android:layout_marginTop="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <Button
            android:text="透明度显示"
            android:id="@+id/btn_alpha_show"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <Button
            android:text="透明度隐藏"
            android:id="@+id/btn_alpha_hide"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </LinearLayout>
    <!--缩放显示隐藏-->
    <ImageView
        android:visibility="invisible"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:id="@+id/iv_scale_logo"
        android:background="@mipmap/ic_launcher"
        android:layout_width="100dp"
        android:layout_height="100dp"/>
    <LinearLayout
        android:layout_marginTop="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <Button
            android:text="放大显示"
            android:id="@+id/btn_scale_show"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <Button
            android:text="缩小隐藏"
            android:id="@+id/btn_scale_hide"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </LinearLayout>
    <!--位移显示隐藏-->
    <ImageView
        android:visibility="invisible"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:id="@+id/iv_translate_logo"
        android:background="@mipmap/ic_launcher"
        android:layout_width="100dp"
        android:layout_height="100dp"/>
    <LinearLayout
        android:layout_marginTop="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <Button
            android:text="位移显示(向上)"
            android:id="@+id/btn_translate_show"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <Button
            android:text="位移隐藏(向下)"
            android:id="@+id/btn_translate_hide"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </LinearLayout>
</LinearLayout>

MainActivity.java

package com.llw.animationdemo;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private ImageView ivLogo, ivAlphaLogo, ivTranslateLogo, ivScaleLogo;
    private Button btnShow, btnHide, btnAlphaShow, btnAlphaHide, btnTranslateShow,
            btnTranslateHide, btnScaleShow, btnScaleHide;
    private AlphaAnimation alphaAniShow, alphaAniHide;
    private TranslateAnimation translateAniShow, translateAniHide;
    private Animation bigAnimation, smallAnimation;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }
    //初始化控件
    private void initView() {
        ivLogo = (ImageView) findViewById(R.id.iv_logo);
        ivAlphaLogo = (ImageView) findViewById(R.id.iv_alpha_logo);
        ivTranslateLogo = (ImageView) findViewById(R.id.iv_translate_logo);
        ivScaleLogo = (ImageView) findViewById(R.id.iv_scale_logo);
        btnShow = (Button) findViewById(R.id.btn_show);
        btnHide = (Button) findViewById(R.id.btn_hide);
        btnAlphaShow = (Button) findViewById(R.id.btn_alpha_show);
        btnAlphaHide = (Button) findViewById(R.id.btn_alpha_hide);
        btnTranslateShow = (Button) findViewById(R.id.btn_translate_show);
        btnTranslateHide = (Button) findViewById(R.id.btn_translate_hide);
        btnScaleShow = (Button) findViewById(R.id.btn_scale_show);
        btnScaleHide = (Button) findViewById(R.id.btn_scale_hide);
        btnShow.setOnClickListener(this);
        btnHide.setOnClickListener(this);
        btnAlphaShow.setOnClickListener(this);
        btnAlphaHide.setOnClickListener(this);
        btnTranslateShow.setOnClickListener(this);
        btnTranslateHide.setOnClickListener(this);
        btnScaleShow.setOnClickListener(this);
        btnScaleHide.setOnClickListener(this);
        alphaAnimation();
        scaleAnimation();
        translateAnimation();
    }
    //位移动画
    private void translateAnimation() {
        //向上位移显示动画  从自身位置的最下端向上滑动了自身的高度
        translateAniShow = new TranslateAnimation(
                Animation.RELATIVE_TO_SELF,//RELATIVE_TO_SELF表示操作自身
                0,//fromXValue表示开始的X轴位置
                Animation.RELATIVE_TO_SELF,
                0,//fromXValue表示结束的X轴位置
                Animation.RELATIVE_TO_SELF,
                1,//fromXValue表示开始的Y轴位置
                Animation.RELATIVE_TO_SELF,
                0);//fromXValue表示结束的Y轴位置
        translateAniShow.setRepeatMode(Animation.REVERSE);
        translateAniShow.setDuration(1000);
        //向下位移隐藏动画  从自身位置的最上端向下滑动了自身的高度
        translateAniHide = new TranslateAnimation(
                Animation.RELATIVE_TO_SELF,//RELATIVE_TO_SELF表示操作自身
                0,//fromXValue表示开始的X轴位置
                Animation.RELATIVE_TO_SELF,
                0,//fromXValue表示结束的X轴位置
                Animation.RELATIVE_TO_SELF,
                0,//fromXValue表示开始的Y轴位置
                Animation.RELATIVE_TO_SELF,
                1);//fromXValue表示结束的Y轴位置
        translateAniHide.setRepeatMode(Animation.REVERSE);
        translateAniHide.setDuration(1000);
    }
    //缩放动画
    private void scaleAnimation() {
        //放大
        bigAnimation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.scale_big);
        //缩小
        smallAnimation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.scale_small);
    }
    //透明度动画
    private void alphaAnimation() {
        //显示
        alphaAniShow = new AlphaAnimation(0, 1);//百分比透明度,从0%到100%显示
        alphaAniShow.setDuration(1000);//一秒
        //隐藏
        alphaAniHide = new AlphaAnimation(1, 0);
        alphaAniHide.setDuration(1000);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_show://普通显示
                ivLogo.setVisibility(View.VISIBLE);
                break;
            case R.id.btn_hide://普通隐藏
                ivLogo.setVisibility(View.INVISIBLE);
                break;
            case R.id.btn_alpha_show://透明度显示
                ivAlphaLogo.startAnimation(alphaAniShow);
                ivAlphaLogo.setVisibility(View.VISIBLE);
                break;
            case R.id.btn_alpha_hide://透明度隐藏
                ivAlphaLogo.startAnimation(alphaAniHide);
                //这个地方为什么要做动画的监听呢,因为隐藏和显示不一样,
                //必须在动画结束之后再隐藏你的控件,这样才不会显得很突兀
                alphaAniHide.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {
                    }
                    @Override
                    public void onAnimationEnd(Animation animation) {
                        ivAlphaLogo.setVisibility(View.INVISIBLE);
                    }
                    @Override
                    public void onAnimationRepeat(Animation animation) {
                    }
                });
                break;
            case R.id.btn_scale_show://放大显示
                ivScaleLogo.startAnimation(bigAnimation);
                ivScaleLogo.setVisibility(View.VISIBLE);
                break;
            case R.id.btn_scale_hide://缩小隐藏
                ivScaleLogo.startAnimation(smallAnimation);
                smallAnimation.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {
                    }
                    @Override
                    public void onAnimationEnd(Animation animation) {
                        ivScaleLogo.setVisibility(View.INVISIBLE);
                    }
                    @Override
                    public void onAnimationRepeat(Animation animation) {
                    }
                });
                break;
            case R.id.btn_translate_show://向上位移显示
                ivTranslateLogo.startAnimation(translateAniShow);
                ivTranslateLogo.setVisibility(View.VISIBLE);
                break;
            case R.id.btn_translate_hide://向下位移隐藏
                ivTranslateLogo.startAnimation(translateAniHide);
                translateAniHide.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {
                    }
                    @Override
                    public void onAnimationEnd(Animation animation) {
                        ivTranslateLogo.setVisibility(View.INVISIBLE);
                    }
                    @Override
                    public void onAnimationRepeat(Animation animation) {
                    }
                });
                break;
        }
    }
}


image.png


还有两个动画xml文件,如下:

scale_big.xml

<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:fromXScale="0"
    android:fromYScale="0"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toXScale="1"
    android:toYScale="1" />

scale_small.xml

<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:fromXScale="1"
    android:fromYScale="1"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toXScale="0"
    android:toYScale="0" />


你只要把上面的代码复制到你的项目里面即可实现效果,很简单的,有什么问题可以直接评论区发问,我会第一时间回复的。

相关文章
|
Android开发 开发者
Android利用SVG实现动画效果
本文介绍了如何在Android中利用SVG实现动画效果。首先通过定义`pathData`参数(如M、L、Z等)绘制一个简单的三角形SVG图形,然后借助`objectAnimator`实现动态的线条绘制动画。文章详细讲解了从配置`build.gradle`支持VectorDrawable,到创建动画文件、关联SVG与动画,最后在Activity中启动动画的完整流程。此外,还提供了SVG绘制原理及工具推荐,帮助开发者更好地理解和应用SVG动画技术。
671 30
|
Android开发 UED 计算机视觉
Android自定义view之线条等待动画(灵感来源:金铲铲之战)
本文介绍了一款受游戏“金铲铲之战”启发的Android自定义View——线条等待动画的实现过程。通过将布局分为10份,利用`onSizeChanged`测量最小长度,并借助画笔绘制动态线条,实现渐变伸缩效果。动画逻辑通过四个变量控制线条的增长与回退,最终形成流畅的等待动画。代码中详细展示了画笔初始化、线条绘制及动画更新的核心步骤,并提供完整源码供参考。此动画适用于加载场景,提升用户体验。
775 5
Android自定义view之线条等待动画(灵感来源:金铲铲之战)
|
API Android开发 开发者
Android颜色渐变动画效果的实现
本文介绍了在Android中实现颜色渐变动画效果的方法,重点讲解了插值器(TypeEvaluator)的使用与自定义。通过Android自带的颜色插值器ArgbEvaluator,可以轻松实现背景色的渐变动画。文章详细分析了ArgbEvaluator的核心代码,并演示了如何利用Color.colorToHSV和Color.HSVToColor方法自定义颜色插值器MyColorEvaluator。最后提供了完整的源码示例,包括ColorGradient视图类和MyColorEvaluator类,帮助开发者更好地理解和应用颜色渐变动画技术。
450 3
|
Android开发 开发者
Android SVG动画详细例子
本文详细讲解了在Android中利用SVG实现动画效果的方法,通过具体例子帮助开发者更好地理解和应用SVG动画。文章首先展示了动画的实现效果,接着回顾了之前的文章链接及常见问题(如属性名大小写错误)。核心内容包括:1) 使用阿里图库获取SVG图形;2) 借助工具将SVG转换为VectorDrawable;3) 为每个路径添加动画绑定属性;4) 创建动画文件并关联SVG;5) 在ImageView中引用动画文件;6) 在Activity中启动动画。文末还提供了完整的代码示例和源码下载链接,方便读者实践操作。
636 65
|
XML Java Maven
Android线条等待动画JMWorkProgress(可添加依赖直接使用)
这是一篇关于Android线条等待动画JMWorkProgress的教程文章,作者计蒙将其代码开源至GitHub,提升可读性。文章介绍了如何通过添加依赖库使用该动画,并详细讲解了XML与Java中的配置方法,包括改变线条颜色、宽度、添加文字等自定义属性。项目已支持直接依赖集成(`implementation &#39;com.github.Yufseven:JMWorkProgress:v1.0&#39;`),开发者可以快速上手实现炫酷的等待动画效果。文末附有GitHub项目地址,欢迎访问并点赞支持!
434 26
|
XML Android开发 数据格式
Android中SlidingDrawer利用透明动画提示效果
本文介绍了在Android中使用`SlidingDrawer`实现带有透明动画提示效果的方法。通过XML布局配置`SlidingDrawer`的把手(handle)和内容(content),结合Activity中的代码实现动态动画效果。最终实现了交互性强、视觉效果良好的滑动抽屉功能。
209 1
Android中SlidingDrawer利用透明动画提示效果
|
XML Java Android开发
Android 动画之帧动画 + 补间动画 + 属性动画
本文介绍了Android开发中的三种动画类型:帧动画、补间动画和属性动画。帧动画通过依次播放一系列静态图片实现动态效果,支持Java代码与XML两种实现方式。补间动画基于起始和结束位置自动生成过渡效果,涵盖透明度、位移、旋转、缩放及组合动画等多种形式,并可搭配插值器优化动画过程。属性动画则通过改变对象属性实现动画,支持透明度、位移、旋转、缩放及组合动画,灵活性更高且适用于更复杂的场景。文中提供了详细的代码示例,帮助开发者快速上手。
628 15
|
XML 搜索推荐 Android开发
Android改变进度条控件progressbar的样式(根据源码修改)
本文介绍了如何基于Android源码自定义ProgressBar样式。首先分析了系统源码中ProgressBar样式的定义,发现其依赖一张旋转图片实现动画效果。接着分两步指导开发者实现自定义:1) 模仿源码创建一个旋转动画XML文件(放置在drawable文件夹),修改图片为自定义样式;2) 在UI控件中通过`indeterminateDrawable`属性应用该动画。最终实现简单且个性化的ProgressBar效果,附带效果图展示。
737 2
|
Android开发
Android控件样式的抽取(小提及快捷方式)
在Android开发中,若多个控件样式重复,可抽取公共部分以简化代码。例如对EditText提取样式,通过编辑`styles.xml`实现复用。为提高效率,Android Studio提供自动提取Style功能:右键点击控件样式选项,选择“Style...”,勾选需要提取的属性后确认,即可快速生成样式代码,显著提升开发便利性。
397 2

热门文章

最新文章