瀏覽代碼

Add button action bar icon menu

pull/1/head
Nils 8 年之前
父節點
當前提交
ec15b82c25
共有 9 個檔案被更改,包括 94 行新增47 行删除
  1. +1
    -1
      app/src/main/AndroidManifest.xml
  2. +71
    -44
      app/src/main/java/de/mlte/soundboard/MainActivity.kt
  3. 二進制
      app/src/main/res/drawable-hdpi/ic_add_new.png
  4. 二進制
      app/src/main/res/drawable-mdpi/ic_add_new.png
  5. 二進制
      app/src/main/res/drawable-xhdpi/ic_add_new.png
  6. 二進制
      app/src/main/res/drawable-xxhdpi/ic_add_new.png
  7. +11
    -2
      app/src/main/res/layout/activity_main.xml
  8. +10
    -0
      app/src/main/res/menu/main.xml
  9. +1
    -0
      app/src/main/res/values/strings.xml

+ 1
- 1
app/src/main/AndroidManifest.xml 查看文件

@@ -8,7 +8,7 @@
android:label="@string/app_name" android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round" android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true" android:supportsRtl="true"
android:theme="@style/AppTheme">
android:theme="@style/Theme.AppCompat.Light.NoActionBar">
<activity android:name=".MainActivity"> <activity android:name=".MainActivity">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN" /> <action android:name="android.intent.action.MAIN" />


+ 71
- 44
app/src/main/java/de/mlte/soundboard/MainActivity.kt 查看文件

@@ -7,9 +7,11 @@ import android.media.MediaPlayer
import android.net.Uri import android.net.Uri
import android.os.Bundle import android.os.Bundle
import android.support.v7.app.AppCompatActivity import android.support.v7.app.AppCompatActivity
import android.view.LayoutInflater
import android.support.v7.widget.Toolbar
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.widget.GridLayout import android.widget.GridLayout
import android.widget.TextView
import java.io.BufferedInputStream import java.io.BufferedInputStream
import java.io.BufferedOutputStream import java.io.BufferedOutputStream


@@ -23,69 +25,94 @@ class MainActivity : AppCompatActivity() {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main) setContentView(R.layout.activity_main)


duplicateButton()
val myToolbar = findViewById<View>(R.id.my_toolbar) as Toolbar
setSupportActionBar(myToolbar)


spawnButtons()


loadPreferences() loadPreferences()


} }


private fun duplicateButton() {
val inflater = getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
val parent = findViewById<GridLayout>(R.id.grid_layout)
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
val inflater = menuInflater
inflater.inflate(R.menu.main, menu)
return true
}


parent.columnCount = 2
override fun onOptionsItemSelected(item: MenuItem?): Boolean {
if (item != null) {
when (item.itemId) {
R.id.action_add_new -> {
val button = SoundButton(this, 0,2)
addButton(button)
return true
}
}
}
return super.onOptionsItemSelected(item)
}


private fun spawnButtons() {
val parent = findViewById<GridLayout>(R.id.grid_layout)
parent.columnCount = 2
for (row in 0..1) { for (row in 0..1) {
for (col in 0..1) { for (col in 0..1) {
val soundButton = SoundButton(this, col, row) val soundButton = SoundButton(this, col, row)
buttons.add(soundButton)
parent.addView(soundButton)
addButton(soundButton)
} }
} }
}


buttons.forEachIndexed { index, soundButton ->
soundButton.btn.setOnClickListener {
if (playing) {
player?.let { mp ->
if (mp.isPlaying) {
mp.stop()
}
private fun addButton(soundButton: SoundButton) {
val index = buttons.size
buttons.add(soundButton)

val parent = findViewById<GridLayout>(R.id.grid_layout)
parent.addView(soundButton)

soundButton.btn.setOnClickListener {
if (playing) {
player?.let { mp ->
if (mp.isPlaying) {
mp.stop()
}
mp.reset()
mp.release()
}
for (button in buttons) {
button.objectAnimator.cancel()
button.progressBar.progress = 0
}
playing = false
} else {
val file = getFileStreamPath("audio" + index)
if (file.exists()) {
val mp = MediaPlayer.create(this, Uri.fromFile(file))
mp.setOnCompletionListener {
soundButton.progressBar.progress = 0
mp.reset() mp.reset()
mp.release() mp.release()
playing = false
} }
for (button in buttons) {
button.objectAnimator.cancel()
button.progressBar.progress = 0
}
playing = false
} else {
val file = getFileStreamPath("audio" + index)
if (file.exists()) {
val mp = MediaPlayer.create(this, Uri.fromFile(file))
mp.setOnCompletionListener {
soundButton.progressBar.progress = 0
mp.reset()
mp.release()
playing = false
}
mp.start()
player = mp
playing = true
mp.start()
player = mp
playing = true


soundButton.progressBar.progress = 0
soundButton.objectAnimator.setDuration(mp.duration.toLong()).start()
}
soundButton.progressBar.progress = 0
soundButton.objectAnimator.setDuration(mp.duration.toLong()).start()
} }
} }
}


soundButton.btn.setOnLongClickListener {
val intent = Intent(baseContext, EditActivity::class.java)
intent.putExtra("index", index)
intent.putExtra("caption", soundButton.btn.text)
startActivityForResult(intent, 1234)
soundButton.btn.setOnLongClickListener {
val intent = Intent(baseContext, EditActivity::class.java)
intent.putExtra("index", index)
intent.putExtra("caption", soundButton.btn.text)
startActivityForResult(intent, 1234)


true
}
true
} }
} }




二進制
app/src/main/res/drawable-hdpi/ic_add_new.png 查看文件

Before After
Width: 48  |  Height: 48  |  Size: 246 B

二進制
app/src/main/res/drawable-mdpi/ic_add_new.png 查看文件

Before After
Width: 32  |  Height: 32  |  Size: 176 B

二進制
app/src/main/res/drawable-xhdpi/ic_add_new.png 查看文件

Before After
Width: 64  |  Height: 64  |  Size: 220 B

二進制
app/src/main/res/drawable-xxhdpi/ic_add_new.png 查看文件

Before After
Width: 96  |  Height: 96  |  Size: 371 B

+ 11
- 2
app/src/main/res/layout/activity_main.xml 查看文件

@@ -6,12 +6,21 @@
android:layout_height="match_parent" android:layout_height="match_parent"
tools:context="de.mlte.soundboard.MainActivity"> tools:context="de.mlte.soundboard.MainActivity">


<android.support.v7.widget.Toolbar
android:id="@+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

<GridLayout <GridLayout
android:paddingTop="?attr/actionBarSize"
android:id="@+id/grid_layout" android:id="@+id/grid_layout"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:columnCount="1"
android:padding="1dp">
android:columnCount="1">


</GridLayout> </GridLayout>




+ 10
- 0
app/src/main/res/menu/main.xml 查看文件

@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_add_new"
android:icon="@drawable/ic_add_new"
android:title="@string/action_add_new"
app:showAsAction="ifRoom|withText"/>

</menu>

+ 1
- 0
app/src/main/res/values/strings.xml 查看文件

@@ -1,3 +1,4 @@
<resources> <resources>
<string name="app_name">Soundboard</string> <string name="app_name">Soundboard</string>
<string name="action_add_new">Add new</string>
</resources> </resources>

Loading…
取消
儲存