最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android繪制平移動畫的示例代碼

 更新時間:2022年01月06日 10:16:46   作者:吃橘子的季節(jié)呢  
這篇文章主要展示的是利用Android繪制平移動畫的示例代碼,文中的實現(xiàn)步驟講解詳細,對我們學(xué)習(xí)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)手機開機之后顯示畫面的功能

    這篇文章主要介紹了Android使用BroadcastReceiver實現(xiàn)手機開機之后顯示畫面的功能,結(jié)合實例形式分析了BroadcastReceiver的具體使用技巧及實現(xiàn)開機畫面的相關(guān)功能代碼,需要的朋友可以參考下
    2016-01-01
  • Android實現(xiàn)Window彈窗效果

    Android實現(xiàn)Window彈窗效果

    這篇文章主要為大家詳細介紹了Android實現(xiàn)Window彈窗效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-10-10
  • Android利用Flutter?path繪制粽子的示例代碼

    Android利用Flutter?path繪制粽子的示例代碼

    端午將至,作為中華民族的非常重要的傳統(tǒng)節(jié)日,粽子那是必不可少的。今天跟隨本篇文章用Flutter?path畫一個會科普節(jié)日的的粽子吧
    2022-05-05
  • Android AS為xutils添加依賴過程圖解

    Android AS為xutils添加依賴過程圖解

    這篇文章主要介紹了Android AS為xutils添加依賴過程圖解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-11-11
  • Android開發(fā)實現(xiàn)圖片圓角的方法

    Android開發(fā)實現(xiàn)圖片圓角的方法

    這篇文章主要介紹了Android開發(fā)實現(xiàn)圖片圓角的方法,涉及Android針對圖形圖像的相關(guān)操作技巧,需要的朋友可以參考下
    2016-10-10
  • Android Presentation雙屏異顯開發(fā)流程詳細講解

    Android Presentation雙屏異顯開發(fā)流程詳細講解

    最近開發(fā)的一個項目,有兩個屏幕,需要將第二個頁面投屏到副屏上,這就需要用到Android的雙屏異顯(Presentation)技術(shù)了,研究了一下,這里做下筆記
    2023-01-01
  • Android開發(fā)實現(xiàn)布局中為控件添加選擇器的方法

    Android開發(fā)實現(xiàn)布局中為控件添加選擇器的方法

    這篇文章主要介紹了Android開發(fā)實現(xiàn)布局中為控件添加選擇器的方法,涉及Android開發(fā)中布局設(shè)置的相關(guān)操作技巧,需要的朋友可以參考下
    2017-10-10
  • Android中Dialog的使用詳解

    Android中Dialog的使用詳解

    Dialog(對話框)是Android中常用的UI組件,用于臨時顯示重要信息或獲取用戶輸入,本文給大家介紹Android中Dialog的使用,感興趣的朋友一起看看吧
    2025-04-04
  • 基于Android實現(xiàn)ListView圓角效果

    基于Android實現(xiàn)ListView圓角效果

    這篇文章主要為大家詳細介紹了基于Android實現(xiàn)ListView圓角效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-06-06
  • Android 實現(xiàn)微信長按菜單 -FloatMenu

    Android 實現(xiàn)微信長按菜單 -FloatMenu

    在日常開發(fā)中,長按某個view出現(xiàn)個菜單是很常見的需求,下面小編給大家?guī)砹薃ndroid 實現(xiàn)微信長按菜單 -FloatMenu的實現(xiàn)思路及具體實現(xiàn)代碼,感興趣的朋友跟隨腳本之家小編一起看看吧
    2018-07-07

最新評論

桂阳县| 吐鲁番市| 太湖县| 茂名市| 项城市| 高阳县| 武胜县| 申扎县| 白山市| 宁德市| 韶山市| 湾仔区| 漳浦县| 额敏县| 荣昌县| 潜山县| 松阳县| 公安县| 西畴县| 雅江县| 大方县| 桐城市| 扎鲁特旗| 宾阳县| 彩票| 阳泉市| 白沙| 营山县| 宽甸| 澄迈县| 平安县| 民乐县| 清徐县| 长泰县| 博爱县| 房产| 钟祥市| 九台市| 公主岭市| 扶风县| 呼和浩特市|