Created
February 11, 2019 05:22
-
-
Save Trumeet/1ff459d2e0ee0173f70e2ef2d3a3c589 to your computer and use it in GitHub Desktop.
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 moe.yuuta.intro | |
import android.graphics.Color | |
import android.graphics.drawable.ColorDrawable | |
import android.os.Bundle | |
import android.view.View | |
import android.widget.Button | |
import android.widget.ImageButton | |
import androidx.appcompat.app.AppCompatActivity | |
import androidx.fragment.app.Fragment | |
import androidx.fragment.app.FragmentPagerAdapter | |
import androidx.viewpager.widget.ViewPager | |
import com.android.systemui.qr.PageIndicator | |
class IntroActivity : AppCompatActivity() { | |
companion object { | |
const val EXTRA_PAGES = "moe.yuuta.intro.IntroActivity.EXTRA_PAGES" | |
} | |
private lateinit var mViewPager: ViewPager | |
private lateinit var mPages: List<Page> | |
private lateinit var mPageARGBDiffs: List<ARGBDiff> | |
private lateinit var mSkipButton: Button | |
private lateinit var mNextButton: ImageButton | |
private lateinit var mDoneButton: Button | |
private lateinit var mIndicator: PageIndicator | |
private var mPageIndicatorPosition: Float = 0F | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
mPages = intent.getParcelableArrayListExtra(EXTRA_PAGES) | |
if (mPages.isEmpty()) { | |
finish() | |
return | |
} | |
setContentView(R.layout.activity_intro) | |
mViewPager = findViewById(R.id.intro_pager) | |
mSkipButton = findViewById(R.id.intro_button_skip) | |
mNextButton = findViewById(R.id.intro_button_next) | |
mDoneButton = findViewById(R.id.intro_button_done) | |
mIndicator = findViewById(R.id.intro_indicator) | |
mIndicator.setNumPages(mPages.size) | |
mIndicator.setLocation(mPageIndicatorPosition) | |
mViewPager.adapter = object : FragmentPagerAdapter(supportFragmentManager) { | |
override fun getItem(position: Int): Fragment = PageFragment.create(mPages.get(position)) | |
override fun getCount(): Int = mPages.size | |
} | |
mViewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener { | |
override fun onPageScrollStateChanged(state: Int) { | |
} | |
private var mLastScrollOffset: Float = 0F | |
private var mSetBackgroundCount: Int = 0 | |
override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) { | |
if (positionOffset > 0.0) { | |
var diff: ARGBDiff? = null | |
if (position + positionOffset == mPageIndicatorPosition) { | |
// Not moving, ignore it | |
} else if (position + positionOffset > mPageIndicatorPosition) { | |
// From right to left | |
if (position == mPages.size - 1) { | |
// It's the last page, skip it | |
} else { | |
diff = mPageARGBDiffs[position] | |
} | |
} else { | |
// From left to right | |
if (position == 0) { | |
// It's the first page, skip it | |
} else { | |
// We did not calculate the backward direction, but we can pick the previous one | |
// and reverse the values | |
val prevDiff = mPageARGBDiffs[position - 1] | |
diff = ARGBDiff(-prevDiff.alpha, -prevDiff.red, -prevDiff.green, -prevDiff.blue) | |
} | |
} | |
if (diff != null) { | |
val offset = positionOffset * 100 | |
diff = ARGBDiff(diff.alpha * offset, | |
diff.red * offset, | |
diff.green * offset, | |
diff.blue * offset) | |
// The final diff with absolute ARGB values | |
val currentPage: Page = mPages[position] | |
val currentColor = currentPage.backgroundColor | |
val finalDiff = ARGBDiff(Color.alpha(currentColor) + diff.alpha, | |
Color.red(currentColor) + diff.red, | |
Color.green(currentColor) + diff.green, | |
Color.blue(currentColor) + diff.blue) | |
System.out.println("Final offset: $offset, DIFF: $diff, patched $finalDiff") | |
if (finalDiff.alpha >= 0 && finalDiff.red >= 0 && | |
finalDiff.green >= 0 && finalDiff.blue >= 0) { | |
// Build and apply the final color | |
if (mSetBackgroundCount == 5) { | |
mSetBackgroundCount = 0 | |
} | |
mViewPager.setBackgroundColor(Color.argb(finalDiff.alpha.toInt(), | |
finalDiff.red.toInt(), | |
finalDiff.green.toInt(), | |
finalDiff.blue.toInt())) | |
mSetBackgroundCount++ | |
} | |
} | |
} | |
mPageIndicatorPosition = position + positionOffset | |
mLastScrollOffset = positionOffset | |
mIndicator.setLocation(mPageIndicatorPosition) | |
} | |
override fun onPageSelected(position: Int) { | |
onSelected(position) | |
} | |
}) | |
mNextButton.setOnClickListener { mViewPager.setCurrentItem(mViewPager.currentItem + 1, true) } | |
val argbDiffs = mutableListOf<ARGBDiff>() | |
// Calculate ARGBDiff | |
for (i in mPages.indices) { | |
// The last one shouldn't be calculated | |
if (i == mPages.size - 1) continue | |
val page = mPages[i] | |
val nextPage = mPages[i + 1] | |
val alphaA = Color.alpha(page.backgroundColor) | |
val redA = Color.red(page.backgroundColor) | |
val greenA = Color.green(page.backgroundColor) | |
val blueA = Color.blue(page.backgroundColor) | |
val alphaB = Color.alpha(nextPage.backgroundColor) | |
val redB = Color.red(nextPage.backgroundColor) | |
val greenB = Color.green(nextPage.backgroundColor) | |
val blueB = Color.blue(nextPage.backgroundColor) | |
System.out.println("Alpha A = $alphaA, Alpha B = $alphaB") | |
val alphaDiff = alphaB - alphaA | |
val redDiff = redB - redA | |
val greenDiff = greenB - greenA | |
val blueDiff = blueB - blueA | |
val diff = ARGBDiff(alphaDiff.toFloat() / 100, redDiff.toFloat() / 100, greenDiff.toFloat() / 100, blueDiff.toFloat() / 100) | |
argbDiffs.add(diff) | |
} | |
System.out.println(argbDiffs) | |
mPageARGBDiffs = argbDiffs.toList() | |
onSelected(0) | |
mIndicator.setLocation(mPageIndicatorPosition) | |
} | |
private fun onSelected(position: Int) { | |
refreshButtons(position) | |
mViewPager.setBackgroundColor(mPages[position].backgroundColor) | |
} | |
private fun refreshButtons(position: Int) { | |
if (position == 0) { | |
mSkipButton.visibility = View.VISIBLE | |
mDoneButton.visibility = View.INVISIBLE | |
mNextButton.visibility = View.VISIBLE | |
} else if (position == mPages.size - 1) { | |
mSkipButton.visibility = View.GONE | |
mDoneButton.visibility = View.VISIBLE | |
mNextButton.visibility = View.GONE | |
} else { | |
mSkipButton.visibility = View.GONE | |
mDoneButton.visibility = View.GONE | |
mNextButton.visibility = View.VISIBLE | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment