点击弹出图片

本文介绍了一个使用Android动画框架实现手机主界面图标展开与收起效果的应用案例。通过AnimatorSet同步控制多个图标的位置变化,实现了流畅的动画效果。
//main

package com.bwei.cellphonedemo;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
/*
* 手机主界面收放图片
* */

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private ImageView mIvTop;
    private ImageView mIvRight;
    private ImageView mIvBottom;
    private ImageView mIvLeft;
    private ImageView mIvCircle;
    private boolean flag;//false表示未显示,true表示已经显示了

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

        initView();
    }

    private void initView() {

        mIvTop = (ImageView) findViewById(R.id.iv_top);
        mIvRight = (ImageView) findViewById(R.id.iv_right);
        mIvBottom = (ImageView) findViewById(R.id.iv_bottom);
        mIvLeft = (ImageView) findViewById(R.id.iv_left);
        mIvCircle = (ImageView) findViewById(R.id.iv_circle);
        mIvCircle.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {

        switch (view.getId()){
            case R.id.iv_circle:
                if(!flag){

                    ////点击把所有图标都展示出来
                    show();
                }else{
                    //点击把所有图标都收起来
                    dismiss();
                }
                flag=!flag;
                break;
        }

    }
    /*
    * 展示图片
    * */
    private void show() {

        ObjectAnimator tranLeft = ObjectAnimator.ofFloat(mIvLeft, "TranslationX", 0f, -200f);
        ObjectAnimator tranRight = ObjectAnimator.ofFloat(mIvRight, "TranslationX", 0f, 200f);
        ObjectAnimator tranTop = ObjectAnimator.ofFloat(mIvTop, "TranslationY", 0f, -200f);
        ObjectAnimator tranBottom = ObjectAnimator.ofFloat(mIvBottom, "TranslationY", 0f, 200f);

        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.playTogether(tranBottom, tranLeft, tranRight, tranTop);
        animatorSet.setDuration(2000);
        animatorSet.start();

    }
    /*
    * 收起图片
    * */
    private void dismiss() {
        ObjectAnimator tranLeft = ObjectAnimator.ofFloat(mIvLeft, "TranslationX", -200f, 0f);
        ObjectAnimator tranRight = ObjectAnimator.ofFloat(mIvRight, "TranslationX", 200f, 0f);
        ObjectAnimator tranTop = ObjectAnimator.ofFloat(mIvTop, "TranslationY", -200f, 0f);
        ObjectAnimator tranBottom = ObjectAnimator.ofFloat(mIvBottom, "TranslationY", 200f, 0f);

        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.playTogether(tranBottom, tranLeft, tranRight, tranTop);
        animatorSet.setDuration(2000);
        animatorSet.start();

    }

}
// 布局
<?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"
    android:background="#11111111"
    tools:context="com.bwei.cellphonedemo.MainActivity">

    <ImageView
        android:id="@+id/iv_top"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="@drawable/top" />

    <ImageView
        android:id="@+id/iv_right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="@drawable/right" />

    <ImageView
        android:id="@+id/iv_bottom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="@drawable/bottom" />

    <ImageView
        android:id="@+id/iv_left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="@drawable/left" />

    <ImageView
        android:id="@+id/iv_circle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="@drawable/circle" />

</RelativeLayout>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值