Android - 如何使图标在触摸时发光?

时间:2012-08-28 10:30:01

标签: android icons touch glow

如何在图标上获得此蓝色发光效果?这样做有什么快捷的方法吗?我真的不想使用photoshop来达到这种效果。

任何帮助都会非常感激。

1 个答案:

答案 0 :(得分:22)

如果您想以编程方式生成光晕,请按照以下步骤操作。我的建议是,在你的活动开始时只生成一次,然后使用它创建一个StateListDrawable,如评论中所述:

    // An added margin to the initial image
    int margin = 24;
    int halfMargin = margin / 2;

    // the glow radius
    int glowRadius = 16;

    // the glow color
    int glowColor = Color.rgb(0, 192, 255);

    // The original image to use
    Bitmap src = BitmapFactory.decodeResource(getResources(),
            R.drawable.ic_launcher);

    // extract the alpha from the source image
    Bitmap alpha = src.extractAlpha();

    // The output bitmap (with the icon + glow)
    Bitmap bmp = Bitmap.createBitmap(src.getWidth() + margin,
            src.getHeight() + margin, Bitmap.Config.ARGB_8888);

    // The canvas to paint on the image
    Canvas canvas = new Canvas(bmp);

    Paint paint = new Paint();
    paint.setColor(glowColor);

    // outer glow
    paint.setMaskFilter(new BlurMaskFilter(glowRadius, Blur.OUTER));
    canvas.drawBitmap(alpha, halfMargin, halfMargin, paint);

    // original icon
    canvas.drawBitmap(src, halfMargin, halfMargin, null);

    ((ImageView) findViewById(R.id.bmpImg)).setImageBitmap(bmp);