출처 : http://godofcode.tistory.com/305

public
class MyCustomButton extends ImageButton {

final static int DEFAULT_IMAGE = R.drawable.default_button;

final static int DEFAULT_CLICK_IMAGE = R.drawable.default_button_click;

Drawable buttonImage; // 버튼 기본 이미지

Drawable clickImage; // 클릭 모션 이미지

public MyCustomButton(Context context, AttributeSet attrs) {

super(context, attrs);

buttonImage = getResources().getDrawable(DEFAULT_IMAGE);

clickImage = getResources().getDrawable(DEFAULT_CLICK_IMAGE);

setImageDrawable(buttonImage);

this.setOnTouchListener(new OnTouchListener(){

public boolean onTouch(View v, MotionEvent event) {

switch ( event.getAction() ){

case MotionEvent.ACTION_DOWN :

setImageDrawable( clickImage );

break;

case MotionEvent.ACTION_UP :

setImageDrawable ( buttonImage );

break;

}

return false;

}); 

}


public void SetButton( int buttonImageID , int clickImageID){

this.buttonImage = getResources().getDrawable(buttonImageID);

this.clickImage = getResources().getDrawable(clickImageID);

setImageDrawable(buttonImage);

}

}


<com.shinrolen.kr.MyCustomButton 

 android:paddingRight="10sp" 

 android:layout_width="fill_parent" 

 android:layout_height="wrap_content" 

 android:text="" 

 android:id="@+id/btn_exit" 

 android:background="@drawable/chat_out1" 

 android:scaleType="fitXY"

 android:gravity="center_horizontal"

/>


MyCustomButton connect_btn = (MyCustomButton)findViewById(R.id.btn_exit);

        connect_btn.SetButton(R.drawable.chat_out1, R.drawable.chat_out2);

        connect_btn.setOnClickListener(new View.OnClickListener(){


        @Override

public void onClick(View v) {

        if(v.getId() == R.id.btn_exit){

        Log.i(TAG2, "CustomButton");

        }

}

        });

+ Recent posts