[Code step]
1. Preparing for two ImageButton required *. ico such as:
Wrote
play.ico: ImageButton-play of the static (not pressed) effects
play_down.ico: ImageButton-play of the press effect
pause.ico: ImageButton-pause of the static (not pressed) effects
pause_down.ico: ImageButton-pause in pressing the effect of
2. To build the layout there are two ImageButton
(I am using "(" in place of "<" due to restriction.)
****************************************
(?xml version="1.0" encoding="utf-8"?> (LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" > (ImageButton android:id="@+id/play" android:layout_width="wrap_content" android:layout_height="wrap_content" /> (ImageButton android:id="@+id/pause" android:layout_width="wrap_content" android:layout_height="wrap_content" /> (/LinearLayout>
****************************************
3. Respectively, the definition of the name of easy reference View
****************************************
public void initialView(){
play = (ImageButton) findViewById(R.id.play);
pause = (ImageButton) findViewById(R.id.pause);
}
****************************************
4. For the ImageButton play, pause specify the default background
****************************************
public void specifyBackground(){
play.setBackgroundResource(R.drawable.play);
pause.setBackgroundResource(R.drawable.pause);
} ****************************************
5. To ImageButton play, pause Registration onTouch () events and in accordance with its ID and state the specific background image specified
****************************************
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.play:
if(event.getAction() == MotionEvent.ACTION_DOWN){
v.setBackgroundResource(R.drawable.play_down);
}
else if(event.getAction() == MotionEvent.ACTION_UP){
v.setBackgroundResource(R.drawable.play);
}
break;
case R.id.pause:
if(event.getAction() == MotionEvent.ACTION_DOWN){
v.setBackgroundResource(R.drawable.pause_down);
}
else if(event.getAction() == MotionEvent.ACTION_UP){
v.setBackgroundResource(R.drawable.pause);
}
break;
}
return false;
}
};
play.setOnTouchListener(listener);
pause.setOnTouchListener(listener);
****************************************
6. All the procedures as follows:
****************************************
public class ButtonStyle3Usage extends Activity {
ImageButton play,pause;
OnTouchListener listener;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initialView();
specifyBackground();
listener = new OnTouchListener(){
@Override public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub switch(v.getId()){ case R.id.play: if(event.getAction() == MotionEvent.ACTION_DOWN){ v.setBackgroundResource(R.drawable.play_down); } else if(event.getAction() == MotionEvent.ACTION_UP){ v.setBackgroundResource(R.drawable.play); } break; case R.id.pause: if(event.getAction() == MotionEvent.ACTION_DOWN){ v.setBackgroundResource(R.drawable.pause_down); } else if(event.getAction() == MotionEvent.ACTION_UP){ v.setBackgroundResource(R.drawable.pause); } break; } return false; } }; play.setOnTouchListener(listener); pause.setOnTouchListener(listener); } public void initialView(){ play = (ImageButton) findViewById(R.id.play); pause = (ImageButton) findViewById(R.id.pause); } public void specifyBackground(){ play.setBackgroundResource(R.drawable.play); pause.setBackgroundResource(R.drawable.pause); } }****************************************7. Emulator to run Screenshot: Static
No comments:
Post a Comment