Android繪制平移動畫的示例代碼
1、具體操作步驟
創(chuàng)建ImageView對象
創(chuàng)建ObjectAnimator對象
通過ofFloat方法實現(xiàn)平移
2、具體實施
創(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對象
1.第一位參數(shù)是需要移動的圖片
2.第二位參數(shù)是設(shè)置在什么軸移動,例子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();
}
}
}
一個最簡單的平移動畫就實現(xiàn)了
到此這篇關(guān)于Android繪制平移動畫的示例代碼的文章就介紹到這了,更多相關(guān)Android平移動畫內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android使用BroadcastReceiver實現(xiàn)手機開機之后顯示畫面的功能
這篇文章主要介紹了Android使用BroadcastReceiver實現(xiàn)手機開機之后顯示畫面的功能,結(jié)合實例形式分析了BroadcastReceiver的具體使用技巧及實現(xiàn)開機畫面的相關(guān)功能代碼,需要的朋友可以參考下2016-01-01
Android利用Flutter?path繪制粽子的示例代碼
端午將至,作為中華民族的非常重要的傳統(tǒng)節(jié)日,粽子那是必不可少的。今天跟隨本篇文章用Flutter?path畫一個會科普節(jié)日的的粽子吧2022-05-05
Android Presentation雙屏異顯開發(fā)流程詳細講解
最近開發(fā)的一個項目,有兩個屏幕,需要將第二個頁面投屏到副屏上,這就需要用到Android的雙屏異顯(Presentation)技術(shù)了,研究了一下,這里做下筆記2023-01-01
Android開發(fā)實現(xiàn)布局中為控件添加選擇器的方法
這篇文章主要介紹了Android開發(fā)實現(xiàn)布局中為控件添加選擇器的方法,涉及Android開發(fā)中布局設(shè)置的相關(guān)操作技巧,需要的朋友可以參考下2017-10-10
Android 實現(xiàn)微信長按菜單 -FloatMenu
在日常開發(fā)中,長按某個view出現(xiàn)個菜單是很常見的需求,下面小編給大家?guī)砹薃ndroid 實現(xiàn)微信長按菜單 -FloatMenu的實現(xiàn)思路及具體實現(xiàn)代碼,感興趣的朋友跟隨腳本之家小編一起看看吧2018-07-07

