[英]How can I pass a Paint or Color to my new object that is using the onDraw() method from an activity?
[英]How to add a color from android resource file to Paint object?
我想知道如何從Andoid資源文件colors.xml向Paint對象添加顏色? 我到目前為止的方法是
Paint orange = new Paint();
orange.setColor(getResources().getColor(R.color.orange));
但是我的問題是getColor()已被棄用,我不知道應該改用哪種方法。 有沒有人遇到同樣的問題並找到了解決方案?
提前致謝!
使用下面的代碼;
Paint orange = new Paint();
orange.setColor(ContextCompat.getColor(MainActivity.this, R.color.your_color));
如果您當時在Activity中使用,請使用以下代碼:-
Paint orange = new Paint();
orange.setColor(ContextCompat.getColor(this, R.color.yourcolor));
如果您當時在Fragment中使用,請使用以下代碼:-
Paint orange = new Paint();
orange.setColor(ContextCompat.getColor(getActivity(), R.color.yourcolor));
您可以使用ContextCompat.getColor(context, R.color.orange)
代替(getResources().getColor(R.color.orange)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.