在Android View 中, 要制作阴影,
可以利用 Paint 物件的 setShadowLayer 函数.
Java 程式码范例 :
public class helloWorld extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView( new drawCanvas(this) );
}
class drawCanvas extends View {
public drawCanvas(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// 建立Paint 物件
Paint vPaint = new Paint();
Paint vPaint2 = new Paint();
// --------------------------------------------
// 设定颜色
vPaint.setColor(0xFFFFFF00);
// 实心矩形
canvas.drawRect( 30
, 50
, 130
, 150
, vPaint
);
// 设定阴影(柔边, X 轴位移, Y 轴位移, 阴影颜色)
vPaint .setShadowLayer (5, 3, 3, 0xFFFF00FF);
// 实心矩形& 其阴影
canvas.drawRect( 30
, 200
, 130
, 300
, vPaint
);
// --------------------------------------------
// 设定颜色
vPaint2.setColor(0xFFFFFF00);
// 空心
vPaint2 .setStyle(Style.STROKE);
// 空心矩形
canvas.drawRect( 200
, 50
, 300
, 150
, vPaint2
);
// 设定阴影(柔边, X 轴位移, Y 轴位移, 阴影颜色)
vPaint2 .setShadowLayer (5, 3, 3, 0xFFFF00FF);
// 空心矩形& 其阴影
canvas.drawRect( 200
, 200
, 300
, 300
, vPaint2
);
}
}
}