0

发光效果工作正常。我的疑问是如何隐藏发光效果?如果我单击我的图像视图,那一次我只想显示我的发光效果请如何在单击时隐藏和显示发光效果。

代码:

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 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.test);

        // 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);
        setContentView(R.layout.activity_main);

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

    }
}

目前的屏幕截图:

在此处输入图像描述

4

3 回答 3

2

setMaskFilter()你可以这样设置null

paint.setMaskFilter(null);

你只需要设置paint.setMaskFilter(new BlurMaskFilter(glowRadius, Blur.OUTER));

为此,您需要为应用程序范围保留绘制对象,以便此绘制对象可以在任何您想要的其他类或活动中访问,或者您可以将标志设置为 true,然后显示发光效果和 false 标志不设置任何内容(默认)

于 2012-10-05T11:21:57.670 回答
2

设置 onclicklistener 并实现此代码:

.setOnClickListener(clicklistener);


private OnClickListener backListener = new OnClickListener() {
        public void onClick(View v) {
    // 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.test);

        // 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);
}}
于 2012-10-11T07:51:44.433 回答
0

一个更简单的方法是使用StateListDrawable.

创建两张图像 - 一张用于正常状态,一张用于按下状态(带发光)。在 res/drawable/button_drawable.xml 中使用它:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:drawable="@drawable/image_normal"
        android:state_enabled="true"/>
    <item
        android:drawable="@drawable/image_pressed"
        android:state_pressed="true"/>
</selector>

并将其用作按钮可绘制对象:

((ImageView) findViewById(R.id.bmpImg)).setImageDrawable(getResources().getDrawable(R.drawable.button_drawable));
于 2012-10-05T11:15:28.967 回答