简体   繁体   English

Android Canvas背景透明

[英]Android Canvas background transparent

I draw on a bitmap an icon with a stroke color. 我在位bitmap上绘制带有笔触颜色的图标。 That origin bitmap has a white background. 该原始bitmap具有白色背景。 On that with canvas I draw a second bitmap with a stroke (circle). canvas我用笔触(圆形)绘制第二个bitmap

What happens is, that the resulting bitmap has on Nexus 7 or Galaxy S2 (both hdpi) a transparent backround. 发生的结果是,生成的位图在Nexus 7或Galaxy S2(均为hdpi)上具有透明背景。 On other devices is all fine (its still white). 在其他设备上一切正常(仍然为白色)。

That bitmap is used as a marker for gmaps. 该位图用作gmap的标记。

public Bitmap getMarkerBitmap(){
    Bitmap clusterBackground = backgroundImage.copy(Bitmap.Config.ARGB_8888, true);
    final Canvas canvas = new Canvas(clusterBackground);
    int xAnchor = (clusterBackground.getWidth() / 2)-(newIcon.getWidth() / 2);
    int yAnchor = (clusterBackground.getHeight()/2)-(newIcon.getHeight() / 2);
    final Point center = new Point(xAnchor,yAnchor);
    canvas.drawBitmap(newIcon, center.x, center.y,paint);
    return drawCircle(canvas,clusterBackground);
}

private Bitmap drawCircle(Canvas canvas, Bitmap bitmap){
    paint.setColor(color);
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeWidth(strokeWidthInPx);
    int centerX = bitmap.getWidth() / 2;
    int centerY = bitmap.getHeight() / 2;
    canvas.drawCircle(centerX,centerY,centerY-strokeWidthInPx/2,paint);

    return bitmap;
}

When I set the background to white per Paint object, then it works, but then the whole Rect is white, my icon is rounded ! 当我将每个Paint对象的背景设置为白色时,它可以工作,但随后整个Rect都是白色的,我的图标将变为圆形!

You code looks correct, make sure your backgroundImage is not transparent from the beginning. 您的代码看起来正确,请确保您的backgroundImage从一开始就不透明。 As you outlined, there are different resource folders for different screen resolutions, so different files can lead to different results on different devices. 正如您概述的那样,有不同的资源文件夹用于不同的屏幕分辨率,因此不同的文件可以在不同设备上产生不同的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM