1. StateListDrawable (상태 드로어블)


상태 드로어블은 뷰의 상태에 따라 뷰에 보여줄 그래픽을 다르게 지정할 수 있다.

/res/drawable 폴더 안에 새로운 XML 파일을 만들면 최상위 태그는 <selector>가 된다.

그 안에 <item> 태그를 넣을 수 있으며, drawable 속성에는 이미지나 다른 그래픽을 설정하여 화면에 보이도록 할 수 있다.


state_ 로 시작하는 속성은 상태를 나타낸다. 

state_pressed: 눌린 상태

state_focused: 포커스를 받은 상태



2. 예제1 결과 화면


  



3. 예제1 - activity_main.xml


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context="com.tistory.qlyh8.pracitice.MainActivity">
 
    <Button
        android:layout_width="128dp"
        android:layout_height="128dp"
        android:background="@drawable/thumbs_up"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent" />
 
</android.support.constraint.ConstraintLayout>
 
cs


4. 예제1 - drawable/thumbs_up.xml

1
2
3
4
5
6
7
8
9
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
 
    <!-- 순서 지켜야 한다. -->
    <item android:state_pressed="true"
        android:drawable="@drawable/thumbs_up_selected"/>
 
    <item android:drawable="@drawable/thumbs_up_not_selected"/>
</selector>
cs



5. 예제2 결과 화면 - 입력상자에 대한 이미지 전환 이벤트




6. 예제2 - drawable/state_focus_press.xml

1
2
3
4
5
6
7
8
9
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_focused="true"
        android:drawable="@drawable/thumbs_up_selected"/>
 
    <item
        android:drawable="@drawable/thumbs_up_not_selected"/>
</selector>
cs

7. 예제2 - activity_main.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.tistory.qlyh8.pracitice.MainActivity">
 
   <EditText
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:background="@drawable/state_focus_press"/>
 
</LinearLayout>
 
cs





출처: https://www.edwith.org/boostcourse-android/lecture/20420/

'Android' 카테고리의 다른 글

Table Layout (테이블 레이아웃)  (0) 2018.04.28
Drawable (드로어블) - Shape Drawable  (0) 2018.04.28
Drawable (드로어블)  (0) 2018.04.27
adMob - (4) 전면 광고 달기  (0) 2018.03.03
adMob - (3) 배너 광고 달기  (0) 2018.03.03

1. Drawable (드로어블)


드로어블은 뷰에 설정할 수 있는 객체이며 그래픽으로 그릴 수 있다.

드로어블은 소스 코드에서 만들 수도 있고 XML에서 정의할 수도 있는데 XML로 만들어 사용하는 경우가 많다.

드로어블 XML파일은 /res/drawable 폴더 안에 넣어서 마치 이미지처럼 뷰의 배경으로 설정될 수 있다.



2. Drawable 종류


- BitmapDrawable: 이미지 파일을 보여줄 때 사용한다.


- StateListDrawable: 상태별로 다른 그래픽을 참조할 때 사용한다.


- TransitionDrawable: 두 개의 드로어블 간에 바뀌도록 한다.


- ShapeDrawable: 색상과 그라데이션을 포함하여 도형 모형을 정의한다.


- InsetDrawable: 지정한 거리만큼 안쪽으로 들어오게 하여, 뷰가 뷰의 실제 범위보다 작은 백그라운드가 필요할 때 사용된다.


- ClipDrawable: 다른 드로어블을 클리핑하여 진행률 표시 줄과 같은 항목을 구현하는데 사용된다.


- ScaleDrawable: 다른 드로어블의 크기를 바꾼다.





출처 : https://www.edwith.org/boostcourse-android/lecture/20420/



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
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
 
public class algo2_1260_dfs_bfs {
    
    private static int N;
    private static int M;
    private static int V;
    private static int[][] W;
    private static boolean[] visit;
    
    public static void main(String[] args) {
    
        Scanner scanner = new Scanner(System.in);
        N = scanner.nextInt();
        M = scanner.nextInt();
        V = scanner.nextInt();
        W = new int[N+1][N+1];
        visit = new boolean[N+1];
        
        for(int i=0 ; i<M ; i++){
            int N1 = scanner.nextInt();
            int N2 = scanner.nextInt();
            W[N1][N2] = 1;
            W[N2][N1] = 1;
        }
        scanner.close();
        
        dfs(V);
        
        System.out.print("\n");
        for(int i=0 ; i<visit.length ; i++)
            visit[i] = false;
    
        bfs(V);
    }
    
    public static void dfs(int num){
        
        System.out.print(num + " ");
        visit[num] = true;
        
        for(int i=1 ; i<=N ; i++){
            if(W[num][i]==1 && !visit[i])
                dfs(i);
        }    
    }
    
    public static void bfs(int num){
        
        Queue<Integer> queue = new LinkedList<>();
        
        queue.offer(num);
        visit[num] = true;
        
        while(!queue.isEmpty()){
            int front = (int)queue.peek();
            queue.poll();
            
            System.out.print(front + " ");
            
            for(int i=1 ; i<=N ; i++){
                if(W[front][i]==1 && !visit[i]){
                    queue.offer(i);
                    visit[i] = true;
                }
            }
        }
    }
}
 
cs




'Algorithm' 카테고리의 다른 글

백준 9012번: 괄호  (0) 2018.07.18
백준 10828번: 스택  (0) 2018.07.17
BFS (Breadth-First Search)  (0) 2018.04.12
DFS (Depth-First Search)  (0) 2018.04.12
백준 1003번: 피보나치 함수  (0) 2018.04.10

+ Recent posts