我想要在Canvas上繪製圖形,使顏色具有可加性。例如,我想產生這樣的:
如何用alpha繪製?
但是,相反,我得到這個:
注意,半白,一半黑的背景是故意的,只是爲了看看阿爾法如何與這兩個交互背景。我很樂意在任何背景下進行這項工作。這裏是我的代碼:
public class VennColorsActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
class VennView extends View {
public VennView(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int alpha = 60, val = 255;
int ar = Color.argb(alpha, val, 0, 0);
int ag = Color.argb(alpha, 0, val, 0);
int ab = Color.argb(alpha, 0, 0, val);
float w = canvas.getWidth();
float h = canvas.getHeight();
float cx = w/2f;
float cy = h/2;
float r = w/5;
float tx = (float) (r * Math.cos(30 * Math.PI/180));
float ty = (float) (r * Math.sin(30 * Math.PI/180));
float expand = 1.5f;
Paint paint = new Paint();
paint.setColor(Color.WHITE);
canvas.drawRect(new Rect(0, 0, (int) w, (int) (h/2)), paint);
PorterDuff.Mode mode = android.graphics.PorterDuff.Mode.ADD;
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColorFilter(new PorterDuffColorFilter(ar, mode));
paint.setColor(ar);
canvas.drawCircle(cx, cy - r, expand * r, paint);
paint.setColorFilter(new PorterDuffColorFilter(ag, mode));
paint.setColor(ag);
canvas.drawCircle(cx - tx, cy + ty, expand * r, paint);
paint.setColorFilter(new PorterDuffColorFilter(ab, mode));
paint.setColor(ab);
canvas.drawCircle(cx + tx, cy + ty, expand * r, paint);
}
}
this.setContentView(new VennView(this));
}
}
有人可以幫助我瞭解如何在Android圖形中添加顏色塗料?
非常感謝Pavel;太快了! – 2012-08-07 02:59:27
順便說一下,你會知道與SDK版本8兼容的PorterDuff.Mode.ADD的替代方案嗎?該文檔說ADD是在第8版中添加的,但它在於。它真的在版本11中引入,並在一些舊的設備上崩潰,而這些設備應該不允許安裝我的小部件。 – 2012-12-15 05:18:10
這太棒了!你可以將這種技術應用於彼此重疊的位圖嗎? – 2013-05-15 20:46:27