iis服务器助手广告广告
返回顶部
首页 > 资讯 > 移动开发 >Android绘制平移动画的示例代码
  • 501
分享到

Android绘制平移动画的示例代码

2024-04-02 19:04:59 501人浏览 泡泡鱼
摘要

目录1、具体操作步骤2、具体实施创建ImageView创建ObjectAnimator对象3、具体实例activity_main.xmlMainActivity.java1、具体操作

1、具体操作步骤

创建ImageView对象

创建ObjectAnimator对象

通过ofFloat方法实现平移

2、具体实施

创建ImageView

<ImageView
        Android:id="@+id/car"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:src="@mipmap/car"/>

创建ObjectAnimator对象

1.第一位参数是需要移动的图片

2.第二位参数是设置在什么轴移动,例子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();
        }
    }
}

一个最简单的平移动画就实现了

到此这篇关于Android绘制平移动画的示例代码的文章就介绍到这了,更多相关Android平移动画内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: Android绘制平移动画的示例代码

本文链接: https://www.lsjlt.com/news/161716.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作