今天就跟大家聊聊有关Android绘制平移动画的示例代码怎么编写,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
创建ImageView对象
创建ObjectAnimator对象
通过ofFloat方法实现平移
<ImageView
android:id="@+id/car"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@mipmap/car"/>
1.第一位参数是需要移动的图片
2.第二位参数是设置在什么轴移动,例子translationX,就是在X轴移动
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(car, "translationX", 0f, -200);
objectAnimator.setDuration(2000);
objectAnimator.start();
<?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>
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绘制平移动画的示例代码怎么编写有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
网络异常,请检查网络