我有以下layout.xml
<LinearLayout
android:id="@+id/layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/image"
android:layout_width="100dp"
android:layout_height="50dp"
android:src="@drawable/logo"/>
...
</LinearLayout>我将父LinearLayout的背景颜色设置如下:
LinearLayout layout = (LinearLayout) view.findViewById(R.id.layout);
layout.setBackgroundColor(Color.parseColor("#727272"));
layout.setAlpha(0.5f);但是,图像(id:ImageView )是有色的。有没有一种方法可以在设置ImageView的父布局的背景颜色时不对其着色?
发布于 2016-06-15 01:21:26
因为我调用了父布局的setAlpha,所以图像是有色的:
layout.setAlpha(0.5f);但是,我需要为父布局设置50%的不透明度。所以我使用下面的方法来设置父布局的背景颜色Alpha和RGB,它是有效的:父布局的背景颜色是在不对孩子的图像进行着色的情况下设置的。
LinearLayout layout = (LinearLayout) view.findViewById(R.id.layout);
layout.setBackgroundColor(BitmapDrawableUtility. getColorByOpacityPercentage(calculateOpacity(Color.parseColor("#727272"), 50));
public static int getColorByOpacityPercentage(int color, double percentage) {
int transparency = (int) Math.round((percentage / 100.0) * 255.0);
return Color.argb(transparency, Color.red(color), Color.green(color), Color.blue(color));
}https://stackoverflow.com/questions/37801784
复制相似问题