|
| 1 | +package ta.com.workshop3 |
| 2 | + |
| 3 | +import android.animation.AnimatorSet |
| 4 | +import android.animation.ObjectAnimator |
| 5 | +import android.os.Bundle |
| 6 | +import android.support.v7.app.AppCompatActivity |
| 7 | +import android.support.v7.widget.CardView |
| 8 | +import android.util.Log |
| 9 | +import android.view.MotionEvent |
| 10 | +import android.widget.Toast |
| 11 | + |
| 12 | +class MainActivity : AppCompatActivity() { |
| 13 | + |
| 14 | + override fun onCreate(savedInstanceState: Bundle?) { |
| 15 | + super.onCreate(savedInstanceState) |
| 16 | + setContentView(R.layout.activity_main) |
| 17 | + val cardView = findViewById(R.id.card_view) as CardView |
| 18 | + |
| 19 | + cardView.setOnClickListener({ _ -> |
| 20 | + Toast.makeText(this, "Clicked", Toast.LENGTH_SHORT).show() |
| 21 | + }) |
| 22 | + |
| 23 | + cardView.setOnTouchListener { v, event -> |
| 24 | + Log.d("ANIMATE", event.toString()) |
| 25 | + when (event.action) { |
| 26 | + MotionEvent.ACTION_DOWN -> { |
| 27 | + v.animate().cancel() |
| 28 | + v.animate().scaleY(0.96f).scaleX(0.96f).setDuration(200).start() |
| 29 | + val img = v.findViewById(R.id.imageBackground) |
| 30 | + img.animate().scaleY(1.1f).scaleX(1.1f).alpha(0.7f).setDuration(200).start() |
| 31 | + |
| 32 | + false |
| 33 | + } |
| 34 | + MotionEvent.ACTION_UP -> { |
| 35 | + |
| 36 | + val xBigScale = ObjectAnimator.ofFloat(v, "scaleX", 1.03f) |
| 37 | + xBigScale.setDuration(160).repeatCount = 0 |
| 38 | + |
| 39 | + val yBigScale = ObjectAnimator.ofFloat(v, "scaleY", 1.03f) |
| 40 | + yBigScale.setDuration(160).repeatCount = 0 |
| 41 | + |
| 42 | + val xSmallScale = ObjectAnimator.ofFloat(v, "scaleX", 0.985f) |
| 43 | + xSmallScale.setDuration(140).repeatCount = 0 |
| 44 | + |
| 45 | + val ySmallScale = ObjectAnimator.ofFloat(v, "scaleY", 0.985f) |
| 46 | + ySmallScale.setDuration(140).repeatCount = 0 |
| 47 | + |
| 48 | + val xNormalScale = ObjectAnimator.ofFloat(v, "scaleX", 1f) |
| 49 | + xNormalScale.setDuration(70).repeatCount = 0 |
| 50 | + |
| 51 | + val yNormalScale = ObjectAnimator.ofFloat(v, "scaleY", 1f) |
| 52 | + yNormalScale.setDuration(70).repeatCount = 0 |
| 53 | + |
| 54 | + val animateSet = AnimatorSet() |
| 55 | + animateSet.play(xBigScale).with(yBigScale) |
| 56 | + animateSet.play(xSmallScale).after(xBigScale) |
| 57 | + animateSet.play(ySmallScale).after(yBigScale) |
| 58 | + animateSet.play(xNormalScale).after(xSmallScale) |
| 59 | + animateSet.play(yNormalScale).after(ySmallScale) |
| 60 | + animateSet.start() |
| 61 | + |
| 62 | + val img = v.findViewById(R.id.imageBackground) |
| 63 | + |
| 64 | + img.animate().scaleY(1f).scaleX(1f).alpha(1f).setDuration(370).start() |
| 65 | + |
| 66 | + false |
| 67 | + } |
| 68 | + |
| 69 | + MotionEvent.ACTION_CANCEL -> { |
| 70 | + val img = v.findViewById(R.id.imageBackground) |
| 71 | + v.animate().scaleY(1f).scaleX(1f).setDuration(200).start() |
| 72 | + img.animate().scaleY(1f).scaleX(1f).alpha(1f).setDuration(370).start() |
| 73 | + |
| 74 | + true |
| 75 | + } |
| 76 | + |
| 77 | + else -> true |
| 78 | + } |
| 79 | + |
| 80 | + |
| 81 | + |
| 82 | + } |
| 83 | + } |
| 84 | +} |
0 commit comments