안드로이드

      

 

 

class ClockActivity
package com.example.choi.ex07_graphic;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;

import java.util.Calendar;

public class ClockActivity extends AppCompatActivity {

    boolean isRun;
    Thread th;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new MyView(this));

    }

    //back 를 누르면 자동적으로 onPause() 가 호출 되어서 스레드를 종료시킨다.
    @Override
    protected void onPause() {
        super.onPause();
        isRun=false;
        th=null;
    }

    //내부 클래스
    class MyView extends View implements  Runnable{
        int cx, cy, cw;
        int watchW, watchH;
        Bitmap clock;
        Bitmap pins[] =new Bitmap[3];
        int hour, min, sec;
        float rHour, rMin, rSec;
        int width, height;

        public MyView(Context context) {
            super(context);
            //화면의 가로, 세로 길이
            //getApplicationContext() : 현재 Activity class
            DisplayMetrics disp =getApplication().getResources().getDisplayMetrics();
            width=disp.widthPixels;
            height=disp.heightPixels;
            //가운데 좌표
            cx=width/2;
            cy=(height-100)/2;
            //비트맵 이미지
            Resources res=context.getResources();
            clock= BitmapFactory.decodeResource(res, R.drawable.dial);//배경이미지
            pins[0]=BitmapFactory.decodeResource(res, R.drawable.pin_1); //초
            pins[1]=BitmapFactory.decodeResource(res, R.drawable.pin_2);//분
            pins[2]=BitmapFactory.decodeResource(res, R.drawable.pin_3);//시

            cw=clock.getWidth()/2; //배경이미지 가운데
            watchW=pins[0].getWidth()/2; //시침 가운데
            watchH=pins[0].getHeight()-10;

            th=new Thread(this);
            isRun=true;
            th.start();
        }

        //시간 계산
        void calcTime(){
            //현재 시각 계산
            //  메니페스트 변경  minSdkVersion 23  targetSdkVersion 25
            Calendar calendar= Calendar.getInstance();
            hour=calendar.get(Calendar.HOUR);
            min=calendar.get(Calendar.MINUTE);
            sec=calendar.get(Calendar.SECOND);

            //시분초를 각도로 변환
            rSec=sec*6; //360도 / 60초 => 6도
            rMin=min*6;
            rHour=hour*30+rMin/12;
        }


        @Override
        protected void onDraw(Canvas canvas) {

            calcTime();//시간계산
            canvas.drawColor(Color.WHITE);//배경 색상 설정
            //scale(가로비율, 세로비율, x축, y축)
            canvas.scale(0.8f,1,cx,cy); //캔버스 사이즈 조절
            //rotate(각도, x, y)
            canvas.rotate(0, cx,cy);//캔버스 회전
            canvas.drawBitmap(clock, cx-cw, cy-cw,null);


            //시침 출력
            canvas.rotate(rHour, cx, cy);
            canvas.drawBitmap(pins[2], cx-watchW, cy-watchH, null);

            //분침 출력
            canvas.rotate(rMin-rHour, cx, cy);
            canvas.drawBitmap(pins[1], cx-watchW, cy-watchH, null);

            //초침 출력
            canvas.rotate(rSec-rMin, cx, cy);
            canvas.drawBitmap(pins[0], cx-watchW, cy-watchH, null);
            canvas.rotate(-rSec, cx, cy);

            //시간 출력
            Paint paint=new Paint();
            paint.setColor(Color.BLUE);
            paint.setTextSize(24);
            //cw 배경이미지 시계
            canvas.drawText(String.format("%02d:%02d:%d", hour, min, sec), cx-40, cy+cw+50, paint);
        }

        @Override
        public void run() {
            while (isRun){
                Log.i("test", "백그라운드 스레드 동작중 "+ String.format("%02d:%02d:%d", hour, min, sec));
                postInvalidate(); //화면 갱신 요청 onDraw() 호출
                try {
                    Thread.sleep(1000);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }



    }




}






 

 

 

 

 

 

 

 

 

 

 

 

android

 

about author

PHRASE

Level 60  머나먼나라

무엇인가를 뛰어 넘을 때마다 나는 행복을 느낍니다. -베토벤

댓글 ( 4)

댓글 남기기

작성