android 标题栏颜色渐变和阴影,ScrollView上下滑动监听,及判断scrollView是否滚动到底部
生活随笔
收集整理的這篇文章主要介紹了
android 标题栏颜色渐变和阴影,ScrollView上下滑动监听,及判断scrollView是否滚动到底部
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1、創建 ?ScrollListener 接口監聽滑動距離
public interface ScrollListener {void onScrollChanged(ScrollListenerView scrollView, int x, int y, int oldX, int oldY); }?
view陰影屬性
android:elevation="4dp"2、重寫 ScrollView? 自定義? ScrollListenerView
?
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;}@Overrideprotected 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);}} }?
判斷scrollView是否滾動到底部方法
@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);}if (getScrollY() + getHeight() >= computeVerticalScrollRange()) {Log.d("lgq", "------滾動到最下方------");} else {Log.d("Lgq", "沒有到最下方");} }?
3、在layout xml文件使用ScrollListenerView
<com.tianxinyw.mapclient.views.ScrollListenerViewandroid:id="@+id/slv"android:layout_width="match_parent"android:layout_height="match_parent"android:scrollbars="none"><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"> </RelativeLayout></com.tianxinyw.mapclient.views.ScrollListenerView>上下層標題漸變色布局,titlelitwo是下層
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:id="@+id/activity_popup"android:orientation="vertical"><LinearLayoutandroid:id="@+id/titlelitwo"android:layout_width="match_parent"android:layout_height="38dp"android:background="@color/homeiconokc"android:gravity="center"android:orientation="horizontal"><TextViewandroid:id="@+id/titletetwo"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="天鑫計費2"android:textColor="@color/white"android:textSize="16dp"android:textStyle="bold"/></LinearLayout><LinearLayoutandroid:id="@+id/lishititleli"android:layout_width="match_parent"android:layout_height="38dp"android:background="@color/homeiconokc"android:gravity="center"android:orientation="horizontal"><TextViewandroid:id="@+id/lishititlete"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="天鑫計費"android:textColor="@color/white"android:textSize="16dp"android:textStyle="bold"/></LinearLayout>?
?
4、Activity 實現滑動距離監聽接口
public class HomeFragment extends BaseFragment implements ScrollListener{初始化?
@BindView(R.id.slv) ScrollListenerView slv;設置監聽
slv.setScrollListener(this);實現方法:
@Override public void onScrollChanged(ScrollListenerView scrollView, int x, int y, int oldX, int oldY) {if (y - oldY > 0&&y>0) {float f = (y + 0f) / 400;//滑動距離350pxif (f > 1) {f = 1f;}if (f < 0) {f = 0;}if (f > 0 && f < 0.3) {f = (float) 0.333;}Log.i("lgq", "......向上滑。。。" + f + ".......y====" + y);lishititleli.setBackgroundColor(Utils.changeAlpha(ContextCompat.getColor(getActivity(), R.color.homeiconokc), (int) (1 - f * 1 * 0xff)));lishititlete.setTextColor(Utils.changeAlpha(ContextCompat.getColor(getActivity(), R.color.white), (int) (1 - f * 1 * 0xff)));titlelitwo.setBackgroundColor(Utils.changeAlpha(ContextCompat.getColor(getActivity(), R.color.homeiconokc), (int) (f * 1 * 0xff)));titletetwo.setTextColor(Utils.changeAlpha(ContextCompat.getColor(getActivity(), R.color.white), (int) (f * 1 * 0xff)));} else if (y-oldY<0&&y>=0){float f = (y + 0f) / 400;//滑動距離350pxif (f > 1) {f = 1f;}if (f < 0) {f = 0;}Log.e("lgq", "......向xia滑。。。" + f + ".......y====" + y + "......zhi====" + (1 - f * 1 * 0xff) + ".....yy===" + f * 1 * 0xff);if (y <= 0||f<0.01) {lishititleli.setBackgroundColor(Utils.changeAlpha(ContextCompat.getColor(getActivity(), R.color.homeiconokc), (int) (1 * 1 * 0xff)));lishititlete.setTextColor(Utils.changeAlpha(ContextCompat.getColor(getActivity(), R.color.white), (int) (1 * 1 * 0xff)));return;}lishititleli.setBackgroundColor(Utils.changeAlpha(ContextCompat.getColor(getActivity(), R.color.homeiconokc), (int) (1 - f * 1 * 0xff)));lishititlete.setTextColor(Utils.changeAlpha(ContextCompat.getColor(getActivity(), R.color.white), (int) (1 - f * 1 * 0xff)));titlelitwo.setBackgroundColor(Utils.changeAlpha(ContextCompat.getColor(getActivity(), R.color.homeiconokc), (int) (f * 1 * 0xff)));titletetwo.setTextColor(Utils.changeAlpha(ContextCompat.getColor(getActivity(), R.color.white), (int) (f * 1 * 0xff)));}}工具方法:
/*** 修改顏色透明度* @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); }?
實現效果:向上滑動漸變顏色,顯示隱藏view
?
?
demo鏈接:https://download.csdn.net/download/meixi_android/10966003
?
?
附:左滑右滑手勢
//漸變色float x1 = 0;float x2 = 0;private int nowpersion = 0;@Overridepublic boolean dispatchTouchEvent(MotionEvent ev) {if (ev.getAction() == MotionEvent.ACTION_DOWN) { // Log.i("lgq","ssssMainActivity_dispatchTouchEvent==="+ev.getX());x1 = ev.getX();}return super.dispatchTouchEvent(ev);}@Overridepublic boolean onTouch(View v, MotionEvent event) {if (event.getAction() == MotionEvent.ACTION_MOVE) {x2 = event.getX();float h = x1 - x2;// Log.i("lgq","eee==滑動距離aaa===="+(h)+"......"+nowpersion+"...."+x1);float f = (h + 0f) / 640;//滑動距離350pxif (f > 1) {f = 1f;}if (f < 0) {f = 0;}if (nowpersion == 0 && h < 0) {return false;} else if (nowpersion == 0 && h > 0) {if (f < 0.4) {return false;}TextView textView = tabLayout.getTabAt(1).getCustomView().findViewById(R.id.tab_iv);TextView textView0 = tabLayout.getTabAt(0).getCustomView().findViewById(R.id.tab_iv);textView.setTextColor(changeAlpha(ContextCompat.getColor(MainActivity.this, R.color.textlan), (int) (f * 1 * 0xff)));textView0.setTextColor(changeAlpha(ContextCompat.getColor(MainActivity.this, R.color.texthui), (int) (f * 1 * 0xff)));}}return false;}/*** 修改顏色透明度* @param color* @param alpha* @return*/public 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);}總結
以上是生活随笔為你收集整理的android 标题栏颜色渐变和阴影,ScrollView上下滑动监听,及判断scrollView是否滚动到底部的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 达梦数据库图形化工具
- 下一篇: 【DS3231 RTC实时时钟模块与Ar