您的位置:首页 > 移动开发 > Android开发

android 透明的标题逐渐显示

2016-12-09 20:11 671 查看
效果图:



关键代码如下:

public class QQSpeakActivity extends AppCompatActivity implements GradationScrollView.ScrollViewListener{
private GradationScrollView scrollView;

private ListView listView;

private TextView textView;
private int height;
private ImageView ivBanner;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
StatusBarUtil.setImgTransparent(this);
setContentView(R.layout.activity_qqspeak);

scrollView = (GradationScrollView) findViewById(R.id.scrollview);
listView = (ListView) findViewById(R.id.listview);
textView = (TextView) findViewById(R.id.textview);
ivBanner = (ImageView) findViewById(R.id.iv_banner);

ivBanner.setFocusable(true);
ivBanner.setFocusableInTouchMode(true);
ivBanner.requestFocus();

initListeners();
initData();
}

/**
* 获取顶部图片高度后,设置滚动监听
*/
private void initListeners() {

ViewTreeObserver vto = ivBanner.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
textView.getViewTreeObserver().removeGlobalOnLayoutListener(
this);
height = ivBanner.getHeight();

scrollView.setScrollViewListener(QQSpeakActivity.this);
}
});
}

private void initData() {
ArrayAdapter<String> adapter = new ArrayAdapter<String>(QQSpeakActivity.this, android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.data));
listView.setAdapter(adapter);
}

/**
* 滑动监听
* @param scrollView
* @param x
* @param y
* @param oldx
* @param oldy
*/
@Override
public void onScrollChanged(GradationScrollView scrollView, int x, int y,
int oldx, int oldy) {
// TODO Auto-generated method stub
if (y <= 0) {   //设置标题的背景颜色
textView.setBackgroundColor(Color.argb((int) 0, 144,151,166));
} else if (y > 0 && y <= height) { //滑动距离小于banner图的高度时,设置背景和字体颜色颜色透明度渐变
float scale = (float) y / height;
float alpha = (255 * scale);
textView.setTextColor(Color.argb((int) alpha, 255,255,255));
textView.setBackgroundColor(Color.argb((int) alpha, 144,151,166));
} else {    //滑动到banner下面设置普通颜色
textView.setBackgroundColor(Color.argb((int) 255, 144,151,166));
}
}
}

如果想看代码的 点击下载
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: