PS:素材和整个文件我都会放在最后请耐心看完~
简单看下效果:
页面布局代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:background="@color/black"
android:orientation="vertical"
tools:context=".MainActivity">
<ImageView
android:id="@+id/img_show"
android:layout_width="400dp"
android:layout_height="400dp"
android:layout_gravity="center"
android:layout_weight="1"
android:src="@mipmap/dzmy" />
<TextView
android:id="@+id/tc_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="000"
android:layout_weight="1"
android:layout_gravity="center_horizontal"
android:textColor="@color/white"
android:textSize="60sp" />
</LinearLayout>
主要实现代码:
package com.example.muyu2;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.media.MediaPlayer;
import android.media.SoundPool;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView touchTime;
int i = 0;
private ImageView imgShow;
private Button bt;
private Animation animation = null;
private TextView mBtnmusic;
private SoundPool soundPool;
private int soundID;
private MediaPlayer music;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
handler.sendEmptyMessage(0);
touchTime = (TextView) findViewById(R.id.tc_time);
intview();
}
private void intview() {
imgShow = findViewById(R.id.img_show);
animation = AnimationUtils.loadAnimation(this, R.anim.muyu);
imgShow.startAnimation(animation);
animation.setFillAfter(true);
imgShow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
intview();
PlayMusic(R.raw.split);//第二种方式
i++;
/* MyThread1 mt = new MyThread1();
Thread mt11 = new Thread(mt);
mt11.start();*/
}
});
}
/* private class MyThread1 implements Runnable {
@Override
public void run() {
}
}*/
@SuppressLint("NewApi")
private void initSound() {
soundPool = new SoundPool.Builder().build();
soundID = soundPool.load(this, R.raw.split, 1);
}
private void PlayMusic(int MusicId) {
music = MediaPlayer.create(this, MusicId);
music.start();
}
private void playsound() {
soundPool.play(
soundID,
0.1f, //左耳道音量【0~1】
0.5f, //右耳道音量【0~1】
0, //播放优先级【0表示最低优先级】
0, //循环模式【0表示循环一次,-1表示一直循环,其他表示数字+1表示当前数字对应的循环次数】
1 //播放速度【1是正常,范围从0~2】
);
}
Handler handler = new Handler() {
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
touchTime.setText(i + "");
if (i != i - 1) {
handler.sendEmptyMessageDelayed(0, 500);
}
}
};
}
这里需要注意的一个点就是修改界面UI的线程得写在主线程上
这里我们需要自己创建动画文件夹和音频文件夹来存放素材:
这里我们选择anim文件夹存放动画xml,raw文件夹存放音频;
muyu动画代码:
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="500"
android:fillAfter="true"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:interpolator="@android:anim/accelerate_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="0.95"
android:toYScale="0.95" />
把前面的代码copy完差不多就能实现功能的,素材下面自取,免费,觉得有帮助麻烦点个赞,谢谢!
迅雷云盘:链接:https://pan.xunlei.com/s/VNF9LOFpmGf6vsfpZR5TEQTQA1?pwd=a2st#
提取码:a2st
百度云盘:链接: https://pan.baidu.com/s/1d407_ZF6GldlIJ1vecJmIA?pwd=xyxf 提取码: xyxf