마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

1초마다 생성되는 랜덤차트

0 추천

안녕하세요. 처음으로 안드로이드 개발을 하는 학생인데 막히는 곳이 있어 질문드립니다.

현재 랜덤으로 1초마다 주식 봉차트가 생성되는 앱을 개발중인데요.

현재 Canvas를 이용해 랜덤으로 차트가 생성되는것 까지는 구현을 했으나, 1초마다 하나씩 차트가 생성되게 하는 부분에서 막혀있습니다.

현재는 임시로 for문 안에서 차트를 한 번에 출력하고 있는데 for문 안의 구문을 Handler를 이용해서 반복하려고 하니, 구문이 실행되긴 하나 canvas로 그린 차트가 안보이는 현상이 일어나고 있습니다.

onDraw(Canvas canvas)를 handler 구문 안에 넣어도 보고, Paint paint = new Paint; Canvas canvas = new Canvas;를 handler 구문안에도 넣어 봤는데도 여전히 차트는 보이지 않습니다. 첫 차트는 handler문 밖이라 무조건 보이는데 handler문 안에서 canvas.와 paint.를 실행하려고 해서 안보이는 건가요?ㅠㅠ

코드가 실행되고 있는것 같긴 한데 도대체 차트가 왜 안보일까요?ㅠㅠ..

package com.cookandroid.stockchart;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.View;


import java.util.Random;

public class MyGraphicView extends View {
    static int barColor = 1;


    public MyGraphicView(Context context) {
        super(context);
    }

    public MyGraphicView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }



    MyGraphicView myGraphicView;
    private Handler handler = new Handler();

    int startX = 36, startY = 693, open, close, shadowHigh, shadowLow, randY, randO, randH, randL;
    boolean randPN;
    double hardLimit, correc, correcL;
    float correc2;

    public class MyGraphicView2 {


    }



    @Override
    public void onDraw(Canvas canvas) {

        super.onDraw(canvas);
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        Random random = new Random();

        String str;

        canvas.scale(1, 0.01f, 0, 693);

        //테스트선
        //paint.setStrokeWidth(51);
        //canvas.drawLine(startX, startY, startX, 1385, paint);


        //1번차트
        randY = ((int)(Math.random()*300)+20);
        randPN = random.nextBoolean();
        randH = (int)(Math.random()*150);
        randL = (int)(Math.random()*150);

        if (randPN == false) { randY *= 1; }
        else { randY *= -1; }

        close = startY + randY;

        //양봉
        if (close < startY ) {
            barColor = 1;
            shadowHigh = close - randH;
            shadowLow = startY + randL;
        }
        //음봉
        else {
            barColor = 2;
            shadowHigh = startY - randH;
            shadowLow = close + randL;
        }



        switch (barColor) {
            case 1:
                paint.setColor(Color.RED);
                break;
            case 2:
                paint.setColor(Color.BLUE);
                break;
        }


        //선 line
        paint.setStrokeWidth(7);
        canvas.drawLine(startX, shadowHigh , startX, shadowLow, paint);

        //봉 bar
        paint.setStrokeWidth(51);
        canvas.drawLine(startX, startY, startX, close, paint);

        startX += 62;
        correcL = 100;

        //2번차트~

        final Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                for(int i =0; i<16; i++) {
                    //가격제한폭 검사
                    randY = (int) (Math.random() * 999 + 20);

                    hardLimit = (close * 30) / 100;
                    if (randY > hardLimit) {
                        i--;
                        continue;
                    }

                    randO = (int) (Math.random() * 50);
                    randPN = random.nextBoolean();
                    if (randPN == false) {
                        randO *= 1;
                    } else {
                        randO *= -1;
                    }
                    open = close + randO;

                    randPN = random.nextBoolean();
                    randH = random.nextInt(150);
                    randL = random.nextInt(150);


                    if (randPN == false) {
                        randY *= 1;
                    } else {
                        randY *= -1;
                    }


                    close = open + randY;


                    //양봉
                    if (close < open) {
                        barColor = 1;
                        shadowHigh = close - randH;
                        shadowLow = open + randL;
                    }
                    //음봉
                    else {
                        barColor = 2;
                        shadowHigh = open - randH;
                        shadowLow = close + randL;
                    }

                    switch (barColor) {
                        case 1:
                            paint.setColor(Color.RED);
                            break;
                        case 2:
                            paint.setColor(Color.BLUE);
                            break;
                    }


                    //선 line
                    paint.setStrokeWidth(7);
                    canvas.drawLine(startX, shadowHigh, startX, shadowLow, paint);

                    //봉 bar
                    paint.setStrokeWidth(51);
                    canvas.drawLine(startX, open, startX, close, paint);


                    //화면 옮기기
                    if (shadowHigh < 0) {
                        correc = 63 * (1385.0 / (1385 + (-1 * shadowHigh)));
                        if (correc < correcL) {
                            correc2 = (float) (correc);
                            setScaleY(correc2);
                            correcL = correc;
                        }
                    }
                    if (shadowLow > 1385) {
                        correc = 63 * (1385.0 / shadowLow);
                        if (correc < correcL) {
                            correc2 = (float) (correc);
                            setScaleY(correc2);
                            correcL = correc;
                        }
                    }

                    startX += 62;

                    handler.postDelayed(this,1000);
                }
            }
        },1000);


    }


}

 

ChartBoy (120 포인트) 님이 2021년 9월 12일 질문
ChartBoy님이 2021년 9월 12일 수정
챠트 쪽은 잘 모르지만, 라이브러리 개발이나 스터디 목적이 아니라 앱 안에 해당 기능을 넣고 싶은신 거라면, MPChart같은 라이브러리를 이용해서 1초마다 한번씩 값을 갱신해 보세요.

답변 달기

· 글에 소스 코드 보기 좋게 넣는 법
· 질문에 대해 추가적인 질문이나 의견이 있으면 답변이 아니라 댓글로 달아주시기 바랍니다.
표시할 이름 (옵션):
개인정보: 당신의 이메일은 이 알림을 보내는데만 사용됩니다.
스팸 차단 검사:
스팸 검사를 다시 받지 않으려면 로그인하거나 혹은 가입 하세요.
...