如何隐藏发光效果?

时间:2012-10-05 11:12:55

标签: android onclick imageview glow

发光效果很好。我的疑问是如何隐藏发光效果?如果我点击我的imageview,那个时候我只希望显示我的发光效果,请点击时如何隐藏和显示发光效果。

代码:

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

    }
}

目前的截图:

enter image description here

3 个答案:

答案 0 :(得分:2)

您可以像这样设置setMaskFilter()的空值

paint.setMaskFilter(null);

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

即可

为此你需要为应用程序范围保留paint对象,这样这个paint对象可以在任何你想要的地方访问另一个类或活动,或者你可以设置为true的标志然后显示发光效果和false标志设置什么(默认)< / p>

答案 1 :(得分: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);
}}

答案 2 :(得分: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>

并将其用作buttom drawable:

((ImageView) findViewById(R.id.bmpImg)).setImageDrawable(getResources().getDrawable(R.drawable.button_drawable));