如何制作带有动画的滑块[Android]

时间:2018-11-02 14:15:57

标签: android-studio android-layout android-fragments kotlin lottie-android

我正在使用动画而不是图像来制作滑块,但是问题出在的目的不是使用一组“ InputStream”类型,文本和动画位于活动文件夹中。我还在使用可以帮助我观看视频的图书馆。

* SliderAdapter

package com.example.gonzalo.proyecto_android_2018.adapter

import android.content.Context
import android.support.v4.view.PagerAdapter
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.RelativeLayout
import com.example.gonzalo.proyecto_android_2018.R
import kotlinx.android.synthetic.main.slide_layout.view.*
import java.io.InputStream

class SliderAdapter(var contexto: Context):PagerAdapter()
{

private val inflador:LayoutInflater

init {
    inflador = LayoutInflater.from(contexto)
}

var slide_background:IntArray = intArrayOf(R.drawable.gradient_uno, R.drawable.gradient_dos, R.drawable.gradient_tres)
var slide_image:IntArray = intArrayOf(R.drawable.persona_saludando, R.drawable.ic_parada_de_autobus, R.drawable.ic_parada_de_autobus_2)
var slide_headings = arrayOf(R.string.Slide_Bienvenido,R.string.Slide_Ubicanos, R.string.Slide_Espera)
var slide_descriptions = arrayOf(R.string.Slider_description_uno, R.string.Slider_description_dos, R.string.Slider_description_tres)

var slide_videos: Array<InputStream> = arrayOf(contexto.assets.open("pollo.json"),contexto.assets.open("mapa.json"),contexto.assets.open("bus_move.json"))

override fun isViewFromObject(view: View, objeto: Any) //Return a Boolean
        = view === objeto as (RelativeLayout)

override fun getCount() //Return a Integer
        = slide_headings.size

override fun instantiateItem(container: ViewGroup, position: Int): Any {
    val view = inflador.inflate(R.layout.slide_layout, container, false)
    view.slide_relative.setBackgroundResource(slide_background[position])
    view.slide_imagen.setImageResource(slide_videos[position])
    view.slide_lbl1.setText(slide_headings[position])
    view.slide_lbl2.setText(slide_descriptions[position])

    if(position == 0){
        view.slide_point1.setTextColor(contexto.resources.getColor(R.color.secondaryTextColor))
        view.slide_point2.setTextColor(contexto.resources.getColor(R.color.primaryTextColor))
        view.slide_point3.setTextColor(contexto.resources.getColor(R.color.secondaryTextColor))
    }

    if(position == 1){
        view.slide_point1.setTextColor(contexto.resources.getColor(R.color.primaryTextColor))
        view.slide_point2.setTextColor(contexto.resources.getColor(R.color.secondaryTextColor))
        view.slide_point3.setTextColor(contexto.resources.getColor(R.color.secondaryTextColor))
    }

    if(position == 2){
        view.slide_point1.setTextColor(contexto.resources.getColor(R.color.secondaryTextColor))
        view.slide_point2.setTextColor(contexto.resources.getColor(R.color.secondaryTextColor))
        view.slide_point3.setTextColor(contexto.resources.getColor(R.color.primaryTextColor))
    }
    container.addView(view)
    return view
}

override fun destroyItem(container:ViewGroup, position: Int, objeto: Any) {
    container.removeView(objeto as (RelativeLayout))
}

}

* IntroActivity

package com.example.gonzalo.proyecto_android_2018.activities

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import com.example.gonzalo.proyecto_android_2018.R
import com.example.gonzalo.proyecto_android_2018.adapter.SliderAdapter
import kotlinx.android.synthetic.main.activity_intro.*

class IntroActivity : AppCompatActivity() {

private lateinit var slidera_adapter:SliderAdapter

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_intro)

    slidera_adapter = SliderAdapter(this)

    intro_viewpager.adapter = slidera_adapter

    boton_saltar.setOnClickListener({
        startActivity(Intent(this@IntroActivity, RegistrarActivity::class.java))
    })
}
}

* slide_layout.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"
    android:id="@+id/slide_relative"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.airbnb.lottie.LottieAnimationView
        android:id="@+id/slide_video"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="128dp"

        app:lottie_autoPlay="true"
        app:lottie_loop="true"
        app:lottie_imageAssetsFolder="assets"
        app:lottie_fileName="pollo.json"
        />

    <TextView
        android:id="@+id/slide_lbl1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_below="@id/slide_video"
        android:text="Hola"
        android:textStyle="bold"
        android:textColor="@android:color/white"
        android:layout_marginTop="16dp"
        android:textSize="24sp"
        />

    <TextView
        android:id="@+id/slide_lbl2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_below="@id/slide_lbl1"
        android:text="Descripcion"
        android:textColor="@android:color/white"
        android:textSize="12sp"
        android:layout_marginRight="10dp"
        android:layout_marginLeft="10dp"
        />

    <TextView
        android:id="@+id/slide_point1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="."
        android:textSize="70dp"
        android:textColor="@android:color/holo_red_light"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        />

    <TextView
        android:id="@+id/slide_point2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="."
        android:textSize="70dp"
        android:textColor="@android:color/holo_red_light"
        android:layout_alignParentBottom="true"
        android:layout_alignRight="@id/slide_point1"
        android:layout_marginRight="16dp"
        />

    <TextView
        android:id="@+id/slide_point3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="."
        android:textSize="70dp"
        android:textColor="@android:color/holo_red_light"
        android:layout_alignParentBottom="true"
        android:layout_alignLeft="@id/slide_point1"
        android:layout_marginLeft="16dp"
        />

</RelativeLayout>

* activity_intro.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=".activities.IntroActivity">

<android.support.v4.view.ViewPager
    android:id="@+id/intro_viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</android.support.v4.view.ViewPager>

<Button
    android:id="@+id/boton_saltar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/gradient_boton"
    android:layout_alignParentRight="true"
    android:layout_alignParentBottom="true"
    android:text="@string/boton_saltar"
    android:textColor="@android:color/white"
    android:layout_marginBottom="8dp"
    android:layout_marginRight="8dp"/>

 </RelativeLayout>

0 个答案:

没有答案