我有一个小丑按喇叭应用程序。当用户按下占据整个屏幕的图像按钮时,它会播放持续半秒钟的媒体播放器声音。
我希望它可以播放说HONK,HONK !!!和HONK !!!!!!!当该计数达到(%10 == 0)和/或(%100 == 0)语句时,随机地保留一个计数变量,该计数变量为惊人的HONKS致敬。
我添加了一个触摸按钮和一个onbuttonlistener,而且我的计数无处可去。我还希望图像按钮占据整个屏幕并在图像按钮顶部显示文本视图......
发布我的MAIN和我的XML:
public class MainActivity extends Activity implements OnTouchListener{
public void amazinghonks(View view)
{
MediaPlayer mp = MediaPlayer.create(this, R.raw.bikehorn);
mp.start();
}
private SoundPool soundPool;
private int soundID;
private int soundID2;
boolean loaded = false;
boolean songloaded = false;
int honkcount = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
View view = findViewById(R.id.toptext);
view.setOnTouchListener(this);
// Set the hardware buttons to control the music
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
// Load the sound
soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool soundPool, int sampleId,
int status) {
loaded = true;
}
});
soundID = soundPool.load(this, R.raw.bikehorn, 1);
soundID2 = soundPool.load(this, R.raw.clown, 1);
Toast.makeText(MainActivity.this,
"HONK!!!", Toast.LENGTH_LONG).show();
}
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
// Getting the user sound settings
AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
float actualVolume = (float) audioManager
.getStreamVolume(AudioManager.STREAM_MUSIC);
float maxVolume = (float) audioManager
.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
float volume = actualVolume / maxVolume;
// Is the sound loaded already?
if (loaded && (honkcount < 3)) {
honkcount++;
soundPool.play(soundID, volume, volume, 1, 0, 1f);
Log.e("Test", "Played sound");
}
if (songloaded && (honkcount > 10)) {
honkcount = 0;
soundPool.play(soundID2, volume, volume, 1, 0, 1f);
Log.e("Test", "Played sound");
}
}
return false;
}
public void addListenerOnButton() {
ImageButton imageButton = (ImageButton) findViewById(R.id.imageButton1);
imageButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
double randomnumber = Math.random()*101;
//WILL HAVE THREE KINDS OF HONKS!
if(randomnumber < 33)
{
Toast.makeText(MainActivity.this,
"HONK!", Toast.LENGTH_SHORT).show();
}
if((randomnumber < 66)&&(randomnumber > 33))
{
Toast.makeText(MainActivity.this,
"HONK!!!", Toast.LENGTH_SHORT).show();
}
if((randomnumber < 101)&&(randomnumber > 66))
{
Toast.makeText(MainActivity.this,
"HONK!!!!!!!!!!!!!!!!!!!!", Toast.LENGTH_SHORT).show();
}
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/toptext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#303030"
android:text="@string/toptext"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#33B5E5" />
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="0.62"
android:contentDescription="@string/honktext"
android:onClick="amazinghonks"
android:src="@drawable/big_clown" />
</LinearLayout>
答案 0 :(得分:1)
据我所知,你永远不会调用你的addListenerOnButton()
方法,因此它永远不会被执行。尝试使用onCreate()
方法调用它。
让ImageButton
占据整个视图,顶部有TextView
(如更大的z-index,对吧?)您想要使用RelativeLayout
代替LinearLayout
。并尝试将ImageButton
的高度设置为“fill_parent”而不是0dip。高度为0将有效隐藏它,使其无法点击。