亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android開發(fā)手冊(cè)Button按鈕實(shí)現(xiàn)點(diǎn)擊音效

 更新時(shí)間:2022年06月11日 09:58:53   作者:芝麻粒兒  
這篇文章主要為大家介紹了Android開發(fā)手冊(cè)Button按鈕實(shí)現(xiàn)點(diǎn)擊音效示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

前言

大家玩游戲的時(shí)候都知道按鈕除了點(diǎn)擊效果還有點(diǎn)擊音效,噗~的一聲,就挺靈性的。

在Unity中實(shí)現(xiàn)很簡單,原生的使用Audio Soure,如果你用過FairyGUI甚至不用寫代碼,直接妥妥拽拽可視化就搞定了。

突然有一天小空也想著怎么在APP中實(shí)現(xiàn)呢?所以準(zhǔn)備封裝一個(gè)。

雖然這個(gè)功能在實(shí)際開發(fā)中并沒有多大卵用。

??實(shí)踐過程

好了,既然要實(shí)現(xiàn)點(diǎn)擊按鈕有音效,那么我們先準(zhǔn)備一個(gè)音效,放到【res-raw】文件夾下。

創(chuàng)建個(gè)布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <Button
        android:id="@+id/btnAudio"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="點(diǎn)擊音效" />
</LinearLayout>

創(chuàng)建Activity

public class TestActivity extends AppCompatActivity {
    private Button btnAudio;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test); 
        initView();
        btnAudio.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //播放音頻
                AudioBtnUtils btnUtils=new AudioBtnUtils(TestActivity.this);
            }
        });
    }
    private void initView() {
        btnAudio = (Button) findViewById(R.id.btnAudio);
    }
}

AudioBtnUtils.class

public class AudioBtnUtils {
    private SoundPool.Builder builder;
    private SoundPool soundpool;
    private int soundId;
    public AudioBtnUtils(Context context) {
        builder = new SoundPool.Builder();
        //AudioAttributes是一個(gè)封裝音頻各種屬性的方法
        AudioAttributes.Builder attrBuilder = new AudioAttributes.Builder();
        //設(shè)置音頻流的合適的屬性
        attrBuilder.setLegacyStreamType(AudioManager.STREAM_SYSTEM);
        soundpool = builder.build();
        soundId = soundpool.load(context, R.raw.audio_btn_click, 1);
        //是否加載完成的監(jiān)聽
        soundpool.setOnLoadCompleteListener((soundPool, sampleId, status) -> {
            //加載完畢后再播放
            soundpool.play(soundId, 1f, 1f, 0, 0, 1);
        });
    }
}

上面play方法共有6個(gè)參數(shù) play(int soundID, float leftVolume, float rightVolume, int priority, int loop, float rate)

  • 參數(shù)soundID:代表聲音文件id;
  • 參數(shù)leftVolume、rightVolume:指定左、右的音量:
  • 參數(shù)priority:指定播放聲音的優(yōu)先級(jí),數(shù)值越大,優(yōu)先級(jí)越高;
  • 參數(shù)loop:指定是否循環(huán),0:不循環(huán),-1:循環(huán),其他值表示要重復(fù)播放的次數(shù);
  • 參數(shù)rate:指定播放的比率,數(shù)值可從0到2, 1為正常比率。  

這只是基本應(yīng)用,有什么的更好的方法呢?

要么寫個(gè)單例,全局就有一個(gè),要么創(chuàng)建個(gè)自定義的按鈕里面寫邏輯,以后按鈕就用這個(gè)自定義的。

那么為什么不用MediaPlayer呢?

MediaPlayer:占用資源較高,不支持同時(shí)播放多個(gè)音頻。

SoundPool:可以同時(shí)播放多個(gè)短促的音頻,而且占用的資源較少。適合在程序中播放按鍵音,或者消息提示音等。

結(jié)果

做完之后我突然想起來,手機(jī)的設(shè)置里面默認(rèn)就有點(diǎn)擊聲音的設(shè)置啊。一般在【設(shè)置-聲音和震動(dòng)-觸摸互動(dòng)】。

所以,我弄了個(gè)寂寞??!

以上就是Android開發(fā)手冊(cè)Button按鈕實(shí)現(xiàn)點(diǎn)擊音效的詳細(xì)內(nèi)容,更多關(guān)于Android開發(fā)Button點(diǎn)擊音效的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論