ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [7] SharedPreferences, Tutorial, Canvas 1,2
    Develpment/Android Sample Source 2020. 9. 13. 21:39

    1. SharedPreferences


     Sample Source

     

    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
    package com.ssh.user.ex_1207;
     
    import android.content.SharedPreferences;
    import android.os.Bundle;
    import android.preference.PreferenceManager;
    import android.support.v7.app.AppCompatActivity;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
     
    public class PrefereActivity extends AppCompatActivity {
     
        Button btn_up, btn_down, btn_reset;
        TextView value;
     
        int num = 0;
     
        //값을 저장하기 위한 클래스
        SharedPreferences pref;
     
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_prefere);
     
            pref = PreferenceManager.getDefaultSharedPreferences( PrefereActivity.this );
     
            // 저장되어 있는 값을 Load
            num = pref.getInt("save"0);
     
            value   = (TextView)findViewById(R.id.value);
     
            value.setText(String.valueOf(num));
     
            btn_up      = (Button)findViewById(R.id.btn_up);
            btn_down    = (Button)findViewById(R.id.btn_down);
            btn_reset   = (Button)findViewById(R.id.btn_reset);
     
            btn_up.setOnClickListener(click);
            btn_down.setOnClickListener(click);
            btn_reset.setOnClickListener(click);
        }//onCreate()
     
        View.OnClickListener click = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
     
                switch(view.getId())
                {
                    case R.id.btn_up:
                        num++;
                        break;
     
                    case R.id.btn_down:
                        num--;
                        break;
     
                    case R.id.btn_reset:
                        num = 0;
                        break;
                }
     
                value.setText( String.valueOf(num) );
            }
        };
     
        @Override
        protected void onDestroy() {
            super.onDestroy();
            //app이 완전히 종료될 때 num 값을 저장.
            SharedPreferences.Editor edit = pref.edit();
     
            edit.putInt("save", num );
            edit.commit();
        }
    }
     
    cs

     

    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
    <?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:id="@+id/activity_prefere"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.ssh.user.ex_1207.PrefereActivity"
        android:gravity="center"
        android:orientation="vertical">
     
        <TextView
            android:id="@+id/value"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:textSize="40dp"
            android:text="0"/>
     
        <Button
            android:id="@+id/btn_up"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="value++"/>
     
        <Button
            android:id="@+id/btn_down"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="value--"/>
     
        <Button
            android:id="@+id/btn_reset"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="reset"/>
     
     
     
    </LinearLayout>
     
    cs

     




    2. Tutorial


     Sample Sourece

     

    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
    package com.ssh.user.ex_1208;
     
    import android.content.Intent;
    import android.content.SharedPreferences;
    import android.os.Bundle;
    import android.preference.PreferenceManager;
    import android.support.v7.app.AppCompatActivity;
    import android.view.View;
    import android.widget.Button;
    import android.widget.CheckBox;
     
    public class TutorialActivity extends AppCompatActivity {
     
        CheckBox check;
        Button start;
     
        SharedPreferences pref;
        boolean b;
     
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_tutorial);
     
            pref = PreferenceManager.getDefaultSharedPreferences(TutorialActivity.this);
     
            b = pref.getBoolean("myTutorial"false);
     
            if(b == true)
                startActivityTutorial();
     
            check = (CheckBox) findViewById(R.id.check);
            start = (Button) findViewById(R.id.start);
     
            start.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
     
                    //체크박스의 현재 상태를 저장
                    SharedPreferences.Editor edit = pref.edit();
                    edit.putBoolean("myTutorial", check.isChecked());
                    edit.commit();
     
                    startActivityTutorial();
                }
            });
     
        }//onCreate()
     
        //화면전환 메서드
        private void startActivityTutorial()
        {
            Intent i = new Intent(TutorialActivity.this, MainActivity.class);
            i.setFlags( Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            startActivity(i);
            finish();
        }
    }
     
    cs

     

    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
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/activity_tutorial"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:background="#aaf"
        tools:context="com.ssh.user.ex_1208.TutorialActivity">
     
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:textColor="#000"
            android:textSize="30dp"
            android:textStyle="italic"
            android:text="tutorlal" />
     
        <CheckBox
            android:id="@+id/check"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Totorial 다시 보지 않기"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"/>
     
        <Button
            android:id="@+id/start"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="start"
            android:layout_alignParentBottom="true"/>
    </RelativeLayout>
     
    cs

     

    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"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/activity_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.ssh.user.ex_1208.MainActivity">
     
        <Button
            android:id="@+id/btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="tutorial"/>
     
    </RelativeLayout>
     
    cs

     



    3. Canvas 1


     Sample Source

     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    package com.ssh.user.ex_1208;
     
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
     
    public class Canvas01Activity extends AppCompatActivity {
     
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView( new MyView(Canvas01Activity.this) );
        }
    }
     
    cs

     

    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
    package com.ssh.user.ex_1208;
     
    import android.content.Context;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.graphics.Rect;
    import android.view.View;
     
    /**
     * Created by USER on 2016-12-08.
     */
     
    public class MyView extends View{
     
        public MyView(Context context) {
            super(context);
        }
     
     
        // View에서는 onDraw에서 작업한내용만 적용
        // 화면에 그림을 갱신하는 메서드
        @Override
        protected void onDraw(Canvas canvas) {
            // 그림을 그리기 위한 붓과 같은 객체
            Paint paint = new Paint();
     
            paint.setColor(Color.BLUE);
     
            //캔버스에 사각형 그리기.
            Rect rt = new Rect();
     
            //  top    right
            //    ______
            //   ㅣ____ㅣ
            // left   bottom
            rt.set(100200300400);
     
            canvas.drawRect(rt, paint);
     
            //캔버스에 원 그리기.
            paint.setColor(Color.RED);
            canvas.drawCircle(200600100, paint);
            //테두리만 있는 색상의 원
            paint.setStyle(Paint.Style.STROKE);
            canvas.drawCircle(400600100, paint);
        }
    }
     
    cs

     



    4. Canvas 2


     Sample Source

     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    package com.ssh.user.ex_1208;
     
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
     
    public class Canvas02Activity extends AppCompatActivity {
     
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(new MyView2(Canvas02Activity.this));
        }
    }
     
    cs

     

    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
    package com.ssh.user.ex_1208;
     
    import android.content.Context;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.graphics.Path;
    import android.view.MotionEvent;
    import android.view.View;
     
    /**
     * Created by USER on 2016-12-08.
     */
     
    public class MyView2 extends View {
     
        public MyView2(Context context) {
            super(context);
        }
     
        Paint paint = new Paint();
        // Path : 화면에 그리기 위한 정보를 모아서 한번에 출력해 주는 클래스
        Path path = new Path();
        int x, y;
     
        @Override
        protected void onDraw(Canvas canvas) {
            paint.setColor(Color.GREEN);
            paint.setStrokeWidth( 3 );
            paint.setStyle(Paint.Style.STROKE);
     
            //path가 기억하는 좌표를 화면에 갱신
            canvas.drawPath(path, paint);
     
        }//onDraw()
     
     
        // 터치 감지
        @Override
        public boolean onTouchEvent(MotionEvent event) {
     
            x = (int)event.getX();
            y = (int)event.getY();
     
            switch(event.getAction())
            {
                case MotionEvent.ACTION_DOWN:
                    //터치된 x,y 좌표를 기억.
                    path.moveTo(x,y);
                    break;
     
                case MotionEvent.ACTION_MOVE:
                    x = (int)event.getX();
                    y = (int)event.getY();
                    path.lineTo(x,y);
                    break;
            }
     
            //onDraw() (화면갱신) 호출
            invalidate();
     
            return true;
        }
    }
     
    cs

     



    댓글

Designed by Tistory.