机器人图形:改变只是图像的α(不是背景) [英] Android Graphics: Changing the alpha of just an image (not the background)

查看:152
本文介绍了机器人图形:改变只是图像的α(不是背景)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个新手,在Android的图形(虽然我是pretty精通与Android程序本身)。我想显示在我的应用程序一个灯泡。基于一些价值观,我想改变灯泡的光明。

I am a newbie to graphics in Android (although I am pretty well-versed with Android programming itself). I am trying to display a "lightbulb" in my app. Based on some values, I would like to change the "brightness" of the lightbulb.

我用一个PNG图像这一点。

I used a PNG image for this.

<View
    android:id="@+id/view2"
    android:layout_width="59dp"
    android:layout_height="65dp"
    android:background="@drawable/lightbulb" />

和中,code是这样的:

And, the code is this:

final View normalView = findViewById(R.id.view2);

//Where I need to set the alpha
int alphaValue = /*Some calculation*/
normalView.getBackground().setAlpha(alphaValue);
normalView.invalidate();

PNG文件是一个透明的图形与只有黑色灯泡的轮廓。现在,会发生什么情况是,当我的alpha设置为较低的值(接近0),整个图形变得透明。我只希望在灯泡的含量即可变得透明。祝灯泡的轮廓保持可见。

The png file is a transparent graphic with only the outline of the bulb in black. Now, what happens is when I set the alpha to a lower value (closer to 0), the entire graphic becomes transparent. I want only the content of the bulb to become transparent. I wish the outline of the bulb to stay visible.

我曾尝试创建我的自定义视图如下,但是现在我没有看到图形都没有。

I did try creating my custom View as follows, but now I don't see the graphic at all.

class LightbulbView extends View {

    private BitmapDrawable mLightbulbDrawable;
    private Paint mPaint;

    /*All constructors which call through to super*/ 

    private void initialize(){
        mLightbulbDrawable = new BitmapDrawable(getResources(), BitmapFactory.decodeResource(getResources(), R.drawable.lightbulb));
        mPaint = new Paint();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mLightbulbDrawable.draw(canvas);
    }

    /*Without this, I get an NPE when I do a getBackground on this view*/
    @Override
    public Drawable getBackground() {
        return mLightbulbDrawable;
        //return super.getBackground();
    }
}

和,当然,使用的ImageView没有好处无论是。结果是完全一样的。

And, of course, using an ImageView does no good either. The result is exactly the same.

<ImageView
    android:layout_width="wrap_content"
    android:id="@+id/imageView1"
    android:layout_height="wrap_content"
    android:src="@drawable/lightbulb"
></ImageView>

和这里的code

final ImageView imageView = (ImageView) findViewById(R.id.imageView1);
imageView.setAlpha(alphaValue);

那么,是什么在Android中做到这一点的最好方法是什么?

So, what is the best way to do this in Android?

这是我做到了。请参阅接受的答案(由@Petrus)了解更多详情:

This is how I did it. Please see the accepted answer (by @Petrus) for more details:

<FrameLayout
        android:id="@+id/frameLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
    >
        <ImageView
            android:layout_height="wrap_content"
            android:src="@drawable/lightbulb_content"
            android:layout_width="wrap_content"
            android:id="@+id/bulbContent"
        ></ImageView>
        <ImageView
            android:layout_height="wrap_content"
            android:src="@drawable/lightbulb"
            android:layout_width="wrap_content"
            android:id="@+id/bulb"
        ></ImageView>

    </FrameLayout>

和在code:

final ImageView bulbContent = (ImageView) findViewById(R.id.bulbContent);
//Where I need to set the alpha
int alphaValue = /*Some calculation*/
normalView.getBackground().setAlpha(alphaValue);
bulbContent.setAlpha(alphaValue);

下面,lightbulb.png是与灯泡的只是轮廓的图像; lightbulb_content.png与灯泡的内容的图象。

Here, lightbulb.png is an image with just the outline of the bulb; lightbulb_content.png is an image with the "contents" of the bulb.

推荐答案

我会用两张图片,其中一个灯泡的轮廓和一个与要调整阿尔法的内容。
然后,添加使用ImageViews中的FrameLayout两个图像。首先添加图像进行调整,则轮廓的图像。确保两个图像具有相同的像素尺寸。

I would use two images, one with the lightbulb outline and one with the content you want to adjust the alpha on. Then add the two images using ImageViews in a FrameLayout. First add the image to be adjusted, then the outline image. Make sure both images have the exact same pixel dimensions.

这样,您就可以调整底部图像(灯泡)的α和保持纲要(上图)不变。

This way you can adjust the alpha on the bottom image (lightbulb) and keep the outline (top image) unchanged.

祝你好运。

这篇关于机器人图形:改变只是图像的α(不是背景)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆