android脸萌3

来源:互联网 发布:中国软件行业100 编辑:程序博客网 时间:2024/05/02 13:28

      今天学习的是安卓事件监听。实现了点击图片之后 图片变化,声音。

 想发送文件 也不知道能不能发送。

重点:1  布局要布局好。<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
   
  
<Button 
    android:id="@+id/sound"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dip"
    android:layout_marginTop="10dip"
    android:background="@drawable/bt_homepage_sound_on"/>


<Button 
    
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_marginLeft="10dip"
    android:layout_marginTop="10dip"
    
    android:background="@drawable/feedback"/>


<RelativeLayout 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true">
    
    <ImageView
        android:id="@+id/logo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:background="@drawable/pic_startpage_logo"/>
    
    <LinearLayout 
        android:id="@+id/sex_11"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/logo"
        android:layout_marginTop="10dip"
        android:orientation="horizontal">
       
        <Button
            android:id="@+id/boy_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/bt_man_up"/>
        
        <Button
            android:id="@+id/girl_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/girl_bg"/>
        </LinearLayout>
       
    <Button android:id="@+id/gift"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/sex_11"
        android:layout_marginTop="10dip"
        android:background="@drawable/gift_bg"/>
    <Button 
        android:id="@+id/more"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/gift"
        android:layout_marginTop="10dip"
         android:background="@drawable/more_bg"
        />
      
        
   
</RelativeLayout>
2,声音部分  soundutil 很重要要写好。

package com.example.test.util;




import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
import com.example.*;
import com.example.test.R;
import com.example.test.R.raw;


public class SoundUnits {


private static SoundUnits instance;

private SoundPool soundPool;

private int boyResId,girlResId,succeedResId;

private SoundUnits(Context context){

soundPool = new SoundPool(10,AudioManager.STREAM_MUSIC,0);
//boy,girl,succeedde 音频资料
boyResId = soundPool.load(context,R.raw.boy, 1);
girlResId = soundPool.load(context, R.raw.girl, 1);
succeedResId = soundPool.load(context, R.raw.save_succeed, 1);


}

//Java语言的关键字,当它用来修饰一个方法或者一个代码块的时候,能够保证在同一时刻最多只有一个线程执行该段代码。
public static synchronized SoundUnits getInstance(Context context){
if(instance == null){
instance = new SoundUnits(context);

}
return instance;
}

public void playBoySound(){
soundPool.play(boyResId,1.0f,1.0f,5,0,1.0f);
}

public void playGirlSound(){
soundPool.play(girlResId, 1.0f, 1.0f, 5, 0, 1.0f);
}

public void playSucceedSound(){
soundPool.play(succeedResId, 1.0f, 1.0f, 5, 0, 1.0f);
}

public void release(){
soundPool.release();
soundPool = null;
instance = null;
}

}

3,事件监听 图片变化和声音变化

package com.example.test;


import com.example.test.util.SoundUnits;


import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.Toast;
//
public class MainActivity extends Activity {
    private Button soundBtn;
    private Button boybtn;
    private Button girlbtn;
    private boolean isSoundopen = true;
    private SoundUnits soundunits = null;
    
    protected void onCreate(Bundle savedInstanceState) {
   
   
        super.onCreate(savedInstanceState);
        //去掉窗口头表头
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //设置这个界面的layout
        setContentView(R.layout.activity_main);
        
        soundunits = SoundUnits.getInstance(MainActivity.this);
        initButton();
        }
        
        
        
        
        private void initButton(){
        soundBtn = (Button) findViewById(R.id.sound);
        boybtn = (Button) findViewById(R.id.boy_btn);
        girlbtn = (Button) findViewById(R.id.girl_btn);
        soundBtn.setOnClickListener(new View.OnClickListener(){
   


public void onClick(View v){
       
isSoundopen=!isSoundopen;
        if(isSoundopen){
        soundBtn.setBackgroundResource(R.drawable.bt_homepage_sound_on);
       
        }
        else{
        soundBtn.setBackgroundResource(R.drawable.bt_homepage_sound_off);
        }
        }
        });
        
        boybtn.setOnTouchListener(new OnTouchListener(){
       
        public boolean onTouch(View v,MotionEvent event){
       
        int action = event.getAction();
       
        switch(action){
       
        case MotionEvent.ACTION_DOWN:
       
        soundunits.playBoySound();
//         soundunits.playGirlSound();
       
        Toast.makeText(MainActivity.this, "按钮按下", 0).show();
       
        boybtn.setBackgroundResource(R.drawable.bt_man_down);
       
        break;
       
        case MotionEvent.ACTION_MOVE:
       
        break;
       
        case MotionEvent.ACTION_UP:
       
        Toast.makeText(MainActivity.this, "按钮抬起", 0).show();
       
        boybtn.setBackgroundResource(R.drawable.bt_man_up);
       
        break;
        }
        return false;
        }
        }
     );
girlbtn.setOnTouchListener(new OnTouchListener(){
       
        public boolean onTouch(View v,MotionEvent event){
       
        int action = event.getAction();
       
        switch(action){
       
        case MotionEvent.ACTION_DOWN:
       
       
        soundunits.playGirlSound();
       
        Toast.makeText(MainActivity.this, "按钮按下", 0).show();
       
        girlbtn.setBackgroundResource(R.drawable.bt_woman_down);
       
        break;
       
        case MotionEvent.ACTION_MOVE:
       
        break;
       
        case MotionEvent.ACTION_UP:
       
        Toast.makeText(MainActivity.this, "按钮抬起", 0).show();
       
        girlbtn.setBackgroundResource(R.drawable.bt_woman_up);
       
        break;
        }
        return false;
        }
        }
     );
  }
        
  
  private   class MyOnclickListener implements View.OnClickListener{
       
        public void onClick(View v){
       
        }
        }


}     
问题1,drawable中的boy_bg等等什么意思

问题2,点击声音的图片变化在哪



































    
    <!--  <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">


        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/sound"
            android:layout_alignParentLeft="true"
            android:src="@drawable/bt_homepage_sound_on"
           />
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/feedback"
            android:layout_alignParentRight="true"
            android:src="@drawable/feedback"
            />
    </RelativeLayout>
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        >
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="80dp">
            <ImageVie
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:src="@drawable/pic_startpage_logo"
                />
        </RelativeLayout>
     <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
         
     
            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                >
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
         
                    android:src="@drawable/bt_man_up"
                    android:id="@+id/boy_btn"/>
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/girl_bg"
                    android:id="@+id/bt_woman_up"
                    android:layout_toRightOf="@+id/boy_btn"/>


            </RelativeLayout>
            </RelativeLayout>
        
            <RelativeLayout 
               android:layout_width="match_parent"
               android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                >
                 <RelativeLayout 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                >
                <ImageButton 
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:src="@drawable/gift_bg"
                    android:id="@+id/zhuanshut"
                    android:layout_centerHorizontal="true"
                    android:background="#00000000"
                    android:layout_marginTop="50dip"
                    />
                <ImageButton 
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:src="@drawable/more_bg"
                    android:background="#00000000"
                    
                    android:id="@+id/more"
                     android:layout_centerHorizontal="true"
                    />
                
            </RelativeLayout>
            
   </RelativeLayout>
    </LinearLayout>


    --> 
     
</RelativeLayout>

0 1
原创粉丝点击