Android——如何实现触摸时使图标发光?

7
如何在图标上实现蓝色发光效果?有没有快速的方法来实现这个效果?我真的不想使用 Photoshop 来制作这个效果。
非常感谢您的帮助。

你需要使用statelistdrawables,在其中定义蓝色效果。请访问线程https://dev59.com/TWw15IYBdhLWcg3wntAQ或者你可以访问http://developer.android.com/guide/topics/resources/drawable-resource.html。 - Ashwin N Bhanushali
1个回答

24

如果您想以程序方式生成发光效果,以下是您可以执行的操作。我的建议是,在活动开始时只生成一次,然后使用它创建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);

我非常喜欢你用注释解释代码的方式。谢谢。 - Varundroid

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接