propertyanim悬浮球

来源:互联网 发布:浪登西装 怎么样 知乎 编辑:程序博客网 时间:2024/04/28 17:54
效果 :     点击图片 四周跳出四张图片 如同手机上的悬浮球xml文件:<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="#555555"    tools:context="com.audrey.propertyanim.Main2Activity">    <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>Activity代码 
public class Main2Activity 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表示已经显示了    private TextView skip;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main2);        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);        skip = (TextView) findViewById(R.id.skip_tv);    }    @Override    public void onClick(View v) {        switch (v.getId()) {            case R.id.iv_circle:                if (!flag) {                    //点击把所有图标都展示出来                    show();                } else {                    //点击把所有图标都收起来                    dismiss();                }                flag = !flag;                break;        }    }    private void show() {        ObjectAnimator tranLeft = ObjectAnimator.ofFloat(mIvLeft, "TranslationX", 0f, -150f);        ObjectAnimator tranRight = ObjectAnimator.ofFloat(mIvRight, "TranslationX", 0f, 150f);        ObjectAnimator tranTop = ObjectAnimator.ofFloat(mIvTop, "TranslationY", 0f, -150f);        ObjectAnimator tranBottom = ObjectAnimator.ofFloat(mIvBottom, "TranslationY", 0f, 150f);        AnimatorSet animatorSet = new AnimatorSet();        animatorSet.playTogether(tranBottom, tranLeft, tranRight, tranTop);        animatorSet.setDuration(1000);        animatorSet.start();    }    private void dismiss() {        ObjectAnimator tranLeft = ObjectAnimator.ofFloat(mIvLeft, "TranslationX", -100f, 0f);        ObjectAnimator tranRight = ObjectAnimator.ofFloat(mIvRight, "TranslationX", 100f, 0f);        ObjectAnimator tranTop = ObjectAnimator.ofFloat(mIvTop, "TranslationY", -100f, 0f);        ObjectAnimator tranBottom = ObjectAnimator.ofFloat(mIvBottom, "TranslationY", 100f, 0f);        AnimatorSet animatorSet = new AnimatorSet();        animatorSet.playTogether(tranBottom, tranLeft, tranRight, tranTop);        animatorSet.setDuration(2000);        animatorSet.start();    }}

原创粉丝点击