滑动前:
滑动中:
滑动后
1.自定义MyScrollView
package com.jukopro.titlebarcolor;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.ScrollView;
/**
* 自定义带滚动监听的scrollview
*
*/
public class MyScrollView extends ScrollView {
/**
* 接口回调
*/
private ScrollViewListener scrollViewListener = null;
public interface ScrollViewListener {
void onScrollChanged(ObservableScrollView scrollView, int x, int y,
int oldx, int oldy);
}
public void setScrollViewListener(ScrollViewListener scrollViewListener) {
this.scrollViewListener = scrollViewListener;
}
/**
* 重写的onScrollChanged方法监听坐标
* 这个监听在源码中没有写成回调的样子,
* 只是写成了方法的样子,由于修饰这个方法的修饰符是protected,
*(protected只能在本类,子类,同一包中调用),
* 所以拿到ScrollView对象后在无法activity中调用此方法,
* 所以只能重写后,子类中自动调用,
* 所以要想在activity调用,
* 就要写回调,
* 上面就是我写的回调
* 在Android源码中这种写法很多,在很多控件中都有
*/
@Override
protected void onScrollChanged(int x, int y, int oldx, int oldy) {
super.onScrollChanged(x, y, oldx, oldy);
if (scrollViewListener != null) {
scrollViewListener.onScrollChanged(this, x, y, oldx, oldy);
}
}
/**
*构造方法(在这里没用)
*/
public MyScrollView(Context context) {
super(context);
}
public MyScrollView(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
}
public MyScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
}
2.MainActivity
package com.jukopro.titlebarcolor;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.jukopro.titlebarcolor.ObservableScrollView.ScrollViewListener;
public class MainActivity extends Activity {
private MyScrollView scrollView;
private ImageView imageView;
private TextView textView;
private int imageHeight;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scrollView = (MyScrollView) findViewById(R.id.scrollview);
imageView = (ImageView) findViewById(R.id.imageview);
textView = (TextView) findViewById(R.id.textview);
initListeners();
}
private void initListeners() {
// 获取顶部图片高度后,设置滚动监听
ViewTreeObserver vto = imageView.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
imageView.getViewTreeObserver().removeGlobalOnLayoutListener(
this);
imageHeight = imageView.getHeight();
scrollView.setScrollViewListener(new ScrollViewListener() {
@Override
public void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy) {
// TODO Auto-generated method stub
// Log.i("TAG", "y--->" + y + " height-->" + height);
if (y <= 0) {
// 设置文字背景颜色,白色
textView.setBackgroundColor(Color.argb((int) 0, 255, 255, 255));//AGB由相关工具获得,或者美工提供
// 设置文字颜色,黑色
textView.setTextColor(Color.argb((int) 0, 255, 255, 255));
Log.e("111","y <= 0");
} else if (y > 0 && y <= imageHeight) {
float scale = (float) y / imageHeight;
float alpha = (255 * scale);
// 只是layout背景透明(仿知乎滑动效果)白色透明
textView.setBackgroundColor(Color.argb((int) alpha, 255, 255, 255));
// 设置文字颜色,黑色,加透明度
textView.setTextColor(Color.argb((int) alpha, 0, 0, 0));
Log.e("111","y > 0 && y <= imageHeight");
} else {
// 白色不透明
textView.setBackgroundColor(Color.argb((int) 255, 255, 255, 255));
// 设置文字颜色
//黑色
textView.setTextColor(Color.argb((int) 255, 0, 0, 0));
Log.e("111","else");
}
}
});
}
});
}
}
3.activity_main布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >
<com.jiyaruo.titlebarcolor.MyScrollView
android:id="@+id/scrollview"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<ImageView
android:id="@+id/imageview"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="@mipmap/msvzs" />
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="话不多说我中意你"
android:gravity="center"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="话不多说我中意你"
android:gravity="center"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="话不多说我中意你"
android:gravity="center"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="话不多说我中意你"
android:gravity="center"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="话不多说我中意你"
android:gravity="center"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="话不多说我中意你"
android:gravity="center"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="话不多说我中意你"
android:gravity="center"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="话不多说我中意你"
android:gravity="center"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="话不多说我中意你"
android:gravity="center"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="话不多说我中意你"
android:gravity="center"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="话不多说我中意你"
android:gravity="center"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="话不多说我中意你"
android:gravity="center"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="话不多说我中意你"
android:gravity="center"
/>
</LinearLayout>
</com.jiyaruo.titlebarcolor.MyScrollView>
<TextView
android:id="@+id/textview"
android:layout_width="match_parent"
android:layout_height="48dp"
android:gravity="center"
android:text="我是标题"
android:textSize="18sp"
android:textColor="#ffffff"
android:background="#00000000" />
</RelativeLayout>