Android ã® Drawable ã¸ã®çè²ã«ã¤ã㦠(Drawable tinting)
Android APIs ãçºãã¦ãã㨠âtintâ ã£ã¦åèªãè¯ãåºã¦ãã¾ã (âandroid:backgroundTintâ ã¨ã âandroid:progressTintâ ã¨ã) ããtint ã«ã¤ãã¦ããã¾ãæ¥æ¬èªã®æ å ±ããªãã£ãã®ã§ã¾ã¨ãã¦ã¿ã¾ãã
ã¡ãªã¿ã« âtintâ ã®è±èªã¨ãã¦ã®æå³ã¯ãåè©ã§ ãè²åãã ã¨ã ãææããåè©ã 㨠ãè²ãä»ããã ã ãæããã ãªã©ã§ãã
ãã®ã¨ã³ããªã®å 容
- API level 21 ãã使ããããã«ãªã£ã Drawable ã¸ã®è²ä»ã (Drawable tinting) ã«ã¤ãã¦èª¬æãã¾ãã
- ã¢ã«ãã¡ãã¹ã¯ã¨ã㦠Drawable ãå®ç¾©ãã表示æã«è²ãæå®ããããã®æ©è½ã§ãã
- View ã®èæ¯ã® Drawable ã¸ã®çè²ã®ããã®ããããã¯
ProgressBar
ãImageView
ãªã©ã§è¡¨ç¤ºããã Drawable ã¸ã®çè²ã®ããã®ã¡ã½ããã XML ã®å±æ§ãç´¹ä»ãã¾ãã - ãããã®æ©è½ã API level 21 æªæºã§ã使ç¨ã§ããããã«ããããã® Android Support Library ãæä¾ããã¡ã½ãããç´¹ä»ãã¾ãã
Drawable ã¸ã®çè² (Drawable tinting)
API level 21 ãããDrawable ãªã½ã¼ã¹ãã¢ã«ãã¡ãã¹ã¯ã¨ãã¦å®ç¾©ãããã® Drawable ã«è²ãä»ãã¦è¡¨ç¤ºãããã¨ãããã¨ãã§ããããã«ãªãã¾ããã åãå½¢ã§ç°ãªãè²ã®ã¢ã¤ã³ã³ãè¤æ°åç¨æãããã¨ãããããªå ´é¢ã§ä¾¿å©ã§ããã
Android 5.0ï¼API ã¬ãã« 21ï¼ä»¥éã§ã¯ãã¢ã«ãã¡ ãã¹ã¯ã¨ãã¦å®ç¾©ãããããããããã 9-patch ã«è²ãä»ãããã¨ãã§ãã¾ãã çè²ã«ã¯ãã«ã©ã¼ãªã½ã¼ã¹ã¾ãã¯ã«ã©ã¼ãªã½ã¼ã¹ã«å¤æããããã¼ãå±æ§ã使ç¨ã§ãã¾ãï¼
ドローアブルの使用 | Android Developers?android:attr/colorPrimary
ãªã©ï¼ã
bitmapãããã¯nine-patcheãã¢ã«ãã¡ãã¹ã¯ã¨ãã¦å®ç¾©ãcolorãªã½ã¼ã¹ããã¼ãã®å±æ§ã§è²ã¥ããã
L Developer Preview マテリアルデザイン - アニメーション - Firespeed
Drawable
ã¯ã©ã¹ã«ãä¸è¨ã®ã¡ã½ããã追å ããã¦ãã¾ãã
Drawable#setTint(int)
ã¡ã½ããDrawable#setTintList(ColorStateList)
ã¡ã½ããDrawable#setTintMode(PoterDuff.Mode)
ã¡ã½ãã
ãããã使ããã¨ã§ãDrawable ã«çè²ãããã¨ãã§ãã¾ãã
// é常éã Drawable ãªãã¸ã§ã¯ããåå¾ãã#setTint ã¡ã½ããã #setTintMode ã¡ã½ãããå¼ã¶ãã¨ã§çè²ã Drawable d = getDrawable(R.drawable.example); d.setTint(ContextCompat.getColor(this, R.color.exampleTint)); d.setTintMode(PorterDuff.Mode.SRC_IN); // å¾ã¯ãã® Drawable ãªãã¸ã§ã¯ãã使ãã imageView.setDrawable(d); // ä¾ãã° ImageView ã§è¡¨ç¤ºããã
ã¡ãªã¿ã«ããã®æ©è½ãå°å
¥ãããåãããåæ§ã®ãã¨ã Drawable#setColorFilter
ã¡ã½ãã ã§å®ç¾ã§ãã¦ããããã§ãã setColorFilter
ãå¼ãã å ´åã¯ãtint ã®è¨å®ã¯ä¸æ¸ãããã¾ãã
Tint mode ã«ã¤ãã¦
Drawable#setTintMode(PoterDuff.Mode)
ã¡ã½ããã§ã¯ãå
ã® Drawable 㨠Tint ã®è²ãã©ã®ããã«æ··ãããã Porter-Duff ã®ã«ã¼ã«ã§æå®ãã¾ãã å
ã® Drawable ã destination ã§ãçè²ããè²ã source ã§ãã
ããã©ã«ãã§ã¯ Source In Destination è¦å (PorterDuff.Mode.SRC_IN
) ã使ç¨ããã¾ãã ããã¯ãåç´ã« tint ãä¸éæãªåè²ã®å ´åãåæå¾ã®åãã¯ã»ã«ã®ä¸éæ度ã¯å
ã® Drawable ã®ä¸éæ度ã«ãªããåæå¾ã®åãã¯ã»ã«ã®è²ã¯ tint ã®è²ã«ãªãã¨ãããã®ã§ãã ã¾ãã« Drawable ãã¢ã«ãã¡ãã¹ã¯ã«ãªãããã§ãã
Porter-Duff ã®ã«ã¼ã«ã«ã¤ãã¦ã¯ä»¥åãã®ããã°ã«æ¸ãã¾ããã ä¸è¨ã¨ã³ããªãåç §ãã¦ãã ããã
Support library ã«ãã Drawable ã¸ã®çè²ã®ãµãã¼ã
API level 21 ããå°å
¥ããã Drawable ã¸ã®çè²ã®æ©è½ã§ãããv4 Support Library ã«å«ã¾ãã DrawableCompat
ã¯ã©ã¹ã使ããã¨ã§ãå¤ã API level ã§ãåæ§ã®æ©è½ãå®ç¾ã§ãã¾ãã
Drawable d = DrawableCompat.wrap(ContextCompat.getDrawable(this, R.drawable.example)); DrawableCompat.setTint(d, ContextCompat.getColor(this, R.color.exampleTint)); DrawableCompat.setTintMode(d, PorterDuff.Mode.SRC_IN);
対象㮠Drawable ã DrawableCompat#wrap
ã¡ã½ããã«æ¸¡ã㦠tint 対å¿ã® Drawable ã«å¤æããããã DrawableCompat#setTint
ã¡ã½ãããªã©ã«æ¸¡ããã¨ãã使ãæ¹ã«ãªãã¾ãã
- é¢é£ãã¼ã¸ : AppCompat と tintを使ってリソースを減らす - Qiita
Drawable ã¸ã®çè²ã®å¿ç¨
å®éã« Drawable ã¸ã®çè²ããããå ´é¢ã¨ããã®ã¯ãä¾ãã° View ã®èæ¯ã« Drawable ãã»ãããã¦ãã® Drawable ã«çè²ããã ã¨ããImageView
ã§è¡¨ç¤ºãã Drawable ã«çè²ããã ã¨ããããã㯠ProgressBar
ã® (ããã°ã¬ã¹ãã¼ãã®ãã®ã表ã) Drawable ã«çè²ããã ã¨ããããããå ´é¢ãå¤ãã§ãããã ãã®ãããªå ´é¢ã§ç°¡åã« Drawable ã¸ã®çè²ãè¡ããããã«ãJava ã¯ã©ã¹ã®ã¡ã½ãããã¬ã¤ã¢ã¦ãã® XML ã§ä½¿ããå±æ§ãªã©ãç¨æããã¦ãã¾ãã
å view ã®èæ¯ã® Drawable ã¸ã®çè² (Background tint)
API level 21 ã§å°å ¥ãããä¸è¨ã®ã¡ã½ããã XML å±æ§ã使ç¨ãã¦ãç°¡åã«èæ¯ã® Drawable ã«çè²ã§ãã¾ãã
View#setBackgroundTintList(ColorStateList)
ã¡ã½ãã- 対å¿ããã¬ã¤ã¢ã¦ã XML ã®å±æ§ã¯
android:backgroundTint
- 対å¿ããã¬ã¤ã¢ã¦ã XML ã®å±æ§ã¯
View#setBackgroundTintMode(PorterDuff.Mode)
ã¡ã½ãã- 対å¿ããã¬ã¤ã¢ã¦ã XML ã®å±æ§ã¯
android:backgroundTintMode
- 対å¿ããã¬ã¤ã¢ã¦ã XML ã®å±æ§ã¯
API level 21 æªæºã®ç°å¢ã§ãããã®æ©è½ã使ããããã«ãv7 appcompat library ãå種 compatible widget ãæä¾ãã¦ãã¾ãã (ãã ãã対å¿ããã¦ãã View ã®ã¯ã©ã¹ã¯ä¸é¨ã®ã¿ã) ãã£ã¦ãv7 appcompat library ã使ããã¨ã§ã以ä¸ã®ããã«ã㦠API level 21 æªæºã®ç°å¢ã§ã background tint ã使ç¨ã§ãã¾ãã
<!-- v7 appcompat library ã使ç¨ãã¦ããã¨ããã® TextView ã¯ã¤ã³ãã¬ã¼ãæã« AppCompatTextView ã«å¤æãããã --> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/example_text_view_background" app:backgroundTint="@color/exampleTextViewBackgroundTint"/>
ãã ãä¸ã®ãããªæ¸ãæ¹ããã㨠Android Studio ã§ã¨ã©ã¼ã表示ãããã®ãæ©ã¾ããã¨ããã§ãã (AppCompatTextView
ã¸ã®å¤æã¾ã§èªèãã¦ãããªãããã)
v7 appcompat library ã® compatible widget ã«ã¤ãã¦ã¯ä»¥åã¨ã³ããªãæ¸ãã¾ããã®ã§ã詳ããã¯ä¸è¨ã¨ã³ããªãåç §ãã¦ãã ããã
ãã®ä»ã® Drawable ã®çè²
ImageView
ã«ã¯ã表示ãã Drawable ã«çè²ããããã®ã¡ã½ãããåå¨ãã¾ãã
ImageView#setImageTintList(ColorStateList)
ã¡ã½ãã- 対å¿ããã¬ã¤ã¢ã¦ã XML ã®å±æ§ã¯
android:tint
- 対å¿ããã¬ã¤ã¢ã¦ã XML ã®å±æ§ã¯
ImageView#setImageTintMode(PorterDuff.Mode)
ã¡ã½ãã- 対å¿ããã¬ã¤ã¢ã¦ã XML ã®å±æ§ã¯
android:tintMode
- 対å¿ããã¬ã¤ã¢ã¦ã XML ã®å±æ§ã¯
ProgressBar
ã§ã Drawable ã¸ã®çè²ãå¯è½ã§ãã
ProgressBar#setProgressTint(ColorStateList)
ã¡ã½ãã- 対å¿ããã¬ã¤ã¢ã¦ã XML ã®å±æ§ã¯
android:progressTint
- 対å¿ããã¬ã¤ã¢ã¦ã XML ã®å±æ§ã¯
ProgressBar#setProgressTintMode(PorterDuff.Mode)
ã¡ã½ãã- 対å¿ããã¬ã¤ã¢ã¦ã XML ã®å±æ§ã¯
android:progressTintMode
- 対å¿ããã¬ã¤ã¢ã¦ã XML ã®å±æ§ã¯
- ãªã©ãå Drawable ã«å¯¾å¿ããã¡ã½ãã (ããã³ããã«å¯¾å¿ãã XML ã®å±æ§) ãå®ç¾©ããã¦ãã
調ã¹ããã¦ãã¾ããããDrawable ãæ±ãä»ã® View ã¯ã©ã¹ã«ãä¼¼ããããªæ©è½ãããããããã¾ããã
API level 21 æªæºã§ãã®ãããªæ©è½ã使ããããã«ããäºæã¯ã©ã¹ã¯ (ä»ã®ã¨ãã) Android Support Library ã«ã¯å«ã¾ãã¦ããªãããã§ãã®ã§ãAPI level 21 æªæºã§ã¯ç´æ¥ Drawable ã«çè²ãããã㧠ImageView
ã«ã»ããããã ProgressBar
ã«ã»ãããããããå¿
è¦ãããããã§ãã
ã¾ã¨ã
- API level 21 ãã Drawable ã¸ã®è²ä»ãããµãã¼ãããã¾ããã (Drawable tinting)
- åºæ¬çã«ã¯ãã¢ã«ãã¡ãã¹ã¯ã¨ã㦠Drawable ãå®ç¾©ãã表示æã«è²ãæå®ããã¨ãã使ãæ¹ãæ³å®ããã¦ãã¾ãã
- View ã®èæ¯ã® Drawable ã¸ã®çè²ãã
ProgressBar
ãImageView
ãªã©ã§è¡¨ç¤ºããã Drawable ã¸ã®çè²ãç°¡åã«è¡ããããã«ãªã£ã¦ãã¾ãã - Android Support Library ã使ããã¨ã§ãDrawable ã¸ã®çè²ã®æ©è½ã View ã®èæ¯ã® Drawable ã¸ã®çè²ã API level 21 æªæºã§ãå©ç¨ã§ãã¾ãã
é¢é£ãã¼ã¸
- 【Android】Support LibraryのVectorDrawableCompatが適応されるまでの仕組み - Qiita
- Drawable ã¸ã®çè²ã§ã¯ãªã Vector Drawable ã®ãµãã¼ãã«ã¤ãã¦ã§ãããå é¨ã§ã©ãããå¦çãããã¦ããã®ã説æããã¦ãã¾ãã ãã®ã¨ã³ããªã®å 容ã¨ãã¶ã£ã¦ããé¨åãããã¾ãã®ã§ãããã¦èªãã¨è¯ãããã§ãã