Tôi muốn vẽ đồ họa lên Canvas sao cho màu sắc là phụ gia. Ví dụ, tôi muốn sản xuất này:
Làm thế nào để vẽ bằng alpha?
Nhưng thay vào đó, tôi có được điều này:
Lưu ý rằng nửa trắng, nền nửa đen là cố ý, chỉ để xem làm thế nào alpha tương tác với cả hai hình nền. Tôi sẽ rất vui khi được làm việc này với một trong hai nền tảng. Đây là mã của tôi:
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));
}
}
Ai đó có thể vui lòng giúp tôi hiểu cách vẽ bằng màu phụ gia trong đồ họa Android?
Cảm ơn rất nhiều, Pavel; và nhanh quá! –
BTW, bạn có biết một giải pháp thay thế cho PorterDuff.Mode.ADD tương thích với SDK phiên bản 8 không? Tài liệu nói rằng ADD đã được thêm vào trong phiên bản 8 nhưng nó nằm. Nó đã thực sự được giới thiệu trong phiên bản 11 và treo trên một số thiết bị cũ mà không nên được phép cài đặt tiện ích của tôi. –
Điều này thật tuyệt!Bạn có thể áp dụng kỹ thuật này vào các ảnh bitmap chồng lên nhau không? –