鱼C论坛

 找回密码
 立即注册
查看: 2752|回复: 1

[技术交流] kotlin for android 之物体的移动

[复制链接]
发表于 2017-12-8 08:20:13 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
        继续昨天的项目,修改fragment_blank_fragment2.xml文件:
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2.     xmlns:tools="http://schemas.android.com/tools"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:orientation="vertical"
  6.     tools:context="com.example.xinwei.fragmentkotlin.BlankFragment2">

  7.     <TextView
  8.         android:id="@+id/idtextb"
  9.         android:layout_width="300dp"
  10.         android:layout_height="70dp"
  11.         android:background="#ff0000"
  12.         android:layout_marginTop="150dp"
  13.         android:layout_gravity="center_horizontal"
  14.         android:text="@string/hello_blank_fragment" />

  15.     <Button
  16.         android:id="@+id/idbuttonc"
  17.         android:layout_width="wrap_content"
  18.         android:layout_height="wrap_content"
  19.         android:layout_gravity="center_horizontal"
  20.         android:layout_marginTop="150dp"
  21.         android:text="旋转" />
  22.     <Button
  23.         android:id="@+id/idbuttond"
  24.         android:layout_width="wrap_content"
  25.         android:layout_height="wrap_content"
  26.         android:layout_gravity="center_horizontal"
  27.         android:text="移动" />

  28. </LinearLayout>
复制代码

        这里就是多加了个按钮,然后修改BlankFragment2.kt:
  1. package com.example.xinwei.fragmentkotlin

  2. import android.app.Fragment
  3. import android.os.Bundle
  4. import android.view.LayoutInflater
  5. import android.view.View
  6. import android.view.ViewGroup
  7. import android.view.animation.Animation
  8. import android.view.animation.RotateAnimation
  9. import android.view.animation.TranslateAnimation
  10. import android.widget.Button
  11. import android.widget.TextView

  12. class BlankFragment2 : Fragment() {
  13.     var view1:View?=null
  14.     var button1:Button?=null
  15.     var button2:Button?=null
  16.     var text:TextView?=null
  17.     override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
  18.                               savedInstanceState: Bundle?): View? {
  19.         view1=inflater.inflate(R.layout.fragment_blank_fragment2, container, false)
  20.         button1=view1?.findViewById<Button>(R.id.idbuttonc)
  21.         button2=view1?.findViewById<Button>(R.id.idbuttond)
  22.         text=view1?.findViewById<TextView>(R.id.idtextb)
  23.         rotateRect()
  24.         translateRect()
  25.         return view1
  26.     }

  27.     private fun rotateRect() {
  28.         var rotate=RotateAnimation(0.0f,360.0f,350.0f,35.0f)
  29.         rotate.duration=3000
  30.         button1?.setOnClickListener {
  31.             text?.startAnimation(rotate)
  32.         }
  33.     }
  34.     private fun translateRect() {
  35.         var translate= TranslateAnimation(0.0f,0.0f,0.0f,350.0f)
  36.         translate.duration=3000
  37.         translate.repeatCount=Animation.INFINITE
  38.         button2?.setOnClickListener {
  39.             text?.startAnimation(translate)
  40.         }
  41.     }

  42. }
复制代码

        我写的 translateRect() 这个方法就是让矩形向下移动,进入app后先点按钮进入第二个fragment然后点底下移动的按钮,就可以了,效果图:
jdfw.gif

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-12-8 12:41:07 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-20 03:11

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表