Android繪制平移動(dòng)畫(huà)的示例代碼
1、具體操作步驟
創(chuàng)建ImageView對(duì)象
創(chuàng)建ObjectAnimator對(duì)象
通過(guò)ofFloat方法實(shí)現(xiàn)平移
2、具體實(shí)施
創(chuàng)建ImageView
<ImageView android:id="@+id/car" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:src="@mipmap/car"/>
創(chuàng)建ObjectAnimator對(duì)象
1.第一位參數(shù)是需要移動(dòng)的圖片
2.第二位參數(shù)是設(shè)置在什么軸移動(dòng),例子translationX,就是在X軸移動(dòng)
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(car, "translationX", 0f, -200); objectAnimator.setDuration(2000); objectAnimator.start();
3、具體實(shí)例
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); } }); } //封裝好一個(gè)方法,開(kāi)控制向左向右移動(dòng)和回到初始位置 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(); } } }
一個(gè)最簡(jiǎn)單的平移動(dòng)畫(huà)就實(shí)現(xiàn)了
到此這篇關(guān)于Android繪制平移動(dòng)畫(huà)的示例代碼的文章就介紹到這了,更多相關(guān)Android平移動(dòng)畫(huà)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Android旋轉(zhuǎn)、平移、縮放和透明度漸變的補(bǔ)間動(dòng)畫(huà)
- Android Animation之TranslateAnimation(平移動(dòng)畫(huà))
- Android動(dòng)畫(huà) 實(shí)現(xiàn)開(kāi)關(guān)按鈕動(dòng)畫(huà)(屬性動(dòng)畫(huà)之平移動(dòng)畫(huà))實(shí)例代碼
- Android模擬開(kāi)關(guān)按鈕點(diǎn)擊打開(kāi)動(dòng)畫(huà)(屬性動(dòng)畫(huà)之平移動(dòng)畫(huà))
- Android實(shí)現(xiàn)循環(huán)平移動(dòng)畫(huà)示例
相關(guān)文章
Android 超詳細(xì)SplashScreen入門(mén)教程
Android 12正式版即將發(fā)布,有一個(gè)非常顯著的視覺(jué)變化就是,Android 12強(qiáng)制給所有的App都增加了SplashScreen的功能。是的,即使你什么都不做,只要你的App安裝到了Android 12手機(jī)上,都會(huì)自動(dòng)擁有這個(gè)新功能2022-03-03Android實(shí)現(xiàn)音樂(lè)視頻播放
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)音樂(lè)視頻播放,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-05-05Android實(shí)現(xiàn)簡(jiǎn)單購(gòu)物車(chē)
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)簡(jiǎn)單購(gòu)物車(chē),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06Android實(shí)現(xiàn)手勢(shì)滑動(dòng)(左滑和右滑)
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)手勢(shì)滑動(dòng),左滑和右滑效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-07-07Android編程錄音工具類(lèi)RecorderUtil定義與用法示例
這篇文章主要介紹了Android編程錄音工具類(lèi)RecorderUtil定義與用法,結(jié)合實(shí)例形式分析了Android錄音工具類(lèi)實(shí)現(xiàn)開(kāi)始錄音、停止錄音、取消錄音、獲取錄音信息等相關(guān)操作技巧,需要的朋友可以參考下2018-01-01Android 中 requestWindowFeature()的應(yīng)用
本文主要介紹 Android requestWindowFeature()方法,這里對(duì) requestWindowFeature()方法進(jìn)行詳解,對(duì)應(yīng)用程序窗體顯示狀態(tài)的操作有進(jìn)一步了解,希望能幫助有需要的小伙伴2016-07-07Android實(shí)現(xiàn)歡迎界面停留3秒效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)歡迎界面停留3秒效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-02-02