티스토리 뷰

Programming/Android

cFont class

군자동꽃미남 2016. 5. 15. 00:21
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
public class cFont
{
    private float           mX, mY;
    private String          mString;
    private Paint           mPaint;
 
    public cFont()
    {
        mPaint              = new Paint();
        mPaint.setAntiAlias(true);
    }
 
    public cFont(String _str, float _x, float _y, int _color, int _size, String _font)
    {
        this();
        mString             = _str;
 
        setLocation(_x, _y);
 
        mPaint.setColor(_color);
        this.setSize(_size);
 
        Typeface            typeface = Typeface.createFromAsset(cGameManager.getInstance().getActivity().getAssets(), _font);
        mPaint.setTypeface(typeface);
    }
 
    public cFont(String _str, float _x, float _y, int _color, int _size)
    {
        this();
        mString             = _str;
 
        setLocation(_x, _y);
 
        mPaint.setColor(_color);
        this.setSize(_size);
    }
 
    public void setAlpha(int _a)
    {
        mPaint.setAlpha(_a);
    }
 
    public void setSize(float _size)
    {
        // 사이즈 * (실행되는 디바이스 dpi / 베이스가 되는 디바이스 dpi) ==> px -> dp로 변환.
        float               size = _size * (cGameManager.getInstance().getDisplayMetrics().densityDpi / 160);
        mPaint.setTextSize(size);
    }
 
    public void setString(String _str)
    {
        mString             = _str;
    }
 
    public void setLocation(float _x, float _y)
    {
        // 베이스가 되는 디바이스의 넓이와 높이로 좌표를 비율로 설정.
        float x             = (float)(_x / cGameManager.getInstance().getWidth());
        float y             = (float)(_y / cGameManager.getInstance().getHeight());
 
        // 설정된 비율을 실행되는 디바이스의 넓이와 높이를 사용하여 비율에 맞게 좌표 재설정.
        mX                  = x * cGameManager.getInstance().getDisplayMetrics().widthPixels;
        mY                  = y * cGameManager.getInstance().getDisplayMetrics().heightPixels;
    }
 
    public void setColor(int _color)
    {
        mPaint.setColor(_color);
    }
 
    public int getAlpha()
    {
        return mPaint.getAlpha();
    }
 
    public float getSize()
    {
        return mPaint.getTextSize();
    }
 
    public String getString()
    {
        return mString;
    }
 
    public void draw(Canvas _canvas)
    {
        _canvas.drawText(mString, mX, mY, mPaint);
    }
}
cs


폰트 사이즈를 dp, 좌표를 상대적 좌표로 재설정하여 그려 줌.

'Programming > Android' 카테고리의 다른 글

imageview set margin programmatically  (0) 2016.06.05
Activity 생명주기  (0) 2016.05.16
안드로이드 인앱 결제 구현 방법.  (0) 2016.05.13
Activity 중복 실행 방지  (0) 2016.04.30
Fatal signal 11 (SIGSEGV) code=2 문제  (0) 2016.04.26
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/03   »
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
글 보관함