Android Open Source - androidui Main Activity






From Project

Back to project page androidui.

License

The source code is released under:

MIT License

If you think the Android project androidui listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package course.examples.modernartpiano;
//from  w w  w. j  a va  2  s.c om
import android.app.Activity;
import android.content.Context;
import android.media.AudioAttributes;
import android.media.AudioManager;
import android.media.SoundPool;
import android.media.SoundPool.OnLoadCompleteListener;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class MainActivity extends Activity {

    private AudioManager mAudioManager;
    private SoundPool mSoundPool;
    private final int[] mSoundIDs = new int[MAX_SOUNDS];
    private float mPitch = 1.0f;
    private boolean mCanPlay;

    private MenuItem mRaisePitchMenuItem;
    private MenuItem mLowerPitchMenuItem;

    private static final int MAX_SOUNDS = 5;

    @SuppressWarnings("unused")
    private static final String TAG = "MainActivity";
    private static int sNumLoaded;

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

        mAudioManager = (AudioManager) this
                .getSystemService(Context.AUDIO_SERVICE);

        setupVolumeControl();
        initViews();

    }

    private void setupVolumeControl() {

        final SeekBar volumeControl = (SeekBar) findViewById(R.id.volume_bar);
        volumeControl.setMax(mAudioManager
                .getStreamMaxVolume(AudioManager.STREAM_MUSIC));

        volumeControl.setProgress(mAudioManager
                .getStreamVolume(AudioManager.STREAM_MUSIC));

        volumeControl
                .setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

                    int volume = volumeControl.getProgress();

                    public void onProgressChanged(SeekBar seekBar,
                                                  int progress, boolean fromUser) {
                        volume = progress;
                    }

                    public void onStartTrackingTouch(SeekBar seekBar) {
                        // Not implemented
                    }

                    public void onStopTrackingTouch(SeekBar seekBar) {
                        mAudioManager.setStreamVolume(
                                AudioManager.STREAM_MUSIC, volume, 0);
                    }
                });

    }

    @Override
    protected void onResume() {

        mSoundPool = new SoundPool.Builder()
                .setAudioAttributes(new AudioAttributes.Builder()
                        .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
                        .setUsage(AudioAttributes.USAGE_MEDIA).build())
                .setMaxStreams(MAX_SOUNDS).build();

        mSoundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {

            @Override
            public void onLoadComplete(SoundPool soundPool, int sampleId,
                                       int status) {

                Log.i(TAG, "Loaded:" + sampleId + " with status:" + status);

                sNumLoaded++;
                if (sNumLoaded == MAX_SOUNDS) {
                    mCanPlay = true;
                }
            }

        });

        sNumLoaded = 0;

        mSoundIDs[0] = mSoundPool.load(this, R.raw.c, 1);
        mSoundIDs[1] = mSoundPool.load(this, R.raw.d, 1);
        mSoundIDs[2] = mSoundPool.load(this, R.raw.e, 1);
        mSoundIDs[3] = mSoundPool.load(this, R.raw.applause, 1);
        mSoundIDs[4] = mSoundPool.load(this, R.raw.g, 1);

        super.onResume();
    }

    @Override
    protected void onPause() {
        mSoundPool.release();
        mSoundPool = null;
        mCanPlay = false;
        super.onPause();
    }

    private void initViews() {
        ImageButton mIndigo = (ImageButton) findViewById(R.id.indigo);
        mIndigo.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                performClick();
            }

            private void performClick() {
                if (mCanPlay) {
                    playSoundNumber(0);
                }
            }
        });

        ImageButton mPink = (ImageButton) findViewById(R.id.pink);
        mPink.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                performClick();
            }

            private void performClick() {
                if (mCanPlay) {
                    playSoundNumber(1);
                }
            }
        });

        ImageButton mWhite1 = (ImageButton) findViewById(R.id.white1);
        mWhite1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                performClick();
            }

            private void performClick() {
                if (mCanPlay) {
                    playSoundNumber(2);
                }
            }
        });

        ImageButton mWhite2 = (ImageButton) findViewById(R.id.white2);
        mWhite2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                performClick();
            }

            private void performClick() {
                if (mCanPlay) {
                    playSoundNumber(3);
                }
            }
        });

        ImageButton mWhite3 = (ImageButton) findViewById(R.id.white3);
        mWhite3.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                performClick();
            }

            private void performClick() {
                if (mCanPlay) {
                    playSoundNumber(4);
                }
            }
        });
    }

    void playSoundNumber(int number) {
        mSoundPool.play(mSoundIDs[number], 1, 1, 1, 0, mPitch);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_layout, menu);

        mRaisePitchMenuItem = menu.getItem(0);
        mLowerPitchMenuItem = menu.getItem(1);

        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (mCanPlay) {
            switch (item.getItemId()) {
                case R.id.raise_pitch:
                    if (mPitch > 0.9) {
                        mPitch = 1.7f;
                        enableRaisePitchMenuItem(false);
                    } else {
                        enableLowerPitchMenuItem(true);
                        mPitch += 0.7;
                    }
                    break;
                case R.id.lower_pitch:
                    if (mPitch < 1.1) {
                        mPitch = 0.3f;
                        enableLowerPitchMenuItem(false);
                    } else {
                        enableRaisePitchMenuItem(true);
                        mPitch -= 0.7;
                    }
                    break;
            }
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void enableLowerPitchMenuItem(boolean b) {
        if (null != mLowerPitchMenuItem) {
            mLowerPitchMenuItem.setEnabled(b);
        }
    }

    private void enableRaisePitchMenuItem(boolean b) {
        if (null != mRaisePitchMenuItem) {
            mRaisePitchMenuItem.setEnabled(b);
        }
    }
}




Java Source Code List

com.example.aporter.helloandroidwithimageview.HelloAndroidWithImageViewActivity.java
course.examples.Fragments.DynamicLayout.QuoteViewerActivity.java
course.examples.Fragments.DynamicLayout.QuotesFragment.java
course.examples.Fragments.DynamicLayout.TitlesFragment.java
course.examples.Notification.StatusBarWithCustomView.NotificationSpecialActivity.java
course.examples.Notification.StatusBarWithCustomView.NotificationStatusBarWithExpandedViewActivity.java
course.examples.Notification.Toast.NotificationToastActivity.java
course.examples.Notification.ToastWithCustomView.NotificationToastActivity.java
course.examples.UI.AlertDialog.AlertDialogActivity.java
course.examples.UI.AutoComplete.AutoCompleteActivity.java
course.examples.UI.Button.ButtonActivity.java
course.examples.UI.CheckBox.CheckBoxActivity.java
course.examples.UI.GridView.GridLayoutActivity.java
course.examples.UI.GridView.ImageAdapter.java
course.examples.UI.GridView.ImageViewActivity.java
course.examples.UI.LinearLayout.LinearLayoutActivity.java
course.examples.UI.ListLayout.ListViewActivity.java
course.examples.UI.ListLayout.ListViewAdapter.java
course.examples.UI.MapView.GoogleMapActivity.java
course.examples.UI.RadioGroup.RadioGroupActivity.java
course.examples.UI.RatingsBar.RatingsBarActivity.java
course.examples.UI.RecyclerView.MyRecyclerViewAdapter.java
course.examples.UI.RecyclerView.RecyclerViewActivity.java
course.examples.UI.RelativeLayout.RelativeLayoutActivity.java
course.examples.UI.Spinner.SpinnerActivity.java
course.examples.UI.TabLayout.GridFragment.java
course.examples.UI.TabLayout.ImageAdapter.java
course.examples.UI.TabLayout.ImageViewActivity.java
course.examples.UI.TabLayout.TabLayoutActivity.java
course.examples.UI.TableLayout.TableLayoutActivity.java
course.examples.UI.ViewPager.GalleryWithViewPagerActivity.java
course.examples.UI.ViewPager.ImageAdapter.java
course.examples.UI.ViewPager.ImageHolderFragment.java
course.examples.UI.WebView.WebViewActivity.java
course.examples.UI.datepicker.DatePickerFragmentActivity.java
course.examples.UI.timepicker.TimePickerFragmentActivity.java
course.examples.UI.togglebutton.ToggleButtonActivity.java
course.examples.colorpalettewithnavdrawer.ApplicationTest.java
course.examples.colorpalettewithnavdrawer.DisplayColorActivity.java
course.examples.colorpalettewithnavdrawer.DisplaySingleColorActivity.java
course.examples.colorpalettewithnavdrawer.PaletteNameAdapter.java
course.examples.colorpalettewithswipe.ApplicationTest.java
course.examples.colorpalettewithswipe.DisplayColorPaletteActivity.java
course.examples.colorpalettewithswipe.DisplaySingleColorActivity.java
course.examples.colorpalettewithswipe.PaletteAdapter.java
course.examples.fragments.StaticLayout.QuoteViewerActivity.java
course.examples.fragments.StaticLayout.QuotesFragment.java
course.examples.fragments.StaticLayout.TitlesFragment.java
course.examples.fragments.staticconfiglayout.QuoteViewerActivity.java
course.examples.fragments.staticconfiglayout.QuotesFragment.java
course.examples.fragments.staticconfiglayout.TitlesFragment.java
course.examples.helloandroidwithlogin.ApplicationTest.java
course.examples.helloandroidwithlogin.HelloAndroidWithImageViewActivity.java
course.examples.helloandroidwithlogin.LoginActivity.java
course.examples.modernartpiano.MainActivity.java
course.examples.modernartui.MainActivity.java
course.examples.notification.StatusBar.NotificationStatusBarActivity.java
course.examples.notification.StatusBar.NotificationSubActivity.java
course.examples.quoteviewer.QuoteListActivity.java
course.examples.quoteviewer.TitlesListActivity.java
course.examples.ui.fragmentactionbar.QuoteFragment.java
course.examples.ui.fragmentactionbar.QuoteViewerActivity.java
course.examples.ui.fragmentactionbar.TitlesFragment.java
course.examples.ui.helloworldwithmenus.HelloAndroidWithMenuActivity.java
course.labs.multipane.MainActivity.java
course.labs.multipane.QuoteFragment.java
course.labs.multipane.TitlesFragment.java
course.labs.placebadges.MockLocationProvider.java
course.labs.placebadges.PlaceDownloaderTask.java
course.labs.placebadges.PlaceRecord.java
course.labs.placebadges.PlaceViewActivity.java
course.labs.placebadges.PlaceViewAdapter.java
course.labs.placebadges.PlaceViewDetailActivity.java
course.labs.placebadges_prel.MockLocationProvider.java
course.labs.placebadges_prel.PlaceDownloaderTask.java
course.labs.placebadges_prel.PlaceRecord.java
course.labs.placebadges_prel.PlaceViewActivity.java
course.labs.placebadges_prel.PlaceViewAdapter.java
course.labs.placebadges_prel.PlaceViewDetailActivity.java
examples.course.basiccolorpalette.ApplicationTest.java
examples.course.basiccolorpalette.DisplayColorActivity.java
examples.course.basiccolorpalette.DisplayColorNames.java
examples.course.basiccolorpalette.DisplaySingleColorActivity.java
examples.course.basiccolorpalette.PaletteAdapter.java
examples.course.basiccolorpaletteupnav.ApplicationTest.java
examples.course.basiccolorpaletteupnav.DisplayColorActivity.java
examples.course.basiccolorpaletteupnav.DisplayColorNamesActivity.java
examples.course.basiccolorpaletteupnav.DisplaySingleColorActivity.java
examples.course.basiccolorpaletteupnav.PaletteAdapter.java
examples.course.ticker.TickerDisplayActivity.java
examples.course.uicardview.ApplicationTest.java
examples.course.uicardview.CardViewActivity.java