1

有没有办法在不使用单独的可绘制资源的情况下以编程方式突出显示选项卡图标图像?

我尝试使用 PorterDuffColorFilter 但它看起来不太好:

// apply a color mask to the icon to mark it as selected
int selectedIconMaskColor = view.getResources().getColor(R.color.tab_icon_selected);
PorterDuffColorFilter selectedIconFilter = new PorterDuffColorFilter(selectedIconMaskColor,
  PorterDuff.Mode.SRC_ATOP);
copyIconDrawable.setColorFilter(selectedIconFilter);

还有其他选择吗?

4

3 回答 3

1

我最终使用了简单的图像处理类:

import android.graphics.Bitmap;
import android.graphics.Color;

/**
 * Image with support for filtering.
 */
public class FilteredImage {

  private Bitmap image;

  private int width;

  private int height;

  private int[] colorArray;

  /**
   * Constructor.
   * 
   * @param img the original image
   */
  public FilteredImage(Bitmap img) {
    this.image = img;
    width = img.getWidth();
    height = img.getHeight();

    colorArray = new int[width * height];
    image.getPixels(colorArray, 0, width, 0, 0, width, height);

    applyHighlightFilter();
  }

  /**
   * Get the color for a specified pixel.
   * 
   * @param x x
   * @param y y
   * @return color
   */
  public int getPixelColor(int x, int y) {
    return colorArray[y * width + x];
  }

  /**
   * Gets the image.
   * 
   * @return Returns the image.
   */
  public Bitmap getImage() {
    return image;
  }

  /**
   * Applies green highlight filter to the image.
   */
  private void applyHighlightFilter() {
    int a;
    int r;
    int g;
    int b;
    for (int x = 0; x < width; x++) {
      for (int y = 0; y < height; y++) {

        int c = getPixelColor(x, y);

        a = Color.alpha(c);
        r = Color.red(c);
        g = Color.green(c);
        b = Color.blue(c);

        r = (int) (r * 0.8);
        g = (int) (g * 1.6);
        b = (int) (b * 0.8);

        if (r > 255) {
          r = 255;
        }
        if (r < 0) {
          r = 0;
        }
        if (g > 255) {
          g = 255;
        }
        if (g < 0) {
          g = 0;
        }
        if (b > 255) {
          b = 255;
        }
        if (b < 0) {
          b = 0;
        }

        int resultColor = Color.argb(a, r, g, b);
        image.setPixel(x, y, resultColor);
      }
    }
  }

}
于 2012-07-09T12:47:51.483 回答
1

我遇到了这段代码,它运行得很好并且执行得更快。希望这对未来的读者有所帮助。

public static Bitmap highlightImage(Bitmap src) {
    // create new bitmap, which will be painted and becomes result image
    Bitmap bmOut = Bitmap.createBitmap(src.getWidth() + 96, src.getHeight() + 96, Bitmap.Config.ARGB_8888);
    // setup canvas for painting
    Canvas canvas = new Canvas(bmOut);
    // setup default color
    canvas.drawColor(0, PorterDuff.Mode.CLEAR);
    // create a blur paint for capturing alpha
    Paint ptBlur = new Paint();
    ptBlur.setMaskFilter(new BlurMaskFilter(15, BlurMaskFilter.Blur.NORMAL));
    int[] offsetXY = new int[2];
    // capture alpha into a bitmap
    Bitmap bmAlpha = src.extractAlpha(ptBlur, offsetXY);
    // create a color paint
    Paint ptAlphaColor = new Paint();
    ptAlphaColor.setColor(0xFFFFFFFF);
    // paint color for captured alpha region (bitmap)
    canvas.drawBitmap(bmAlpha, offsetXY[0], offsetXY[1], ptAlphaColor);
    // free memory
    bmAlpha.recycle();

    // paint the image source
    canvas.drawBitmap(src, 0, 0, null);

    // return out final image
    return bmOut;
}
于 2015-12-10T07:18:07.947 回答
0

我这样做了,不是最好的解决方案,但有效......

imageView.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            imageView.setColorFilter(R.color.button_selection);
            doLater(SECOND / 3, new Run() {
                @Override
                public void run() {
                    imageView.setColorFilter(0x00000000);
                }
            });
            doClick(imageView);
        }
    });

这是为了其他一些观点......

view.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            view.getBackground().setColorFilter(R.color.button_selection, Mode.SRC_OVER);
            view.getBackground().invalidateSelf();
            doLater(SECOND / 3, new Run() {
                @Override
                public void run() {
                    view.getBackground().setColorFilter(0x00000000, Mode.SRC_OVER);
                    view.getBackground().invalidateSelf();
                }
            });
            doClick(view);
        }
    });
于 2013-07-30T13:33:10.107 回答