首页 > 技术文章 > 68.Vehicles Sound

L1nus 2017-09-21 23:34 原文

In this turorial , i will help you creat an Vehicles Sound app With some different.

-Use Fragment -Use Toolbar

First, We need write a simple program by C# to get all file name on folder,we will get all file name on drawable folder which have images for our app;

Second, Creat new project and import all asset (drawable+raw files) You can download all assets by link at description

Third,make image Asset , choose Icon Type Action Bar and Tab Icons,import two icon for two menu button.rewrite the name of the action icon 

Fourth ,copy drawable an raw into res folder

Fifth ,in main_activity.java,Creat a String array ,and Copy all files name to String array,

Six,Creat UI for our App, in activity_main.xml ,we add <android.support.v7.widget.Toolbar  

android:layout_height="?attr/actionBarSize"

android:background = "?attr/colorPrimary"

android:elevation = "4dp"

android:theme = "@style/ThemeOverlay.AppCompat.ActionBar"

app:popupTheme = "@style/ThemeOverlay.AppCompat.Light"

>element,

Theven , add ViewPager

Eight ,add TextView ,style = "@style/Widget.Appcompat.Button.Colored"

Nine , Creat viewpager_item.xml for our item in viewpager with root element LinearLayout ,and add ImageView element.

ten , Creat new fragment.xml and copy all from activity_main.xml , Remove all View in "activity_main.xml" and add fragment to it

  <fragment

Eleven , Creat fragment class and extend Frament,  in activity_main element , add <fragment  tools :layout = "@layout/fragment_vehicles"  

  android:name = "com.bodor.fragment"

Twleve , Creat ViewPagerAdapter for custom our ViewPager adapter , 

  public class

Thirteen ,This event will change name of vehicles base on our String array, we just replace"_" by "space" because i have set all name in correct format.

Fourteen, We will need "index" variable for some function, so we need change value for it by "position".  and check if player is available we will stip and release it.

Fifteen , Default ViewPager doesn't have animation, we will custom parallax Animation for it,

    pager.setPageTransformer(false,new ParallaxPageTransformer);

    Creat ParllaxPageTransformer extends View.page.pagerTransformer{

      public static class Prar

    }

Sixteen ,Now we will add support menu  to our app, Oh ,I forget to creat menu ,no proplem ,we will creat it right now 

  right click on drawable folder , and select New resource directory , chose Resource type menu, in menu folder , creat menu.xml file ,

  <menu>

    <item 

      id:    icon:  title: showAsAction

    <item id icon title showAsAction

Seventeen,We will get sound id from raw folder by this function.chek player if null ,just creat it .and if player is available ,just release it and set new instance .Button "voice" will play "voice_<vehicles name>" so we just copy all function of button "sound"

  @override onOptionItemSelected(MenuItem item){

 

  }

Eighteen ,oh error because our theme is "Theme.Appcompat.Light.DarkActionBar",with this theme we can't add Toolbar,just changer Theme

推荐阅读