android 监听标题栏颜色渐变

 创建scrollView监听类接口

public interface ScrollListener {
    void onScrollChanged(ScrollListenerView scrollView, int x, int y, int oldX, int oldY);
}

创建继承于scrollView的自定义view

public class ScrollListenerView extends ScrollView {
    private ScrollListener scrollViewListener = null;

    public ScrollListenerView(Context context) {
        super(context);
    }

    public ScrollListenerView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public ScrollListenerView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void setScrollListener(ScrollListener scrollViewListener) {
        this.scrollViewListener = scrollViewListener;
    }

    @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);
        }
    }
}

在layout.xml文件中引用自定义scrollView

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <Button
        android:id="@+id/title_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="标题栏"/>
    <clan.yuanxin.com.myditukaifa.ScrollListenerView
        android:id="@+id/slv"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:background="@color/colorAccent"></LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:background="@color/colorPrimary"></LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:background="@color/colorAccent"></LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:background="@color/colorPrimaryDark"></LinearLayout>

        </LinearLayout>
    </clan.yuanxin.com.myditukaifa.ScrollListenerView>

</LinearLayout>

小工具类 ColorUtils

public class ColorUtils {


    /**
     * 将十六进制 颜色代码 转换为 int
     *
     * @return
     */
    public static int HextoColor(String color) {

        // #ff00CCFF
        String reg = "#[a-f0-9A-F]{8}";
        if (!Pattern.matches(reg, color)) {
            color = "#00ffffff";
        }

        return Color.parseColor(color);
    }

    /**
     * 修改颜色透明度
     * @param color
     * @param alpha
     * @return
     */
    public static int changeAlpha(int color, int alpha) {
        int red = Color.red(color);
        int green = Color.green(color);
        int blue = Color.blue(color);

        return Color.argb(alpha, red, green, blue);
    }


}

在activity中使用,实现监听标题栏颜色渐变

public class MyServiceTestActivity extends Activity implements ScrollListener{

    private MyCONNCTION myCONNCTION;
    private Button button;
    private ScrollListenerView slv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.servicetest);
        button = (Button)findViewById(R.id.title_button);

        myCONNCTION= new MyCONNCTION();
        button.setOnClickListener(new MyoCLIC());
        slv = (ScrollListenerView)findViewById(R.id.slv);
        slv.setScrollListener(this);
    }
    class MyCONNCTION implements ServiceConnection {

        @Override
        public void onServiceConnected(ComponentName arg0, IBinder arg1) {
            // TODO Auto-generated method stub
            Myserviec.MyBinder binder = (Myserviec.MyBinder)arg1;
            Myserviec myserviec= binder.getservice();
            myserviec.aa();
        }

        @Override
        public void onServiceDisconnected(ComponentName arg0) {
            // TODO Auto-generated method stub

        }

    }
    class MyoCLIC implements View.OnClickListener {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Intent intent = new Intent("ccss");
            MyServiceTestActivity.this.bindService(intent, myCONNCTION, Context.BIND_AUTO_CREATE);

        }

    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        return true;
    }

    @Override
    public void onScrollChanged(ScrollListenerView scrollView, int x, int h, int oldX, int oldY) {
        if (h > 0) {
            button.setBackgroundResource(R.color.colorPrimary);
        }
        float f = (h + 0f) / 350;
        if (f > 1) {
            f = 1f;
        }
        if (f < 0) {
            f = 0;
        }
        button.setBackgroundColor(ColorUtils.changeAlpha(ContextCompat.getColor(MyServiceTestActivity.this, R.color.colorPrimary),(int)(f * 1 * 0xff)));

    }
}
ColorUtils

猜你喜欢

转载自blog.csdn.net/meixi_android/article/details/78665242