平移动画
👱
1、具体操作步骤
创建ImageView对象
创建ObjectAnimator对象
通过ofFloat方法实现平移
2、具体实施
1、创建ImageView
<ImageView
android:id="@+id/car"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@mipmap/car"/>
2、创建ObjectAnimator对象
第一位参数是需要移动的图片
第二位参数是设置在什么轴移动,例子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();
}
}
}
一个最简单的平移动画就实现了。