Android 图形:仅更改图像(而不是背景)的 Alpha
我是 Android 图形方面的新手(尽管我非常熟悉 Android 编程本身)。我正在尝试在我的应用程序中显示一个“灯泡”。基于一些值,我想改变灯泡的“亮度”。
我为此使用了 PNG 图像。
<View
android:id="@+id/view2"
android:layout_width="59dp"
android:layout_height="65dp"
android:background="@drawable/lightbulb" />
并且,代码是这样的:
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)时,整个图形将变得透明。我只希望灯泡的内容变得透明。我希望灯泡的轮廓保持可见。
我确实尝试按如下方式创建自定义视图,但现在我根本看不到图形。
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 也没有什么好处。结果是完全一样的。
<ImageView
android:layout_width="wrap_content"
android:id="@+id/imageView1"
android:layout_height="wrap_content"
android:src="@drawable/lightbulb"
></ImageView>
这是代码
final ImageView imageView = (ImageView) findViewById(R.id.imageView1);
imageView.setAlpha(alphaValue);
那么,在 Android 中执行此操作的最佳方法是什么?
解决方案
我就是这样做的。请参阅已接受的答案(由@Petrus)了解更多详细信息:
<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>
在代码中:
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 是包含灯泡“内容”的图像。
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.
I used a PNG image for this.
<View
android:id="@+id/view2"
android:layout_width="59dp"
android:layout_height="65dp"
android:background="@drawable/lightbulb" />
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();
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();
}
}
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>
And here's the code
final ImageView imageView = (ImageView) findViewById(R.id.imageView1);
imageView.setAlpha(alphaValue);
So, what is the best way to do this in Android?
SOLUTION
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>
And in 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);
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会使用两张图像,一张带有灯泡轮廓,一张带有您要调整 Alpha 的内容。
然后使用 FrameLayout 中的 ImageView 添加两个图像。首先添加要调整的图像,然后添加轮廓图像。确保两个图像具有完全相同的像素尺寸。
这样您就可以调整底部图像(灯泡)上的 Alpha 并保持轮廓(顶部图像)不变。
祝你好运。
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.
Good luck.