Android 可透過 Paint 物件, 來設定 Bitmap 透明度.
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);
// 取得 Resource 圖片的 Bitmap
Bitmap vBitmap = BitmapFactory.decodeResource( this.getResources()
, R.drawable.icon
);
// 建立 Paint 物件
Paint vPaint = new Paint();
vPaint.setStyle( Paint.Style.STROKE ); // 空心
vPaint.setAlpha( 75 ); // Bitmap 透明度 (0 ~ 100)
canvas.drawBitmap( vBitmap, 50, 100, null ); // 無透明
canvas.drawBitmap( vBitmap, 50, 200, vPaint ); // 有透明
}
}
}
範例結果 :