Guest User

How to change colors of a Drawable in Android

a guest
Jan 2nd, 2012
1,081
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. private static final int[] FROM_COLOR = new int[]{49, 179, 110};
  2. private static final int THRESHOLD = 3;
  3.  
  4. public void onCreate(Bundle savedInstanceState)
  5. {
  6. super.onCreate(savedInstanceState);
  7. setContentView(R.layout.test_colors);
  8.  
  9. ImageView iv = (ImageView) findViewById(R.id.img);
  10. Drawable d = getResources().getDrawable(RES);
  11. iv.setImageDrawable(adjust(d));
  12. }
  13.  
  14. private Drawable adjust(Drawable d)
  15. {
  16. int to = Color.RED;
  17.  
  18. //Need to copy to ensure that the bitmap is mutable.
  19. Bitmap src = ((BitmapDrawable) d).getBitmap();
  20. Bitmap bitmap = src.copy(Bitmap.Config.ARGB_8888, true);
  21. for(int x = 0;x < bitmap.getWidth();x++)
  22. for(int y = 0;y < bitmap.getHeight();y++)
  23. if(match(bitmap.getPixel(x, y)))
  24. bitmap.setPixel(x, y, to);
  25.  
  26. return new BitmapDrawable(bitmap);
  27. }
  28.  
  29. private boolean match(int pixel)
  30. {
  31. //There may be a better way to match, but I wanted to do a comparison ignoring
  32. //transparency, so I couldn't just do a direct integer compare.
  33. return Math.abs(Color.red(pixel) - FROM_COLOR[0]) < THRESHOLD &&
  34. Math.abs(Color.green(pixel) - FROM_COLOR[1]) < THRESHOLD &&
  35. Math.abs(Color.blue(pixel) - FROM_COLOR[2]) < THRESHOLD;
  36. }
  37.  
  38. /*
  39. * Copyright (C) 2007 The Android Open Source Project
  40. *
  41. * Licensed under the Apache License, Version 2.0 (the "License");
  42. * you may not use this file except in compliance with the License.
  43. * You may obtain a copy of the License at
  44. *
  45. * http://www.apache.org/licenses/LICENSE-2.0
  46. *
  47. * Unless required by applicable law or agreed to in writing, software
  48. * distributed under the License is distributed on an "AS IS" BASIS,
  49. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  50. * See the License for the specific language governing permissions and
  51. * limitations under the License.
  52. */
  53.  
  54. package com.example.android.apis.graphics;
  55.  
  56. import com.example.android.apis.R;
  57.  
  58. import android.app.Activity;
  59. import android.content.Context;
  60. import android.graphics.*;
  61. import android.os.Bundle;
  62. import android.view.KeyEvent;
  63. import android.view.View;
  64.  
  65. public class ColorMatrixSample extends GraphicsActivity {
  66.  
  67. @Override
  68. protected void onCreate(Bundle savedInstanceState) {
  69. super.onCreate(savedInstanceState);
  70. setContentView(new SampleView(this));
  71. }
  72.  
  73. private static class SampleView extends View {
  74. private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
  75. private ColorMatrix mCM = new ColorMatrix();
  76. private Bitmap mBitmap;
  77. private float mSaturation;
  78. private float mAngle;
  79.  
  80. public SampleView(Context context) {
  81. super(context);
  82.  
  83. mBitmap = BitmapFactory.decodeResource(context.getResources(),
  84. R.drawable.balloons);
  85. }
  86.  
  87. private static void setTranslate(ColorMatrix cm, float dr, float dg,
  88. float db, float da) {
  89. cm.set(new float[] {
  90. 2, 0, 0, 0, dr,
  91. 0, 2, 0, 0, dg,
  92. 0, 0, 2, 0, db,
  93. 0, 0, 0, 1, da });
  94. }
  95.  
  96. private static void setContrast(ColorMatrix cm, float contrast) {
  97. float scale = contrast + 1.f;
  98. float translate = (-.5f * scale + .5f) * 255.f;
  99. cm.set(new float[] {
  100. scale, 0, 0, 0, translate,
  101. 0, scale, 0, 0, translate,
  102. 0, 0, scale, 0, translate,
  103. 0, 0, 0, 1, 0 });
  104. }
  105.  
  106. private static void setContrastTranslateOnly(ColorMatrix cm, float contrast) {
  107. float scale = contrast + 1.f;
  108. float translate = (-.5f * scale + .5f) * 255.f;
  109. cm.set(new float[] {
  110. 1, 0, 0, 0, translate,
  111. 0, 1, 0, 0, translate,
  112. 0, 0, 1, 0, translate,
  113. 0, 0, 0, 1, 0 });
  114. }
  115.  
  116. private static void setContrastScaleOnly(ColorMatrix cm, float contrast) {
  117. float scale = contrast + 1.f;
  118. float translate = (-.5f * scale + .5f) * 255.f;
  119. cm.set(new float[] {
  120. scale, 0, 0, 0, 0,
  121. 0, scale, 0, 0, 0,
  122. 0, 0, scale, 0, 0,
  123. 0, 0, 0, 1, 0 });
  124. }
  125.  
  126. @Override protected void onDraw(Canvas canvas) {
  127. Paint paint = mPaint;
  128. float x = 20;
  129. float y = 20;
  130.  
  131. canvas.drawColor(Color.WHITE);
  132.  
  133. paint.setColorFilter(null);
  134. canvas.drawBitmap(mBitmap, x, y, paint);
  135.  
  136. ColorMatrix cm = new ColorMatrix();
  137.  
  138. mAngle += 2;
  139. if (mAngle > 180) {
  140. mAngle = 0;
  141. }
  142.  
  143. //convert our animated angle [-180...180] to a contrast value of [-1..1]
  144. float contrast = mAngle / 180.f;
  145.  
  146. setContrast(cm, contrast);
  147. paint.setColorFilter(new ColorMatrixColorFilter(cm));
  148. canvas.drawBitmap(mBitmap, x + mBitmap.getWidth() + 10, y, paint);
  149.  
  150. setContrastScaleOnly(cm, contrast);
  151. paint.setColorFilter(new ColorMatrixColorFilter(cm));
  152. canvas.drawBitmap(mBitmap, x, y + mBitmap.getHeight() + 10, paint);
  153.  
  154. setContrastTranslateOnly(cm, contrast);
  155. paint.setColorFilter(new ColorMatrixColorFilter(cm));
  156. canvas.drawBitmap(mBitmap, x, y + 2*(mBitmap.getHeight() + 10),
  157. paint);
  158.  
  159. invalidate();
  160. }
  161. }
  162. }
RAW Paste Data