Android动画移动

实现方式

TranslateAnimation translateAnimation = new TranslateAnimation(
                        TranslateAnimation.RELATIVE_TO_SELF, -1, TranslateAnimation.RELATIVE_TO_SELF, 0,
                        //0代表控件本身的位置
                        TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF, 0);
                translateAnimation.setDuration(900);
                mViewReward.startAnimation(translateAnimation);

dmeo

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".MainActivity">

    <androidx.constraintlayout.widget.ConstraintLayout
        app:layout_constraintLeft_toLeftOf="parent"
        android:id="@+id/reward_view"
        app:layout_constraintBottom_toTopOf="@id/reward"
        android:layout_marginBottom="100dp"
        android:visibility="gone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/left_image"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:layout_width="60dp"
            android:layout_height="60dp"/>

        <TextView
            android:id="@+id/tv"
            app:layout_constraintLeft_toRightOf="@id/left_image"
            app:layout_constraintTop_toTopOf="@id/left_image"
            app:layout_constraintBottom_toBottomOf="@id/left_image"
            android:layout_marginLeft="10dp"
            android:text="穿云箭"
            android:textSize="14sp"
            android:textColor="#000000"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

        <ImageView
            android:id="@+id/right_image"
            app:layout_constraintLeft_toRightOf="@id/tv"
            app:layout_constraintTop_toTopOf="parent"
            android:layout_marginLeft="10dp"
            android:layout_width="60dp"
            android:layout_height="60dp"/>

        <TextView
            android:id="@+id/number"
            android:gravity="center"
            android:textSize="30sp"
            android:textColor="#FF5722"
            app:layout_constraintLeft_toRightOf="@id/right_image"
            app:layout_constraintTop_toTopOf="parent"
            android:text="x90"
            app:layout_constraintBottom_toBottomOf="parent"
            android:layout_width="60dp"
            android:layout_height="0dp"/>

    </androidx.constraintlayout.widget.ConstraintLayout>

    <Button
        android:id="@+id/reward"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</androidx.constraintlayout.widget.ConstraintLayout>
public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageView mImageLeft;
    private TextView mTv;
    private ImageView mImageRight;
    private TextView mNumber;
    private Button mReward;
    private ConstraintLayout mViewReward;

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

    private void initView() {
        mImageLeft = (ImageView) findViewById(R.id.left_image);
        mTv = (TextView) findViewById(R.id.tv);
        mImageRight = (ImageView) findViewById(R.id.right_image);
        mNumber = (TextView) findViewById(R.id.number);
        mReward = (Button) findViewById(R.id.reward);
        mReward.setOnClickListener(this);
        mViewReward = (ConstraintLayout) findViewById(R.id.reward_view);

        Glide.with(this).load("https://avatar.csdnimg.cn/E/C/F/1_weixin_45680654_1609595020.jpg")
                .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                .into(mImageLeft);

        Glide.with(this).load("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fww4.sinaimg.cn%2Flarge%2F9150e4e5ly1fhhjs6mu89j20hs0hsqdk.jpg&refer=http%3A%2F%2Fwww.sina.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1632467419&t=cb6ff9f1c7d67e67ffc7c9984cbb3daa")
                .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                .into(mImageRight);
    }

    private CountDownTimer countDownTimer = new CountDownTimer(3000,1000) {
        @Override
        public void onTick(long millisUntilFinished) {

        }

        @Override
        public void onFinish() {
            TranslateAnimation translateAnimation = new TranslateAnimation(
                    TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF, -1,
                    //0代表控件本身的位置
                    TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF, 0);
            translateAnimation.setDuration(900);
            mViewReward.startAnimation(translateAnimation);

            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    mViewReward.setVisibility(View.GONE);
                }
            },800);
        }
    };

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.reward:
                // TODO 21/08/25
                Random ra =new Random();
                int i = ra.nextInt(101 - 10) + 10;
                mNumber.setText("x"+i);

                mViewReward.setVisibility(View.VISIBLE);

                TranslateAnimation translateAnimation = new TranslateAnimation(
                        TranslateAnimation.RELATIVE_TO_SELF, -1, TranslateAnimation.RELATIVE_TO_SELF, 0,
                        //0代表控件本身的位置
                        TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF, 0);
                translateAnimation.setDuration(900);
                mViewReward.startAnimation(translateAnimation);
                countDownTimer.start();
                break;
            default:
                break;
        }
    }

    @Override
    protected void onDestroy() {
        if (countDownTimer!=null){
            countDownTimer.cancel();
        }
        super.onDestroy();
    }

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值