2019 summer 부스트코스 에이스(안드로이드 프로그래밍) /2. 이벤트와 리스트뷰
2-4-2 비트맵 버튼 만들기
GGuJunHee
2019. 7. 30. 22:42
BitmapButton(자바 파일)
...더보기
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
package com.example.boostcoursepractice;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import androidx.appcompat.widget.AppCompatButton;
public class BitmapButton extends AppCompatButton {
public BitmapButton(Context context) {
super(context);
init(context) ;
}
public BitmapButton(Context context, AttributeSet attrs) {
super(context, attrs);
init(context) ;
}
private void init(Context context)
{
setBackgroundResource(R.drawable.ic_kakao);
float textSize = getResources().getDimension(R.dimen.text_size);
setTextSize(textSize);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction() ;
switch (action){
case MotionEvent.ACTION_DOWN:
setBackgroundResource(R.drawable.ic_facebook);
break ;
case MotionEvent.ACTION_UP:
setBackgroundResource(R.drawable.ic_kakao);
break;
}
invalidate();
return true ;
}
}
|
cs |
비트맵 버튼은 AppCompatButton을 상속받아 만들 수 있다.
setTextSize(textSize) 할 때 매개변수는 픽셀단위로 입력 된다. 따라서 dp 단위로 입력하려면 아래와 같이
values에 dimens.xml을 만들어 주고 <dimen>을 만들어 지정해 주면 된다. 그리고
float textSize = getResources().getDimension(R.dimen.text_size);
setTextSize(textSize);
위와 같이 해주면 원하는 dp단위로 지정할 수 있다.