我正在尝试创建反射阴影并发现一个问题。
请在我的自定义视图中找到以下代码:
@Override
protected void onDraw(Canvas canvas) {
//prepare
canvas.drawColor(Color.GRAY);
Bitmap source = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
//First Column
canvas.drawBitmap(source, 0, 0, new Paint());
//2nd Column
canvas.drawBitmap(source, source.getWidth(), 0, new Paint());
//Reflection
Matrix matrix = new Matrix();
matrix.preScale(1.0f, -1.0f);
matrix.postTranslate(source.getWidth(), source.getHeight()*2);
canvas.drawBitmap(source, matrix, new Paint());
Paint paint2 = new Paint();
LinearGradient shader = new LinearGradient(
source.getWidth()*3/2,
source.getHeight(),
source.getWidth()*3/2,
source.getHeight()*2,
0x7FFFFFFF, 0x00FFFFFF, TileMode.CLAMP);
paint2.setShader(shader);
paint2.setXfermode(new PorterDuffXfermode(
android.graphics.PorterDuff.Mode.DST_IN));
canvas.drawRect(
source.getWidth(),
source.getHeight(),
source.getWidth()*2,
source.getHeight()*2,
paint2);
//3rd Column
Bitmap bitmap = Bitmap.createBitmap(source.getWidth(), source.getHeight()*2, Config.ARGB_8888);
Canvas canvas2 = new Canvas(bitmap);
canvas2.drawBitmap(source, 0, 0, new Paint());
matrix = new Matrix();
matrix.preScale(1.0f, -1.0f);
matrix.postTranslate(0, source.getHeight()*2);
canvas2.drawBitmap(source, matrix, new Paint());
paint2 = new Paint();
shader = new LinearGradient(
source.getWidth()*1/2,
source.getHeight(),
source.getWidth()*1/2,
source.getHeight()*2,
0x7FFFFFFF, 0x00FFFFFF, TileMode.CLAMP);
paint2.setShader(shader);
paint2.setXfermode(new PorterDuffXfermode(
android.graphics.PorterDuff.Mode.DST_IN));
canvas2.drawRect(
0,
source.getHeight(),
source.getWidth(),
source.getHeight()*2,
paint2);
canvas.drawBitmap(bitmap, source.getWidth()*2,0, new Paint());
}
我在画布上做同样的事情(我从onDraw(canvas)
)和canvas2(使用创建new Canvas(bitmap)
)
But, both drawing different shader effect as follows:
为什么着色器效果不同?