android仿饿了么点餐动画

先上代码

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:id="@+id/relayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#D4D0C8"
    tools:context="com.tianxia.hnq.donghuatext.MainActivity">

    <ImageView
        android:id="@+id/image002"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:src="@mipmap/ic_launcher"
        ></ImageView>

</RelativeLayout>
activity:

package com.tianxia.hnq.donghuatext;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.relayout)
    RelativeLayout relayout;
    @BindView(R.id.image002)
    ImageView image002;
    private int h;//屏幕高度
    private int h001;//控件高度
    private int statusBarHeight1;//状态栏高度


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        h = getResources().getDisplayMetrics().heightPixels;
        //获取状态栏高度
        //获取status_bar_height资源的ID
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            //根据资源ID获取响应的尺寸值
            statusBarHeight1 = getResources().getDimensionPixelSize(resourceId);
        }
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        h001 = (int) image002.getHeight();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        final int action = event.getAction();
        float x = event.getX();
        float y = event.getY();
        switch (action) {
            case MotionEvent.ACTION_UP:
                //属性动画效果
                final ImageView image003 = new ImageView(this);
                image003.setImageResource(R.mipmap.ic_launcher);
                image003.setVisibility(View.VISIBLE);
                relayout.addView(image003);
                //x轴动画
               // image003.setX(x);
                //image003.setY(y - statusBarHeight1);
                ObjectAnimator animator = ObjectAnimator.ofFloat(image003, "x", x, 0);
                animator.setDuration(1000);
                animator.setInterpolator(new LinearInterpolator());
                //Y轴动画
                ObjectAnimator animator1 = ObjectAnimator.ofFloat(image003, "y", y -
                        statusBarHeight1, h - h001 - statusBarHeight1);
                animator1.setDuration(1000);
                animator1.setInterpolator(new AccelerateInterpolator());
                animator.addListener(new Animator.AnimatorListener() {
                    @Override
                    public void onAnimationStart(Animator animation) {
                        Log.i("donghua", "onAnimationStart");
                    }

                    @Override
                    public void onAnimationEnd(Animator animation) {
                        relayout.removeView(image003);
                        ObjectAnimator animator2 = ObjectAnimator.ofFloat(image002, "ScaleX", 1.0f, 0.6f,1.0f,0.8f,1.0f);
                        animator2.setDuration(250);
                        animator2.setInterpolator(new DecelerateInterpolator());
                        ObjectAnimator animator3 = ObjectAnimator.ofFloat(image002, "ScaleY", 1.0f, 0.6f,1.0f,0.8f,1.0f);
                        animator3.setDuration(250);
                        animator3.setInterpolator(new DecelerateInterpolator());
                        AnimatorSet set=new AnimatorSet();
                        set.play(animator2).with(animator3);
                        set.start();
                    }

                    @Override
                    public void onAnimationCancel(Animator animation) {
                        Log.i("donghua", "onAnimationCancel");
                    }

                    @Override
                    public void onAnimationRepeat(Animator animation) {
                        Log.i("donghua", "onAnimationRepeat");
                    }
                });
                AnimatorSet set = new AnimatorSet();
                set.playTogether(animator, animator1);
                set.start();
                break;
        }
        return super.onTouchEvent(event);
    }

}


注意事项:属性动画里面的属性坐标是相对于父布局的坐标,这里主题是noactionbar所以没有考虑actionbar的高度,但是考虑了状态栏的高度。且这new的控件必须的添加到relayout之类的才行,framelayout不行,猜测要计算高度什么的,初始化完成才行,如果直接XML添加的使用属性动画不需要填加到容器中

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值