最近在做一个在线音乐播放器,调用豆瓣的API在做播放音乐那个界面的时候,我想做个播放模式切换:循环模式-->列表模式--->随机模式-->单曲模式.我打算只用一个ImageButton,默认是循环模式,然后点击时候就会从循环切换到列表,在点击一次从列列表钱换到随机。切换时候不同模式显示不同的背景图片,但是现在好像是背景图片叠加在一起了。大家帮我看看下,谢谢了!
本来是这样的:
点击一下就这样了:
我把布局文件和代码贴一下:
main_play.xml(播放界面)
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
     android:background="#FFF5F5F5">
    
        <!-- 歌手头像区-->
    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/PlayMusic_content"
        android:src="@drawable/music"
        android:layout_gravity="center"
     />
    
    <!-- 歌曲信息区-->
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top">
        
        <ImageButton
            android:id="@+id/PlayMusic_return_to_"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/back"
            android:layout_marginTop="10dp"/>
        <ImageButton
            android:id="@+id/PlayMusic_playmode"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/playmode_sequence"
            android:layout_alignParentRight="true"
            android:layout_marginTop="10dp"/>
            
        
        <TextView 
            android:id="@+id/PlayMusic_music_name"
            android:text="可惜不是你"
            android:textStyle="bold"
            android:textSize="18dp"
            android:layout_centerHorizontal="true"
            android:layout_alignParentTop="true"
            android:layout_marginTop="15dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        
        <TextView 
            android:id="@+id/PlayMusic_music_singer"
            android:text="梁静茹"
            android:textColor="#FF0000"
            android:textSize="15dp"
            android:layout_centerHorizontal="true"
            android:layout_below="@id/PlayMusic_music_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>        <TextView
            android:id="@+id/PlayMusic_time_begin"
            android:layout_alignParentLeft="true"
            android:layout_below="@id/PlayMusic_music_singer"
            android:text="@string/timeBegin"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:textSize="20dp"
            android:textColor="#FFFFFF"/>
        
         <TextView
            android:id="@+id/PlayMusic_time_finish"
            android:layout_alignParentRight="true"
            android:layout_below="@id/PlayMusic_music_singer"
            android:text="@string/timeFinish"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_alignTop="@id/PlayMusic_time_begin"
            android:textSize="20dp"
            android:textColor="#FFFFFF"/>
         
         <!-- 播放时间键 -->
         <SeekBar
             android:id="@+id/PlayMusic_time_seekBar"
             android:layout_toLeftOf="@id/PlayMusic_time_finish"
             android:layout_toRightOf="@id/PlayMusic_time_begin"
             android:layout_below="@id/PlayMusic_music_singer"
             android:layout_height="wrap_content"
             android:layout_width="wrap_content"
             android:layout_marginLeft="20dp"
             android:layout_marginRight="20dp"/>    </RelativeLayout>
       
    <!-- 播放控制区 -->    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:background="@drawable/bg_control_bar">
         
         <!-- 音量 -->
         <SeekBar
            android:id="@+id/PlayMusic_volume_seekBar"
            android:layout_width="280dp"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_marginLeft="20dp" 
            android:layout_marginRight="20dp"/>
          
         <!-- 上一首 -->
        <ImageButton
            android:id="@+id/PlayMusic_pre_button"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="30dp"
            android:src="@drawable/pre_button_selector"
            android:layout_height="wrap_content"
            android:layout_width="65dp"
            android:layout_above="@id/PlayMusic_volume_seekBar"/>
        
        <!-- 下一首 -->
        <ImageButton
            android:id="@+id/PlayMusic_next_button"
            android:layout_alignParentRight="true"
            android:layout_marginRight="30dp"
            android:src="@drawable/next_button_selector"
            android:layout_height="wrap_content"
            android:layout_width="65dp"
            android:layout_above="@id/PlayMusic_volume_seekBar"
            android:layout_alignTop="@id/PlayMusic_pre_button"/>
        
        <!-- 播放/暂停 -->
        <ImageButton
            android:id="@+id/PlayMusic_play_button"
            android:layout_width="70dp"
            android:layout_height="wrap_content"
            android:layout_above="@id/PlayMusic_volume_seekBar"
            android:layout_alignTop="@id/PlayMusic_pre_button"
            android:layout_toLeftOf="@id/PlayMusic_next_button"
            android:layout_toRightOf="@+id/latestButton"
            android:layout_marginLeft="30dp"
            android:layout_marginRight="30dp"/>
        <!--  android:src="@drawable/play_button_selector"--> 
    </RelativeLayout>
    
</FrameLayout>切换模式改变
下面的ConstantsUtils.Playmode.PLAYMODE_SEQUENCE是我专门用了一个类来存放这些常数。
case R.id.PlayMusic_playmode:
            /**
             * 循环模式-->列表循环
             * 列表循环--->单曲循环
             * 单曲循环-->随机模式
             * 随机模式--->循环模式
             * */
            if(loop_flag==ConstantsUtils.Playmode.PLAYMODE_SEQUENCE){
                loop_flag=ConstantsUtils.Playmode.PLAYMODE_LIST_CIRCLE;
                Log.e(TAG, "列表循环");
                playmode_button.setBackgroundResource(R.drawable.playmode_list_circle);
            }
            else if(loop_flag==ConstantsUtils.Playmode.PLAYMODE_LIST_CIRCLE){
                loop_flag=ConstantsUtils.Playmode.PLAYMODE_SINGLE_CIRCLE;
                Log.e(TAG, "单曲循环");
                playmode_button.setBackgroundResource(R.drawable.playmode_single_circle);
           }
            else if(loop_flag==ConstantsUtils.Playmode.PLAYMODE_SINGLE_CIRCLE){
                loop_flag=ConstantsUtils.Playmode.PLAYMODE_RANDOM;
                Log.e(TAG, "随机模式");
                playmode_button.setBackgroundResource(R.drawable.playmode_random);
                
           }
            else{
                loop_flag=ConstantsUtils.Playmode.PLAYMODE_SEQUENCE;
                Log.e(TAG, "循环模式");
                playmode_button.setBackgroundResource(R.drawable.playmode_sequence);
            }
            break;