網站首頁 編程語言 正文
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(); } } }
一個最簡單的平移動畫就實現了
原文鏈接:https://blog.csdn.net/w_Eternal/article/details/122278591
相關推薦
- 2023-04-02 pytorch中關于distributedsampler函數的使用_python
- 2022-07-07 python?函數定位參數+關鍵字參數+inspect模塊_python
- 2023-05-06 react中定義變量并使用方式_React
- 2022-10-23 Android性能優化之ANR問題定位分析_Android
- 2023-06-05 python中xlwt模塊的具體用法_python
- 2022-05-20 python繪制餅圖的方法詳解_python
- 2022-08-14 python中matplotlib調整圖例位置的方法實例_python
- 2022-06-16 C#使用符號表實現查找算法_C#教程
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支