Zip*_*ppy 7 graphics png android colors drawable
我正在写一个有基本精灵(气球)的游戏 - 目前我有2个不同颜色的气球PNG文件,我已创建,我需要创建更多(可能是另外5个左右)并且不希望有7个不同的png文件.(这将是20个额外的文件,因为我有4个不同的尺寸用于缩放目的)我宁愿坚持1 - 我现在拥有的是黄色和红色(几乎可靠,但不完全 - 他们有详细信息).
问题 - 是否有一种简单的方法可以改变现有PNG文件的颜色?我见过人们提setColor和setColorFilter,但我不能工作了如何使用这些.这些甚至可以用于已经有颜色的PNG文件,还是仅用于白色PNG文件(我不认为我的PNG实际上只是白色)?
谢谢大家,任何帮助将不胜感激.
Nit*_*thi 11
你可以使用黑色气球png文件来创建不同颜色的气球.
下面的代码使用一些奇特的混合模式技巧来设置颜色.
protected BitmapDrawable setIconColor(int color) {
if (color == 0) {
color = 0xffffffff;
}
final Resources res = getResources();
Drawable maskDrawable = res.getDrawable(R.drawable.actionbar_icon_mask);
if (!(maskDrawable instanceof BitmapDrawable)) {
return;
}
Bitmap maskBitmap = ((BitmapDrawable) maskDrawable).getBitmap();
final int width = maskBitmap.getWidth();
final int height = maskBitmap.getHeight();
Bitmap outBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(outBitmap);
canvas.drawBitmap(maskBitmap, 0, 0, null);
Paint maskedPaint = new Paint();
maskedPaint.setColor(color);
maskedPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP));
canvas.drawRect(0, 0, width, height, maskedPaint);
BitmapDrawable outDrawable = new BitmapDrawable(res, outBitmap);
return outDrawable;
}
Run Code Online (Sandbox Code Playgroud)
小智 3
您可以尝试使用随机 RGB 值定义自定义 ColorMatrix:
Random rand = new Random();
int r = rand.nextInt(256);
int g = rand.nextInt(256);
int b = rand.nextInt(256);
ColorMatrix cm = new ColorMatrix();
cm.set(new float[] {
1, 0, 0, 0, r,
0, 1, 0, 0, g,
0, 0, 1, 0, b,
0, 0, 0, 1, 0 }); // last line is antialias
paint.setColorFilter(new ColorMatrixColorFilter(cm));
canvas.drawBitmap(myBitmap, toX, toY, paint);
Run Code Online (Sandbox Code Playgroud)
我希望它有帮助。
| 归档时间: |
|
| 查看次数: |
4209 次 |
| 最近记录: |