java 使用 Canvas 绘制半透明位图 (Android)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2466562/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Drawing translucent bitmaps using Canvas (Android)
提问by wordyword
I have a Bitmap object and want to render it to a Canvas object with varying levels of translucency (i.e. make the whole bitmap partially see through). For example, I have sprites in a game (that are drawn over the top of a bitmap background) that I want to fade out from being opaque to being invisible. Can I do this without having to resort to OpenGL?
我有一个 Bitmap 对象,并希望将其渲染为具有不同透明度级别的 Canvas 对象(即使整个位图部分透视)。例如,我有一个游戏中的精灵(绘制在位图背景的顶部),我想从不透明淡出到不可见。我可以在不必求助于 OpenGL 的情况下做到这一点吗?
回答by YGL
You should be able to define a tween animation and apply that animation to an imageView. The XML resource would be similar to the following (named fade_animation.xml):
您应该能够定义补间动画并将该动画应用到 imageView。XML 资源将类似于以下内容(名为fade_animation.xml):
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator"
android:fromAlpha="1.0"
android:toAlpha="0.0"
android:duration="100" />
then you would load and apply this animation to your custom imageView when ready:
那么你可以在准备好后加载这个动画并将其应用到你的自定义 imageView 上:
ImageView sprite = (ImageView) findViewById(R.id.sprite);
Animation animation = AnimationUtils.loadAnimation(this, R.anim.fade_animation);
sprite.startAnimation(animation);
There are other options too. If you are using bitmaps and you don't want to do an animation, then you can manually decrease the alpha value of each frame, somewhat like this:
还有其他选择。如果你正在使用位图并且你不想做动画,那么你可以手动降低每一帧的 alpha 值,有点像这样:
paint.setAlpha(100);
canvas.drawBitmap(spriteImage, left, top, paint);
Did you try any of these options?
您是否尝试过这些选项中的任何一个?
回答by Lawrence Dol
Reviewing the Android doc (which looks based on JSE with a completely alternative GUI), the following will get you a alpha-channel supporting image:
查看 Android 文档(看起来基于具有完全替代 GUI 的 JSE),以下内容将为您提供 alpha 通道支持图像:
Bitmap bmp=createBitmap(100,100,Bitmap.Config.ARGB_8888);
As long as the sourcebitmap is alpha supporting, you should be able to paint it onto another bitmap and have it's alpha values honored.
只要源位图支持 alpha,您就应该能够将其绘制到另一个位图上并使其 alpha 值得到尊重。
To fade it out, you will need to repeatedly paint the image with some suitable delay (but event driven from a timer, not in a loop, unless Android's rendering system is very different from other Javas) manipulating the alpha channel for each pixel on each successive paint.
要淡出它,您将需要以适当的延迟重复绘制图像(但事件由计时器驱动,而不是在循环中,除非 Android 的渲染系统与其他 Java 非常不同)操纵每个像素上的 alpha 通道连续油漆。
// Make image more transparent by 50%
int[] pxls=new int[100*100];
bmp.getPixels(pxls,0,100,0,0,100,100);
for(int xa=0,len=pxl.length; xa<len; xa++) {
int alp=(pxls[xa] & 0xFF000000);
alp<<1;
pxls[xa]=(alp | (pxls[xa] & 0x00FFFFFF);
}
bmp.setPixels(pxls,0,100,0,0,100,100);
Caveat 1: I wrote this code in-situ from the JavaDoc; it has not been compiled or tested.
警告 1:我从 JavaDoc 原位编写了这段代码;它尚未编译或测试。
Caveat 2: If the hardware device color resolution does not support an alpha-channel (commonly known as 32 bit color), painting of any images to the underlying graphics system maysimply ignore the alpha-channel). But any compositing you do before then on a back-buffer image should be respected. The point being you may haveto use double-buffered painting to do alpha blending while being immune to the underlying device capabilities/config.
警告 2:如果硬件设备颜色分辨率不支持 alpha 通道(通常称为 32 位颜色),则将任何图像绘制到底层图形系统可能会简单地忽略 alpha 通道)。但是在此之前您在后台缓冲图像上所做的任何合成都应该受到尊重。关键是您可能必须使用双缓冲绘画来进行 alpha 混合,同时不受底层设备功能/配置的影响。

