一對一直播app源碼Android控件顯示、隱藏時,增加動畫效果

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;
        }
    }
}

有兩個動畫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" />
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章