正如視頻中提到的那樣,您可以使用Canvas#saveLayerAlpha(....)
這個。您也可以在不使用它的情況下獲得類似的效果。我會在稍後討論。
讓我們創建一個示例視圖:
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();
}
}
會發生什麼:saveLayerAlpha(....)
被稱爲
脫屏位圖進行分配。
所有繪圖操作都發生在這個位圖上。
當調用canvas.restore()
時,會將此位圖傳輸到屏幕畫布上,並將我們在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);
}
}
}
輸出:

只是參考,上圖中的基礎容器是LinearLayout
,背景設置爲此jpeg:Link。
而且,用作SampleView的背景中的繪製:here:
// Just a random image to 'see' the difference
setBackground(getResources().getDrawable(R.drawable.hor_lines));
取自。
你確定它是你想改變的alpha嗎?似乎你想要的效果仍然是零透明的,它只是減輕了。 – Scott
是的,我需要透明度,因爲我正在使用必須看到的紋理背景。 – EGHDK
我的最佳想法是看看是否有方法將重疊形狀繪製爲一個形狀(或在繪製後將它們合併爲一個形狀)。將Alpha添加到複合形狀應該是他們想要的方式 – Scott