Created
August 19, 2018 09:58
-
-
Save mreram/788937df6b83f2a6448b62b901fd680b to your computer and use it in GitHub Desktop.
CameraFragment with some gestures and animations
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 eram.ir.camerapager | |
import android.os.Bundle | |
import android.support.v4.app.Fragment | |
import kotlinx.android.synthetic.main.camera_fragment.* | |
import android.view.* | |
import android.widget.FrameLayout | |
import android.animation.ValueAnimator | |
import android.content.res.Resources | |
import android.view.animation.DecelerateInterpolator | |
import android.util.DisplayMetrics | |
import android.util.TypedValue | |
import com.otaliastudios.cameraview.Facing | |
class CameraFragment : Fragment() { | |
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { | |
return LayoutInflater.from(context).inflate(R.layout.camera_fragment, container, false) | |
} | |
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | |
super.onViewCreated(view, savedInstanceState) | |
val viewPager = view.parent as CViewPager | |
viewPager.setOnExitListener(object : CViewPager.ExitListener { | |
override fun onExit() { | |
(viewPager).enabledScroll = true | |
viewPager.setCurrentItem(1, true) | |
val margin = dpToPx(100f).toInt() | |
val parentWidth = parent.measuredWidth - margin | |
val widthAnimator = ValueAnimator.ofInt(cv.width, parentWidth) | |
widthAnimator.duration = 700 | |
widthAnimator.interpolator = DecelerateInterpolator() | |
widthAnimator.addUpdateListener { animation -> | |
cv.layoutParams.width = animation.animatedValue as Int | |
cv.layoutParams.height = FrameLayout.LayoutParams.WRAP_CONTENT | |
cv.requestLayout() | |
} | |
val scaleAnimator = ValueAnimator.ofFloat(viewPager.scaleX, 1f) | |
scaleAnimator.duration = 400 | |
scaleAnimator.interpolator = DecelerateInterpolator() | |
scaleAnimator.addUpdateListener { animation -> | |
viewPager.scaleX = animation.animatedValue as Float | |
viewPager.scaleY = animation.animatedValue as Float | |
} | |
widthAnimator.start() | |
scaleAnimator.start() | |
} | |
}) | |
camera.setOnTouchListener { _, event -> | |
if (event.action == MotionEvent.ACTION_UP) { | |
(viewPager).enabledScroll = false | |
viewPager.setCurrentItem(1, true) | |
val parentWidth = parent.measuredWidth | |
val widthAnimator = ValueAnimator.ofInt(cv.width, parentWidth) | |
widthAnimator.duration = 700 | |
widthAnimator.interpolator = DecelerateInterpolator() | |
widthAnimator.addUpdateListener { animation -> | |
cv.layoutParams.width = animation.animatedValue as Int | |
cv.layoutParams.height = animation.animatedValue as Int | |
cv.requestLayout() | |
} | |
val scaleAnimator = ValueAnimator.ofFloat(viewPager.scaleX, 1.5f) | |
scaleAnimator.duration = 400 | |
scaleAnimator.interpolator = DecelerateInterpolator() | |
scaleAnimator.addUpdateListener { animation -> | |
viewPager.scaleX = animation.animatedValue as Float | |
viewPager.scaleY = animation.animatedValue as Float | |
} | |
widthAnimator.start() | |
scaleAnimator.start() | |
true | |
} else | |
// cv.layoutParams= FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,FrameLayout.LayoutParams.MATCH_PARENT) | |
false | |
} | |
} | |
override fun onResume() { | |
super.onResume() | |
camera.facing = Facing.BACK | |
camera.start() | |
} | |
override fun onPause() { | |
camera.stop() | |
super.onPause() | |
} | |
override fun onDestroyView() { | |
super.onDestroyView() | |
camera.destroy() | |
} | |
fun dpToPx(dp: Float): Float { | |
return (dp * Resources.getSystem().getDisplayMetrics().density); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment