2012-07-09 86 views
0

有没有什么方法可以通过编程方式突出显示选项卡图标图像,而无需使用单独的可绘制资源?Android:以编程方式在图像上突出显示效果

我尝试使用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); 

是否还有其他选择吗?

+0

而不是做这个复杂的,选择时创建选择文件并改变形象,也会给高光效果有效 – AkashG 2012-07-09 12:50:41

+0

,你只需要通过XML,而不是绘制,多数民众赞成它。 – AkashG 2012-07-09 12:51:48

+0

是的,我同意。事情是,当为Android和iOS做同样的应用程序时,我们通常会得到iOS的选项卡图标,这是它自己的突出显示,并且不会为Android获取单独的突出显示的图标。我编写了这个代码,在Android上产生类似的效果,如iOS标签图标突出显示。 – peceps 2012-07-09 12:55:44

回答

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); 
     } 
    } 
    } 

} 
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); 
     } 
    }); 
0

我遇到了这个代码,它完美的运行并且执行得更快。希望这将有助于未来的读者。

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; 
} 
相关问题