안드로이드 개발 질문/답변
(글 수 358)
package qwe.asd.zxc;
import android.app.Activity; import android.content.Context; import android.graphics.Color; import android.graphics.ColorFilter; import android.graphics.LightingColorFilter; import android.graphics.drawable.Drawable; import android.graphics.drawable.LayerDrawable; import android.os.Bundle; import android.util.AttributeSet; import android.widget.Button;
public class WkpExActivity extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } } class SAutoBgButton extends Button { public SAutoBgButton(Context context) { super(context); } public SAutoBgButton(Context context, AttributeSet attrs) { super(context, attrs); } public SAutoBgButton(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public void setBackgroundDrawable(Drawable d) { SAutoBgButtonBackgroundDrawable layer = new SAutoBgButtonBackgroundDrawable(d); super.setBackgroundDrawable(layer); } protected class SAutoBgButtonBackgroundDrawable extends LayerDrawable { protected ColorFilter _pressedFilter = new LightingColorFilter(Color.LTGRAY, 1); protected int _disabledAlpha = 100; public SAutoBgButtonBackgroundDrawable(Drawable d) { super(new Drawable[] { d }); } protected boolean onStateChange(int[] states) { boolean enabled = false; boolean pressed = false; for (int state : states) { if (state == android.R.attr.state_enabled) enabled = true; else if (state == android.R.attr.state_pressed) pressed = true; } mutate(); if (enabled && pressed) { setColorFilter(_pressedFilter); } else if (!enabled) { setColorFilter(null); setAlpha(_disabledAlpha); } else { setColorFilter(null); } invalidateSelf(); return super.onStateChange(states); } public boolean isStateful() { return true; } } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" >
<Button android:layout_width="wrap_content" android:layout_height="154dp" android:layout_weight="0.85" android:background="@drawable/a" android:text="background image" />
<your.dong.button.SAutoBgButton android:layout_width="wrap_content" android:layout_height="232dp" android:background="@drawable/a" android:text="background image" />
</LinearLayout>
자바 초보자인데요 저기 소스코드 인터넷에서 본건데 주석좀 달아주실수 있으신가요 ? ㅠ 급합니다.....
이런건 일일히 달아달라고 하시는것 보단 읽기 쉽게 줄 맞춘다음에 한줄씩한줄씩 공부하신다는 마음으로 보시는게 나을 것 같아요.
그리고 저 코드는 버튼을 상속받아(버튼의 기능을 포함한 상태로,) 눌림 효과를 덧붙인 클래스에 관한 코드인것 같은데,
버튼에 사용할 이미지에 따라 다르겠지만, 이미지를 2개 만들어 사용하시는게 퀄리티적인 면에선 더 좋을 것 같네요.