使用View 的 setAlpha(int alpha);
代码
setContentView(R.layout.activity_alpha_view);
SeekBar seekBar = (SeekBar) findViewById(R.id.seekbar);
seekBar.setMax(100);
final LinearLayout ll = (LinearLayout) findViewById(R.id.rl);
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
ll.setAlpha(progress * 1.0f / 100);
}
});
xml:
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#333333">
android:id="@+id/seekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
android:id="@+id/rl"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#f0f0f0">
其实就是将当前view 的透明度放低, 从而显示出ViewGroup的背景颜色