Android动画移动

Posted 吹着空调哼着歌

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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();
    

以上是关于Android动画移动的主要内容,如果未能解决你的问题,请参考以下文章

Android开发 动画之translate(位移动画)

Android动画上下两个View,下边的往上移动覆盖后,上边的设置GONE,下边的向下移动,上边的显示VISIABLE

android帧动画,移动位置,缩放,改变透明度等动画解说

Android移动应用开发之制作动画基础

Android动画移动

Android动画移动