Created
May 21, 2016 12:25
-
-
Save ekosuhariyadi/f7904692cfe434199ba7d88b1907a710 to your computer and use it in GitHub Desktop.
kotlin ku
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.kubuskotak.gazo.ui | |
import android.content.pm.ActivityInfo | |
import android.database.DataSetObserver | |
import android.os.Bundle | |
import android.os.Parcelable | |
import android.support.v4.view.PagerAdapter | |
import android.support.v4.view.ViewPager | |
import android.view.View | |
import android.view.ViewGroup | |
import android.view.Window | |
import android.view.animation.Animation | |
import android.view.animation.AnimationUtils | |
import android.widget.ImageView | |
import com.kubuskotak.gazo.R | |
import com.kubuskotak.gazo.ui.base.activity.BaseActivity | |
import kotlinx.android.synthetic.main.intro_layout.* | |
import kotlinx.android.synthetic.main.intro_view_layout.view.* | |
import timber.log.Timber | |
/** | |
* Created by echo on 5/21/16. | |
*/ | |
class KIntroActivity : BaseActivity() { | |
private val TAG = javaClass.simpleName | |
private val viewPager: ViewPager by lazy { | |
intro_view_pager | |
} | |
private val topImage1: ImageView by lazy { | |
icon_image1 | |
} | |
private val topImage2: ImageView by lazy { | |
icon_image2 | |
} | |
private val bottomPages: ViewGroup by lazy { | |
bottom_pages | |
} | |
private var lastPage = 0 | |
private var justCreated = false | |
private var startPressed = false | |
private val icons by lazy { | |
arrayOf( | |
R.drawable.intro1, | |
R.drawable.intro2, | |
R.drawable.intro3, | |
R.drawable.intro4, | |
R.drawable.intro5, | |
R.drawable.intro6, | |
R.drawable.intro7 | |
) | |
} | |
private val titles by lazy { | |
arrayOf( | |
R.string.Page1Title, | |
R.string.Page2Title, | |
R.string.Page3Title, | |
R.string.Page4Title, | |
R.string.Page5Title, | |
R.string.Page6Title, | |
R.string.Page7Title | |
) | |
} | |
private val messages by lazy { | |
arrayOf( | |
R.string.Page1Message, | |
R.string.Page2Message, | |
R.string.Page3Message, | |
R.string.Page4Message, | |
R.string.Page5Message, | |
R.string.Page6Message, | |
R.string.Page7Message | |
) | |
} | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
requestWindowFeature(Window.FEATURE_NO_TITLE) | |
setContentView(R.layout.intro_layout) | |
requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT | |
val startMessagingButton = start_messaging_button | |
startMessagingButton.text = getString(R.string.StartMessaging).toUpperCase() | |
topImage2.visibility = View.GONE | |
with(viewPager) { | |
adapter = IntroAdapter() | |
pageMargin = 0 | |
offscreenPageLimit = 1 | |
addOnPageChangeListener(object : ViewPager.OnPageChangeListener { | |
override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) { | |
} | |
override fun onPageSelected(position: Int) { | |
} | |
override fun onPageScrollStateChanged(state: Int) { | |
if (state == ViewPager.SCROLL_STATE_IDLE || state == ViewPager.SCROLL_STATE_SETTLING) { | |
if (lastPage != currentItem) { | |
lastPage = currentItem | |
Timber.e(TAG, lastPage.toString()) | |
val (fadeOutImage, fadeInImage) = if (topImage1.visibility == View.VISIBLE) { | |
Pair(topImage1, topImage2) | |
} else { | |
Pair(topImage2, topImage1) | |
} | |
with(fadeInImage) { | |
bringToFront() | |
setImageResource(icons[lastPage]) | |
clearAnimation() | |
} | |
fadeOutImage.clearAnimation() | |
val outAnimation = AnimationUtils | |
.loadAnimation(this@KIntroActivity, R.anim.icon_anim_fade_out); | |
outAnimation.setAnimationListener(object : Animation.AnimationListener { | |
override fun onAnimationStart(animation: Animation?) { | |
} | |
override fun onAnimationEnd(animation: Animation?) { | |
fadeOutImage.visibility = View.GONE | |
} | |
override fun onAnimationRepeat(animation: Animation?) { | |
} | |
}) | |
val inAnimation = AnimationUtils. | |
loadAnimation(this@KIntroActivity, R.anim.icon_anim_fade_in) | |
inAnimation.setAnimationListener(object : Animation.AnimationListener { | |
override fun onAnimationStart(animation: Animation?) { | |
fadeInImage.visibility = View.VISIBLE | |
} | |
override fun onAnimationEnd(animation: Animation?) { | |
} | |
override fun onAnimationRepeat(animation: Animation?) { | |
} | |
}) | |
fadeOutImage.startAnimation(outAnimation) | |
fadeInImage.startAnimation(inAnimation) | |
} | |
} | |
} | |
}) | |
} | |
startMessagingButton.setOnClickListener { view -> | |
if (startPressed) { | |
} | |
} | |
justCreated = true | |
} | |
override fun onResume() { | |
super.onResume() | |
if (justCreated) { | |
viewPager.currentItem = 0 | |
lastPage = 0 | |
justCreated = false | |
} | |
} | |
override fun onPause() { | |
super.onPause() | |
} | |
inner class IntroAdapter : PagerAdapter() { | |
override fun getCount(): Int { | |
return 7 | |
} | |
override fun instantiateItem(container: ViewGroup, position: Int): Any? { | |
val view = View.inflate(container.context, R.layout.intro_view_layout, null) | |
container.addView(view, 0) | |
// problem di class dum, langsung pakai header_text tanpa view, jadinya run | |
view.header_text.text = getString(titles[position]) | |
view.message_text.text = getString(messages[position]) | |
return view | |
} | |
override fun destroyItem(container: ViewGroup, position: Int, obj: Any) { | |
container.removeView(obj as View) | |
} | |
override fun setPrimaryItem(container: ViewGroup, position: Int, obj: Any) { | |
super.setPrimaryItem(container, position, obj) | |
val count = bottomPages.childCount | |
for (index in 0 until count) { | |
val child = bottomPages.getChildAt(index) | |
child.setBackgroundColor( | |
if (index == position) 0xff2ca5e0.toInt() else 0xffbbbbbb.toInt()) | |
} | |
} | |
override fun isViewFromObject(view: View, obj: Any): Boolean { | |
return view == obj | |
} | |
override fun restoreState(state: Parcelable?, loader: ClassLoader?) { | |
} | |
override fun saveState(): Parcelable? { | |
return null | |
} | |
override fun unregisterDataSetObserver(observer: DataSetObserver?) { | |
observer?.let { super.unregisterDataSetObserver(observer) } | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment