来自另一个资源的位图绘制 [英] Draw bitmaps from resources over another
问题描述
我有两个位图的背景的和的前景的。我如何借鉴背景位图的前景,而不使用其他画布?
I have got two bitmaps, background and foreground. How do I draw bitmap foreground on background without using another Canvas?
解决方案:
1)首先创建一个额外的选项ARGB_8888从位图资源
1) First create bitmaps from resources with additional option ARGB_8888
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
2)声明位图
Bitmap background = BitmapFactory.decodeResource(getResources(), R.drawable.background, options);
Bitmap foreground = BitmapFactory.decodeResource(getResources(), R.drawable.foreground, options);
3)内部的onDraw()函数绘制图形
3) Inside onDraw() function draw graphics
protected void onDraw(Canvas canvas)
{
canvas.drawColor(Color.White);
Paint paint = new Paint();
canvas.drawBitmap(background, 0, 0, paint);
paint.setXfermode( new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER));
canvas.drawBitmap(foreground, 0, 0, paint);
}
和作为Soxxeh说,这是信息的很好来源:<一个href=\"http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/Xfermodes.html\" rel=\"nofollow\">http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/Xfermodes.html
And as Soxxeh said, this is very good source of information: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/Xfermodes.html
推荐答案
试试这个:
canvas.drawBitmap(backgroundImageBitmap, 0.0f, 0.0f, null);
canvas.drawBitmap(foregroundImageBitmap, 0.0f, 0.0f, null);
第二个图像(前景图像)都有阿尔法方面或您无法通过它看到的。
The second image (foreground image) has to have Alpha aspects or you can't see through it.
这篇关于来自另一个资源的位图绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!