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使用BroadcastReceiver實(shí)現(xiàn)手機(jī)開(kāi)機(jī)之后顯示畫(huà)面的功能
這篇文章主要介紹了Android使用BroadcastReceiver實(shí)現(xiàn)手機(jī)開(kāi)機(jī)之后顯示畫(huà)面的功能,結(jié)合實(shí)例形式分析了BroadcastReceiver的具體使用技巧及實(shí)現(xiàn)開(kāi)機(jī)畫(huà)面的相關(guān)功能代碼,需要的朋友可以參考下2016-01-01
Android實(shí)現(xiàn)Window彈窗效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)Window彈窗效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10
Android利用Flutter?path繪制粽子的示例代碼
端午將至,作為中華民族的非常重要的傳統(tǒng)節(jié)日,粽子那是必不可少的。今天跟隨本篇文章用Flutter?path畫(huà)一個(gè)會(huì)科普節(jié)日的的粽子吧2022-05-05
Android AS為xutils添加依賴過(guò)程圖解
這篇文章主要介紹了Android AS為xutils添加依賴過(guò)程圖解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11
Android開(kāi)發(fā)實(shí)現(xiàn)圖片圓角的方法
這篇文章主要介紹了Android開(kāi)發(fā)實(shí)現(xiàn)圖片圓角的方法,涉及Android針對(duì)圖形圖像的相關(guān)操作技巧,需要的朋友可以參考下2016-10-10
Android Presentation雙屏異顯開(kāi)發(fā)流程詳細(xì)講解
最近開(kāi)發(fā)的一個(gè)項(xiàng)目,有兩個(gè)屏幕,需要將第二個(gè)頁(yè)面投屏到副屏上,這就需要用到Android的雙屏異顯(Presentation)技術(shù)了,研究了一下,這里做下筆記2023-01-01
Android開(kāi)發(fā)實(shí)現(xiàn)布局中為控件添加選擇器的方法
這篇文章主要介紹了Android開(kāi)發(fā)實(shí)現(xiàn)布局中為控件添加選擇器的方法,涉及Android開(kāi)發(fā)中布局設(shè)置的相關(guān)操作技巧,需要的朋友可以參考下
基于Android實(shí)現(xiàn)ListView圓角效果
這篇文章主要為大家詳細(xì)介紹了基于Android實(shí)現(xiàn)ListView圓角效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-06-06
Android 實(shí)現(xiàn)微信長(zhǎng)按菜單 -FloatMenu
在日常開(kāi)發(fā)中,長(zhǎng)按某個(gè)view出現(xiàn)個(gè)菜單是很常見(jiàn)的需求,下面小編給大家?guī)?lái)了Android 實(shí)現(xiàn)微信長(zhǎng)按菜單 -FloatMenu的實(shí)現(xiàn)思路及具體實(shí)現(xiàn)代碼,感興趣的朋友跟隨腳本之家小編一起看看吧2018-07-07

