Android绘制平移动画的示例代码

2022-10-19

这篇文章主要展示的是利用Android绘制平移动画的示例代码,文中的实现步骤讲解详细,对我们学习Android有一定的帮助,感兴趣的可以试一试

目录
  • 1、具体操作步骤
  • 2、具体实施
    • 创建ImageView
    • 创建ObjectAnimator对象
  • 3、具体实例
    • activity_main.xml
    • MainActivity.java

1、具体操作步骤

创建ImageView对象

创建ObjectAnimator对象

通过ofFloat方法实现平移

2、具体实施

创建ImageView

<ImageView
        android:id="@+id/car"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:src="@mipmap/car"/>

创建ObjectAnimator对象

1.第一位参数是需要移动的图片

2.第二位参数是设置在什么轴移动,例子translationX,就是在X轴移动

ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(car, "translationX", 0f, -200);
            objectAnimator.setDuration(2000);
            objectAnimator.start();

3、具体实例

activity_main.xml

<?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"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/car"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:src="@mipmap/car"/>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:orientation="horizontal"
        android:layout_below="@id/car"
        android:layout_marginTop="100dp">
        <Button
            android:id="@+id/left"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Forward"
            android:textAllCaps="false"/>
        <Button
            android:id="@+id/reset"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Reset"
            android:textAllCaps="false"
            android:layout_marginLeft="20dp"/>
        <Button
            android:id="@+id/right"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Backword"
            android:textAllCaps="false"
            android:layout_marginLeft="20dp"/>
    </LinearLayout>
</RelativeLayout>

MainActivity.java

package com.example.a4_10_float;

import androidx.appcompat.app.AppCompatActivity;

import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    private ImageView car;
    private Button left;
    private Button reset;
    private Button right;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        car = findViewById(R.id.car);
        left = findViewById(R.id.left);
        reset = findViewById(R.id.reset);
        right = findViewById(R.id.right);
    }

    @Override
    protected void onStart() {
        super.onStart();
        left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Floaat(1);
            }
        });
        reset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Floaat(0);
            }
        });
        right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Floaat(2);
            }
        });
    }
	//封装好一个方法,开控制向左向右移动和回到初始位置
    private void Floaat(int a) {
        if (a==1) {
            ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(car, "translationX", 0f, -200);
            objectAnimator.setDuration(2000);
            objectAnimator.start();
        }else if (a==0){
            ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(car, "translationX", 0f, 0);
            objectAnimator.setDuration(2000);
            objectAnimator.start();
        }else if (a==2){
            ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(car, "translationX", 0f, 200);
            objectAnimator.setDuration(2000);
            objectAnimator.start();
        }
    }
}

一个最简单的平移动画就实现了

到此这篇关于Android绘制平移动画的示例代码的文章就介绍到这了,更多相关Android平移动画内容请搜索北冥有鱼以前的文章或继续浏览下面的相关文章希望大家以后多多支持北冥有鱼!

您可能感兴趣的文章:

  • Android旋转、平移、缩放和透明度渐变的补间动画
  • Android Animation之TranslateAnimation(平移动画)
  • Android动画 实现开关按钮动画(属性动画之平移动画)实例代码
  • Android模拟开关按钮点击打开动画(属性动画之平移动画)
  • Android实现循环平移动画示例

《Android绘制平移动画的示例代码.doc》

下载本文的Word格式文档,以方便收藏与打印。