Android - 背景透明度也设置 Paint 对象的透明度
Android - Background transparency sets also transparency on Paint object
我正在创建一个自定义视图(扩展 Linearlayout),其背景颜色为:#B3000000(75% 透明度 = B3),现在我需要在这个视图上用全彩色而不是 B3/75% 透明度。
我目前拥有的:
public class MyCustomObject extends LinearLayout {
private Paint paint;
public MyCustomObject(Context context) {
super(context);
init();
}
public MyCustomObject(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MyCustomObject(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public MyCustomObject(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init();
}
private void init() {
View v = View.inflate(getContext(), R.layout.custom_view, this);
paint = new Paint();
paint.setColor(Color.RED);
paint.setAlpha(255);
paint.setStrokeWidth(50);
setWillNotDraw(false);
}
@Override
protected void onDraw(Canvas canvas) {
// super.onDraw(canvas);
canvas.drawLine(0,0,getWidth(),getHeight(),paint);
Timber.e("Drawing...");
}
}
我想在这个视图上画一条红线,此时它画了一条红线,但上面有一个 "black layer",而不是充满活力的红色。
我找到了解决方案here
@Override
protected void dispatchDraw(Canvas canvas) {
// draw here if you want to draw behind children
super.dispatchDraw(canvas);
// draw here if you want to draw over children
}
我正在创建一个自定义视图(扩展 Linearlayout),其背景颜色为:#B3000000(75% 透明度 = B3),现在我需要在这个视图上用全彩色而不是 B3/75% 透明度。
我目前拥有的:
public class MyCustomObject extends LinearLayout {
private Paint paint;
public MyCustomObject(Context context) {
super(context);
init();
}
public MyCustomObject(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MyCustomObject(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public MyCustomObject(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init();
}
private void init() {
View v = View.inflate(getContext(), R.layout.custom_view, this);
paint = new Paint();
paint.setColor(Color.RED);
paint.setAlpha(255);
paint.setStrokeWidth(50);
setWillNotDraw(false);
}
@Override
protected void onDraw(Canvas canvas) {
// super.onDraw(canvas);
canvas.drawLine(0,0,getWidth(),getHeight(),paint);
Timber.e("Drawing...");
}
}
我想在这个视图上画一条红线,此时它画了一条红线,但上面有一个 "black layer",而不是充满活力的红色。
我找到了解决方案here
@Override
protected void dispatchDraw(Canvas canvas) {
// draw here if you want to draw behind children
super.dispatchDraw(canvas);
// draw here if you want to draw over children
}