鱼C论坛

 找回密码
 立即注册
查看: 2283|回复: 0

[技术交流] kotlin for android 之组合动画

[复制链接]
发表于 2017-12-9 10:34:10 | 显示全部楼层 |阅读模式

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

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

x
        因为地方不够了按原来的方法再新建个fragment,然后修改activity_main.xml文件:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"
  4.     xmlns:tools="http://schemas.android.com/tools"
  5.     android:layout_width="match_parent"
  6.     android:layout_height="match_parent"
  7.     android:orientation="vertical">

  8.     <LinearLayout
  9.         android:id="@+id/idlayout1"
  10.         android:layout_width="match_parent"
  11.         android:layout_height="0dp"
  12.         android:layout_weight="8"
  13.         android:orientation="vertical"/>

  14.     <LinearLayout
  15.         android:layout_width="match_parent"
  16.         android:layout_height="0dp"
  17.         android:layout_weight="1"
  18.         android:orientation="horizontal">

  19.         <Button
  20.             android:id="@+id/idbutton1"
  21.             android:layout_width="0dp"
  22.             android:layout_height="wrap_content"
  23.             android:layout_weight="1"
  24.             android:text="button1" />

  25.         <Button
  26.             android:id="@+id/idbutton2"
  27.             android:layout_width="0dp"
  28.             android:layout_height="wrap_content"
  29.             android:layout_weight="1"
  30.             android:text="button2" />

  31.         <Button
  32.             android:id="@+id/idbutton3"
  33.             android:layout_width="0dp"
  34.             android:layout_height="wrap_content"
  35.             android:layout_weight="1"
  36.             android:text="button3" />

  37.     </LinearLayout>

  38. </LinearLayout>
复制代码

        修改MainActivity.kt文件:
  1. package com.example.xinwei.fragmentkotlin

  2. import android.app.FragmentTransaction
  3. import android.support.v7.app.AppCompatActivity
  4. import android.os.Bundle
  5. import android.view.View
  6. import kotlinx.android.synthetic.main.activity_main.*

  7. class MainActivity : AppCompatActivity(),View.OnClickListener {

  8.     override fun onClick(p0: View?) {
  9.         when(p0?.id){
  10.             idbutton1.id->{
  11.                 var manager=fragmentManager
  12.                 var transaction=manager.beginTransaction()
  13.                 transaction?.replace(R.id.idlayout1,BlankFragment())
  14.                 transaction?.commit()
  15.             }
  16.             idbutton2.id->{
  17.                 var manager=fragmentManager
  18.                 var transaction=manager.beginTransaction()
  19.                 transaction?.replace(R.id.idlayout1,BlankFragment2())
  20.                 transaction?.commit()
  21.             }
  22.             idbutton3.id->{
  23.                 var manager=fragmentManager
  24.                 var transaction=manager.beginTransaction()
  25.                 transaction?.replace(R.id.idlayout1,BlankFragment3())
  26.                 transaction?.commit()
  27.             }
  28.         }
  29.     }

  30.     override fun onCreate(savedInstanceState: Bundle?) {
  31.         super.onCreate(savedInstanceState)
  32.         setContentView(R.layout.activity_main)
  33.         idbutton1.setOnClickListener(this)
  34.         idbutton2.setOnClickListener(this)
  35.         idbutton3.setOnClickListener(this)
  36.         var manager=fragmentManager
  37.         var transaction=manager.beginTransaction()
  38.         transaction?.replace(R.id.idlayout1,BlankFragment())
  39.         transaction?.commit()
  40.     }
  41. }
复制代码

        修改fragment_blank_fragment3.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.BlankFragment3">

  7.         <TextView
  8.             android:id="@+id/idtext1"
  9.             android:layout_width="match_parent"
  10.             android:layout_height="300dp"
  11.             android:gravity="center"
  12.             android:layout_gravity="center_horizontal"
  13.             android:textColor="#00ff00"
  14.             android:textSize="50sp"
  15.             android:layout_marginBottom="100dp"
  16.             android:text="我 爱 鱼 C" />

  17.     <Button
  18.         android:id="@+id/idbuttonall"
  19.         android:layout_width="wrap_content"
  20.         android:layout_height="wrap_content"
  21.         android:text="组合动画"
  22.         android:layout_gravity="center_horizontal"/>

  23. </LinearLayout>
复制代码

        修改BlankFragment3.kt文件:
  1. package com.example.xinwei.fragmentkotlin


  2. import android.os.Bundle
  3. import android.app.Fragment
  4. import android.view.LayoutInflater
  5. import android.view.View
  6. import android.view.ViewGroup
  7. import android.view.animation.*
  8. import android.widget.Button
  9. import android.widget.TextView


  10. /**
  11. * A simple [Fragment] subclass.
  12. */
  13. class BlankFragment3 : Fragment() {

  14.     var view1:View?=null
  15.     var button: Button?=null
  16.     var text1: TextView?=null
  17.     var text2: TextView?=null
  18.     var text3: TextView?=null
  19.     var text4: TextView?=null

  20.     override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
  21.                               savedInstanceState: Bundle?): View? {

  22.         view1=inflater.inflate(R.layout.fragment_blank_fragment3, container, false)
  23.         text1=view1?.findViewById<TextView>(R.id.idtext1)
  24.         button=view1?.findViewById<Button>(R.id.idbuttonall)
  25.         text1?.visibility=View.INVISIBLE
  26.         groupAnimation()
  27.         return view1
  28.     }

  29.     private fun groupAnimation() {
  30.         button?.setOnClickListener {
  31.             var set=AnimationSet(false)
  32.             var scale= ScaleAnimation(0.1f,1.0f,0.1f,1.0f,0.5f,0.5f)
  33.             scale.duration=3000
  34.             var animation= AlphaAnimation(0.0f,1.0f)
  35.             animation.duration=3000
  36.             var rotate= RotateAnimation(0.0f,360.0f,400.0f,400.0f)
  37.             rotate.duration=3000
  38.             var translate= TranslateAnimation(-100.0f,0.0f,-100.0f,0.0f)
  39.             translate.duration=3000
  40.             set.addAnimation(scale)
  41.             set.addAnimation(animation)
  42.             set.addAnimation(rotate)
  43.             set.addAnimation(translate)
  44.             text1?.visibility=View.VISIBLE
  45.             text1?.startAnimation(set)
  46.         }
  47.     }

  48. }
复制代码

        这里的groupAnimation() 方法就是组合动画代码,效果图为:
jdfw.gif

本帖被以下淘专辑推荐:

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-3-29 06:19

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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