[英]How to make background transparent in android
我在drawable文件夾中有兩個圖像,我正在這樣做:
image = BitmapFactory.decodeResource(getResources(), R.drawable.image1);
background = BitmapFactory.decodeResource(getResources(), R.drawable.image2);
canvas.drawBitmap(background,0, 0, null);
canvas.drawBitmap(image, x, y, null);
我的圖片中有一個名為“圖像”的白色背景,但是我只想顯示該圖片的對象,這意味着要使背景透明。 我怎樣才能做到這一點 ?
您可以在繪制圖像之前使畫布透明。
canvas.drawColor(Color.TRANSPARENT);
您始終可以將“透明”顏色用作背景,該顏色與任何可繪制對象的設置相同,只需使用@android:color/transparent
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/myimage"
android:alpha=".75"/>
Programatically, you can also set it with
int alphaAmount = 125; // some value 0-255 where 0 is fully transparent and 255 is fully opaque
myImage.setAlpha(alphaAmount);
您可以創建一個透明的drawable
並將其作為View
的背景。
Android使用十六進制ARGB值,其格式為#AARRGGBB。 第一對字母AA代表Alpha Channel。 要設置alpha值,必須將十進制不透明度值轉換為十六進制值。
這是十六進制不透明度值
如果圖像具有白色背景,則可以將setAlpha()用於可繪制。 但是最好的方法是您應該使用沒有背景的圖像。
范例:
imageview.getDrawable().setAlpha(60);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.