MotionEvent라는 클래스가 있다.

이 클래스에는 터치 관련된 상수들이 정의되어 있다.


이 중에서 여러 상수들이 정의돼 있지만 일단 내가 정확히 알고 있고 왠지 많이 쓰일 거 같은 거 3가지를 추려보자면...


ACTION_DOWN : 처음 눌렸을 때

ACTION_MOVE : 누르고 움직였을 때

ACTION_UP : 누른걸 땠을 때



MotionEvent를 사용할 수 있는 곳은


1. onTouchEvent


onTouchEvent는 화면을 터치하면 호출되는 콜백 메소드이다.

onTouchEvent는 인자로 MotionEvent를 가지고 있으니 매개변수에 getAction()메소드를 사용하여 MotionEvent 상수들과 비교하면 된다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public boolean onTouchEvent(MotionEvent event) {
 
    switch (event.getAction())
    {
        case MotionEvent.ACTION_DOWN:
            textView.setText("ACTION_DOWN");
            return true;
 
        case MotionEvent.ACTION_MOVE:
            textView.setText("ACTION_MOVE");
            return true;
 
        case MotionEvent.ACTION_UP:
            textView.setText("ACTION_UP");
            return false;
    }
    return false;
}
cs



ACTION_DOWN과 ACTION_MOVE는 return true를 하고

ACTION_UP은 return false를 해야지 안전하다고 하는데 

return대신 break를 넣어도 나는 차이를 느낄 수 없었다.



2. 버튼의 리스너


버튼의 리스너로 등록가능하다.

onTouch에도 MotionEvent가 인자로 들어가니 1번 처럼 쓰면 된다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
button = (Button)findViewById(R.id.button);
 
button.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction())
        {
            case MotionEvent.ACTION_DOWN:
                textView.setText("ACTION_DOWN");
                return true;
 
            case MotionEvent.ACTION_MOVE:
                textView.setText("ACTION_MOVE");
                return true;
 
            case MotionEvent.ACTION_UP:
                textView.setText("ACTION_UP");
                return false;
        }
        return false;
    }
});
 
cs






다른 사용방법이 있거나 내가 틀린 부분이 있겠지만 아무튼 다른 내용은 구글 디벨로퍼를 참고하자

https://developer.android.com/reference/android/view/MotionEvent.html


블로그 이미지

stuban

ㅇ.ㅇ

,
우선 xml에 있는 위젯을 자바코드로 가지고 오는 방법


1
TextView widget = (TextView)findViewById(R.id.textview);

cs




저번에 올린 속성들을 자바코드에서 컨트롤하는 법


1
2
3
4
widget.setBackgroundColor(Color.RED); //배경색깔
widget.setVisibility(View.INVISIBLE); //보이기
widget.setClickable(false); //클릭활성화
widget.setRotation(44); //회전 
cs


보통 set 뒤에 바꾸고 싶은 속성을 시작을 대문자로 하여 조합하면 됨


1
android:rotation="44"
cs

  ↓

1
widget.setRotation(44);
cs


반대로 속성값을 가지고 오려면 get을 시작으로 두면 되는데

테스트는 안 해 봤으니깐 장담할 수는 없다.




레이아웃속성을 바꾸는 방법은 다른 것들을 바꾸는 것보다 조금더 복잡하다.


1
2
3
4
5
6
7
8
9
10
11
//레이아웃 파라미터를 만듬
//LayoutParams의 생성자는 너비, 높이 순서다 (물론 다른 생성자도 있음)
LinearLayout.LayoutParams lparam = 
    new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
 
//이렇게 따로 설정 할 수 있다.
lparam.width = ViewGroup.LayoutParams.WRAP_CONTENT;
lparam.height = ViewGroup.LayoutParams.MATCH_PARENT;
lparam.topMargin = 100;//픽셀 단위인듯
 
widget.setLayoutParams(lparam);//마지막에 setLayoutParams으로 넣어주면 끝
cs



https://developer.android.com/reference/android/widget/LinearLayout.LayoutParams.html

블로그 이미지

stuban

ㅇ.ㅇ

,

그림 출처는 한빛 아카데미에서 나온 Android Studio를 활용한 안드로이드 프로그래밍



뷰는 화면에 나오는 모든것들이 상속 받고 있는 클래스다.


버튼이나 텍스트같은 위젯은 물론이고 이것들을 정렬하는 레이아웃도 뷰를 상속하고 있다.




아래는 자주쓴다는 뷰의 속성들을 가지고 만든 예제 이다.


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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<!--xml에서 주석은 이렇게 쓴다 하지만 내용 중간에(전문용어로는 애트리뷰트라고 하는 듯)쓰면 빨간줄이 쳐진다-->
<!--따라서 설명하고 싶은 부분은 괄호를 처서 설명할거임 '()' -->
 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" (정렬방향을 정함 (기본은 가로방향))
    android:paddingTop="50dp" (안쪽에 위쪽으로 간격을  (위나 아래로 따로 주고 싶다면 뒤에 paddingTop paddingLeft등을 사용하면 됨))
    tools:context="com.stuban.test222.MainActivity">
 
 
    <TextView
        android:id="@+id/textview" (java코드에서 위젯을 찾기위한 id @+id/textView는 R파일의 id에 textView를 추가한다는 뜻)
        
        (위젯의 크기 match_parent는 부모의 사이즈에 맞추는 거고 wrap_content는 내용물의 크기에 맞추는 것)
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        
        android:background="#FF0000" (배경색)
        
        android:text="test"
        />
 
    <!--visibility-->
 
    <TextView
        android:visibility="invisible" (안보임)
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="invisible"
        />
 
    <TextView
        android:visibility="visible" (보임 (기본))
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="visible"
        />
 
    <TextView
        android:visibility="gone" (그냥 없는 취급)
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="gone"
        />
 
    <!--enabled,clickable-->
 
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button"
        />
 
    <Button
        android:enabled="false"(버튼 사용 못함)
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="!enabled"
        />
 
    <Button
        android:clickable="false" (터치 무시)
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="!clickable"
        />
 
    <!--rotation-->
 
    <Button
        android:layout_marginTop="100dp"(바깠쪽에 간격을  (위나 아래로 따로 주고 싶다면 뒤에 marginTop marginLeft등을 사용하면 됨))
        android:rotation="44" (회전)
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button" />
 
</LinearLayout>
cs



결과 



참고 : https://developer.android.com/reference/android/view/View.html

블로그 이미지

stuban

ㅇ.ㅇ

,

안드로이드 스튜디오를 시작해 보려고 모든 준비를 끝마쳤다.

나온 지 jdk설치, 안드로이드 스튜디오 설치 + 젤리빈부터 오레오까지 모든 sdk깔기 ,10달도 안된 신상 책 구매까지


그리고 기쁜 마음으로 새 프로젝트를 만들었는데 만들자마자 붉은 줄이 낭자한 화면을 맞이하고야 말았다.





정확히 낭자는 아니고 2줄 정도...


아무튼 빈 엑티비티를 선택하고 만든 깨끗한 프로젝트인데 버그라니...


덕분에 처음에는 3.0을 깔았는데 혹시 몰라서 2.3.3으로 다운그레이드도 해보고

sdk매니저에서 관련 있어 보이는 모든 sdk와 툴도 깔고 구글링도 하며 삽질을 해보았다.


결론은 


빌드 그레이들(build.gradle (Module: app) )을 위에 처럼 바꾸면 된다...


좀 꺼림칙해서 마지막까지 시도하지 않으려던 건데 나만 문제가 있는 건지 아니면 모두가 겪는 일인지는 모르겠지만 구글이 좀 노력했으면 좋겠다.

블로그 이미지

stuban

ㅇ.ㅇ

,