모바일/Android

안드로이드 색깔 바꾸기 getColor() 및 DP로 stroke치기

늘근이 2016. 4. 13. 13:07

참조

https://blog.asamaru.net/2015/08/28/android-getcolor-getdrawable-deprecated/

http://stackoverflow.com/questions/2406449/does-setwidthint-pixels-use-dip-or-px


아래의 getColor() 가 Deprecated되었고,

getResources().getColor(R.color.color_name)


아래와 같이 쓰면 된다.

ContextCompat.getColor(getApplicationContext(), R.color.c_orange)


paint.setColor();

안에 잘 집어넣어주면 된다.


또한 이미지에 스트로크를 치는데 px로 설정이 되지 않도록 다음과 같이 한다.

values - dimens.xml에 이렇게 추가하고

<dimen name="c_box_stroke">2dp</dimen>

코드에

Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.STROKE);
paint.setColor(ContextCompat.getColor(getApplicationContext(), R.color.c_orange));
float width = getResources().getDimension(R.dimen.c_box_stroke);
paint.setStrokeWidth(width);