Code Tutorial #004

activity_main.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"
android:id="@+id/layout"
tools:context=".MainActivity">

<ImageView
android:id="@+id/disk_view"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true"
app:srcCompat="@drawable/disk" />

<ImageView
android:id="@+id/ground_view"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
app:srcCompat="@drawable/ground" />

</RelativeLayout>

 

MainActivity.java:

import androidx.appcompat.app.AppCompatActivity;

import android.animation.Animator;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.ViewPropertyAnimator;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.BounceInterpolator;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

ImageView mDisk, mGround;

MediaPlayer mp;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mGround = findViewById(R.id.ground_view);
mDisk = findViewById(R.id.disk_view);

mp = MediaPlayer.create(this, R.raw.fall);

mDisk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
animateFall();
}
});
}

private void animateFall() {
float newY = mGround.getY() - mDisk.getHeight()/2 - mGround.getHeight() - 10;

ViewPropertyAnimator animator = mDisk.animate()
.translationY(newY)
.setInterpolator(new AccelerateInterpolator())
.setInterpolator(new BounceInterpolator())
.setDuration(2000);

animator.setListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
mp.start();
}

@Override
public void onAnimationEnd(Animator animation) {
Toast.makeText(MainActivity.this, "It Ended", Toast.LENGTH_SHORT).show();
}

@Override
public void onAnimationCancel(Animator animation) {

}

@Override
public void onAnimationRepeat(Animator animation) {

}
});
animator.start();
}
}

2 comments

Mamibet adalah bandar judi slot pulsa terpercaya yang memiliki beragam jenis permaiann judi slot online yang menguntungkan bagi semua pemain judi slot Indonesia. Heather Ximenes Zoellick

Leave a Reply

Your email address will not be published. Required fields are marked *