gpt4 book ai didi

java - Android 自定义 View 不以正确的方式处理透明度/alpha

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:01:05 30 4
gpt4 key购买 nike

我正在绘制自定义 View 。在此 View 中,我使用两个不同的绘画和路径对象在 Canvas 上绘画。我基本上是在绘制两个重叠的形状。添加 alpha 后,重叠的 View 部分比图像的其余部分更暗。这是不受欢迎的,但我不确定如何解决它。

这是我的代码剪辑,展示了我如何在我的 NewButtonView.java 中使用 alpha

Paint paint = new Paint();
int color = 0x33ffffff;
int borderColor = 0xFF000000;

paint.setColor(color);
paint.setAntiAlias(true);
paint.setStrokeWidth(strokeWidth);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeCap(Paint.Cap.ROUND);
paint.setStyle(Paint.Style.FILL);

大约 31 分钟 Google I/O video ...他们显示了我想要的效果。

他们基本上展示了这张图片: enter image description here

添加透明度并获得此图像:不希望的结果

enter image description here

他们最终得到的是:期望的结果

enter image description here

有没有人知道如何获得这种想要的效果?

最佳答案

如视频中所述,您将为此使用 Canvas#saveLayerAlpha(....)。您也可以在不使用它的情况下获得类似的效果。我稍后会讨论这个问题。

让我们创建一个示例 View :

public class SampleView extends View {

// Declare Paint objects
Paint paintColor, paintBorder;

public SampleView(Context context) {
super(context);

// Initialize and set up Paint objects
paintColor = new Paint();
paintBorder = new Paint();

paintColor.setAntiAlias(true);
paintBorder.setAntiAlias(true);

paintBorder.setColor(Color.BLACK);
paintBorder.setStyle(Style.STROKE);
paintBorder.setStrokeWidth(10);

// Just a random image to 'see' the difference
setBackground(getResources().getDrawable(R.drawable.hor_lines));
}

@Override
protected void onDraw(Canvas canvas) {

// Save layer alpha for Rect that covers the view : alpha is 90 / 255
canvas.saveLayerAlpha(0, 0, getWidth(), getHeight(), 90,
Canvas.HAS_ALPHA_LAYER_SAVE_FLAG);

// Draw first circle, and then the border
paintColor.setColor(Color.RED);
canvas.drawCircle(getWidth() / 3, getHeight() / 2,
getWidth() / 4 - 20, paintColor);

canvas.drawCircle(getWidth() / 3, getHeight() / 2,
getWidth() / 4 - 15, paintBorder);

// Draw second circle, and then the border
paintColor.setColor(Color.BLUE);
canvas.drawCircle(2 * getWidth() / 3, getHeight() / 2,
getWidth() / 4 - 20, paintColor);
canvas.drawCircle(2 * getWidth() / 3, getHeight() / 2,
getWidth() / 4 - 15, paintBorder);

// Finally, restore the canvas
canvas.restore();
}
}

发生了什么:

  1. 在调用 saveLayerAlpha(....) 时分配屏幕外位图。

  2. 所有绘图操作都发生在这张位图上。

  3. canvas.restore() 被调用时,这个位图被转移到屏幕 Canvas 上,我们在 saveLayerAlpha(....) 中提供的 alpha 值 应用于离屏位图。

(我认为)以下是在不使用 saveLayerAlpha(....) 的情况下创建此效果的等效方法:

public class SView extends View {

Paint paintColor, paintBorder, paintAlpha;

Bitmap toDrawOn;

public SView(Context context) {
super(context);

paintAlpha = new Paint();

paintAlpha.setColor(Color.parseColor("#90FFFFFF"));
paintAlpha.setAntiAlias(true);

....
....

}

@Override
protected void onDraw(Canvas canvas) {

if (toDrawOn == null) {

// Create a new Bitmap
toDrawOn = Bitmap.createBitmap(getWidth(), getHeight(),
Config.ARGB_8888);

// Create a new Canvas; drawing operations
// will happen on 'toDrawOn'
Canvas offScreen = new Canvas(toDrawOn);

// First circle
paintColor.setColor(Color.RED);
offScreenCanvas.drawCircle(getWidth() / 3, getHeight() / 2,
getWidth() / 4 - 20, paintColor);
offScreenCanvas.drawCircle(getWidth() / 3, getHeight() / 2,
getWidth() / 4 - 15, paintBorder);

// Second circle
paintColor.setColor(Color.BLUE);
offScreenCanvas.drawCircle(2 * getWidth() / 3, getHeight() / 2,
getWidth() / 4 - 20, paintColor);
offScreenCanvas.drawCircle(2 * getWidth() / 3, getHeight() / 2,
getWidth() / 4 - 15, paintBorder);

// Draw bitmap 'toDrawOn' to canvas using 'paintAlpha'
canvas.drawBitmap(toDrawOn, 0, 0, paintAlpha);

} else {

// 'toDrawOn' is not null; draw it
canvas.drawBitmap(toDrawOn, 0, 0, paintAlpha);
}
}
}

输出:

enter image description here

仅供引用,上图中的基础容器是一个 LinearLayout,背景设置为这个 jpeg:Link .

并且,用作 SampleView 背景的 drawable:

// Just a random image to 'see' the difference
setBackground(getResources().getDrawable(R.drawable.hor_lines));

取自:here .

关于java - Android 自定义 View 不以正确的方式处理透明度/alpha,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20254558/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com