欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android繪制平移動(dòng)畫(huà)的示例代碼

 更新時(shí)間:2022年01月06日 10:16:46   作者:吃橘子的季節(jié)呢  
這篇文章主要展示的是利用Android繪制平移動(dòng)畫(huà)的示例代碼,文中的實(shí)現(xiàn)步驟講解詳細(xì),對(duì)我們學(xué)習(xí)Android有一定的幫助,感興趣的可以試一試

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)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Android下拉列表框Spinner使用方法詳解

    Android下拉列表框Spinner使用方法詳解

    這篇文章主要為大家詳細(xì)介紹了Android下拉列表框Spinner的基本使用,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-06-06
  • Android 超詳細(xì)SplashScreen入門(mé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-03
  • Android實(shí)現(xiàn)音樂(lè)視頻播放

    Android實(shí)現(xiàn)音樂(lè)視頻播放

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)音樂(lè)視頻播放,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-05-05
  • Android實(shí)現(xiàn)簡(jiǎn)單購(gòu)物車(chē)

    Android實(shí)現(xiàn)簡(jiǎn)單購(gòu)物車(chē)

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)簡(jiǎn)單購(gòu)物車(chē),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • Android實(shí)現(xiàn)手勢(shì)滑動(dòng)(左滑和右滑)

    Android實(shí)現(xiàn)手勢(shì)滑動(dòng)(左滑和右滑)

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)手勢(shì)滑動(dòng),左滑和右滑效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-07-07
  • Android編程錄音工具類(lèi)RecorderUtil定義與用法示例

    Android編程錄音工具類(lèi)RecorderUtil定義與用法示例

    這篇文章主要介紹了Android編程錄音工具類(lèi)RecorderUtil定義與用法,結(jié)合實(shí)例形式分析了Android錄音工具類(lèi)實(shí)現(xiàn)開(kāi)始錄音、停止錄音、取消錄音、獲取錄音信息等相關(guān)操作技巧,需要的朋友可以參考下
    2018-01-01
  • Android 中 requestWindowFeature()的應(yīng)用

    Android 中 requestWindowFeature()的應(yīng)用

    本文主要介紹 Android requestWindowFeature()方法,這里對(duì) requestWindowFeature()方法進(jìn)行詳解,對(duì)應(yīng)用程序窗體顯示狀態(tài)的操作有進(jìn)一步了解,希望能幫助有需要的小伙伴
    2016-07-07
  • Android 常用log 關(guān)鍵字

    Android 常用log 關(guān)鍵字

    本文給大家分享android常用的log關(guān)鍵字,感興趣的朋友一起看看吧
    2017-09-09
  • Android獲取應(yīng)用程序大小的方法

    Android獲取應(yīng)用程序大小的方法

    這篇文章主要介紹了Android獲取應(yīng)用程序大小的方法,有需要的朋友可以參考一下
    2014-01-01
  • Android實(shí)現(xiàn)歡迎界面停留3秒效果

    Android實(shí)現(xiàn)歡迎界面停留3秒效果

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)歡迎界面停留3秒效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-02-02

最新評(píng)論