【问题标题】:how to hide glow effect?如何隐藏发光效果?
【发布时间】:2012-10-11 15:51:44
【问题描述】:

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

代码:

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

    }
}

现在的屏幕截图:

【问题讨论】:

  • 这是什么? R.id.bmpImg 。你想说一些关于最后一行的描述吗?!
  • @Mr.Hyde 它什么都没有我的布局中有 imageview,所以我通过 id 使用该 imageview。就这样。这是另一种格式,您可以同时使用这两种格式 1. ImageView img=(ImageView)findViewById(R.id.bmpImg); 2.img.setImageBitmap(bmp);

标签: android onclick imageview glow


【解决方案1】:

设置 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】:

    你可以像这样为setMaskFilter()设置null

    paint.setMaskFilter(null);
    

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

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

    【讨论】:

    • 谢谢,还有一个疑问如何在我的图像视图中使用 onclick 事件
    【解决方案3】:

    更简单的方法是使用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));
    

    【讨论】:

    • android:drawable="@drawable/image_normal", android:drawable="@drawable/image_pressed" 错误
    • 如前所述,创建这些图像(image_normal.png 和 image_pressed.png)并添加到您的可绘制文件夹中。还要注意更新的 XML。
    • ImageView类型中的setImageDrawable(Drawable)方法不适用于参数(int)错误行:((ImageView) findViewById(R.id.bmpImg)).setImageDrawable(R.drawable. button_drawable);
    • 请看更新后的代码:((ImageView) findViewById(R.id.bmpImg)).setImageDrawable(getResources().getDrawable(R.drawable.button_drawable));
    猜你喜欢
    • 1970-01-01
    • 2016-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-25
    • 2020-11-11
    • 2017-09-26
    • 1970-01-01
    相关资源
    最近更新 更多