woong's

Android HalfCircle 사용하기 본문

Develop/Android

Android HalfCircle 사용하기

dlsdnd345 2016. 2. 14. 17:17
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

Android HalfCircle 사용하기 


안녕하세요 . 이번에 ApiDemo 보고 커스텀을 통해서 만든 반원 입니다 .

간단한건 값을 빼놓았는데 필요한거는 변경해서 사용하시면 될 듯 합니다.





사용법 


Colored By Color Scripter

1
2
3
4
5
6
7
8
9
10
11
    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        
        HalfCircleView halfCircleView = new HalfCircleView(getApplicationContext() , layout.getWidth() , layout.getHeight());
        halfCircleView.setStrokeWidth(10);
        halfCircleView.setEndPoint(180);
        halfCircleView.startDraw();
        
        layout.addView(halfCircleView);
        
    }

 

 

Colored By Color Scripter

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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
    private static class HalfCircleView extends View {
 
        private static final float SWEEP_INC = 2;
 
        private boolean isAlive = false;
        
        private float width ;
        private float height ;
        
        private Paint mPaint;
        private RectF mHalfCircle;
        // 시작위치
        private float startPoint = 180;
        private float endPoint = 180;
        // 시간
        private float mSweep ;        
        //선 굵기
        private int strokeWidth = 10;
        //색
        private int[] colors = {Color.RED, Color.YELLOW, Color.GREEN, Color.CYAN, Color.BLUE,Color.MAGENTA, Color.RED};
        //색 범위
        float[] colorPos = {0.0f, 0.17f, 0.34f, 0.51f, 0.68f, 0.85f, 1.0f};
        
        
        public float[] getColorPos() {
            return colorPos;
        }
 
        public int[] getColors() {
            return colors;
        }
 
        public int getStrokeWidth() {
            return strokeWidth;
        }
 
        public void setStrokeWidth(int strokeWidth) {
            this.strokeWidth = strokeWidth;
        }
        
        public float getStartPoint() {
            return startPoint;
        }
 
        public float getEndPoint() {
            return endPoint;
        }
 
        public void setEndPoint(float endPoint) {
            this.endPoint = endPoint;
        }
 
        
        public HalfCircleView(Context context , float width , float height) {
            super(context);
 
            this.width = width;
            this.height = height;
            
            //반원 좌표
            mHalfCircle = new RectF((float)(width * 0.1), (float)(height *0.2), (float)(width * 0.9), (float)(height * 1.6));
        }
 
 
        /**
         * Draw 시작
         */
        public void startDraw(){
            
            initData();
            
            isAlive = true;
            invalidate();
        }
        
        /**
         * 데이터 초기화
         */
        private void initData(){
            
            startPoint = getStartPoint();
            endPoint   = getEndPoint();
            
            //페인트
            mPaint = new Paint();
            mPaint.setStyle(Paint.Style.STROKE);
            mPaint.setStrokeWidth(getStrokeWidth());
            
            int[] colors = getColors();
            float[] color_pos = getColorPos();
            
            LinearGradient shader = new LinearGradient(0, 0, width, height, colors, color_pos,Shader.TileMode.CLAMP);       
            mPaint.setShader(shader);
        }
        
        /**
         * draw 원
         * @param canvas
         * @param oval
         * @param useCenter
         * @param startPoint
         * @param paint
         */
        private void drawArcs(Canvas canvas, RectF oval, boolean useCenter,float startPoint ,Paint paint) {
            canvas.drawArc(oval, startPoint, mSweep, useCenter, paint);
        }
 
        @Override protected void onDraw(Canvas canvas) {
 
            //배경색
            canvas.drawColor(Color.WHITE);
            //선
            drawArcs(canvas, mHalfCircle, false, getStartPoint(), mPaint);
            //반원만
            mSweep += SWEEP_INC;
            
            if (mSweep > 180) {
                isAlive = false;
            }
            if (mSweep > endPoint) {
                isAlive = false;
            }
            
            if(isAlive){
                invalidate();
            }
        }
        
    }

 


Comments