android - 如何将viewpager的图片与状态栏沉侵
巴扎黑
巴扎黑 2017-04-17 17:39:21
0
1
548

如何将viewpager的图片与状态栏沉侵,这该如何做呢???我这里的viewpager不是全屏的,而是占屏幕的1/4.

巴扎黑
巴扎黑

reply all(1)
阿神

I happened to make a semi-finished app before, and the homepage used it just to achieve the main effect of the question. I will post the link first:
Click here, there are comments inside

StatusBarUtil.setTransparent(MainActivity.this);
//然后动态的改变Activity的背景就可以实现了

I’m here to update:
Let’s look at the layout first (give me an example here):

<FrameLayout
    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"
    android:background="@drawable/bg_saber_q"
    tools:context="didikee.com.demoapk.activity.StatuBarActivity">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <FrameLayout
            android:id="@+id/bg_main"
            android:layout_width="match_parent"
            android:layout_height="400dp"
            android:background="@drawable/meinv">
        //我要把这个 meinv 变为沉浸状态栏,注意层级
        </FrameLayout>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:textSize="50sp"
            android:textColor="@color/orange"
            android:gravity="center"
            android:text="哈哈哈"/>
    </LinearLayout>
</FrameLayout>

Then modify the method in StatusBarUtil (note the level):

private static void setRootView(Activity activity) {
        ViewGroup rootView = (ViewGroup) ((ViewGroup) activity.findViewById(android.R.id.content)).getChildAt(0);

        ViewGroup childAt = (ViewGroup) rootView.getChildAt(0);
        ViewGroup childAt1 = (ViewGroup) childAt.getChildAt(0);
        //childAt1 这个就是有美女背景图的那个FrameLayout,也是ViewGroup嘛
        childAt1.setFitsSystemWindows(true);
        childAt1.setClipToPadding(true);
    }

Demo picture:

It’s up to you to fine-tune the specific effects of your project

-----Update again:
The carousel has OnPageChangeListener(); dynamic settings and it’s okOnPageChangeListener();动态的设置就可以了

/ 2016-10-20 更新答案 */

PS:可能题主已经走了,但是还是写下一下更新,今天碰到类似的需求

  1. Activity 要继承 AppCompatActivity.
    2.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.xxxx);
        setBarStyle();
        init();
    }
    public void setBarStyle() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            // 设置状态栏透明
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
    }
    //.....
  1. 这个时候你的状态栏你就可以使用了.你可以在状态栏填充一个View然后按需求改变这个View的背景透明度

我的 是用ScrollView

/ 2016-10-20 Updated answer */


PS: Maybe the author of the question has left, but I still write an update. I encountered a similar need today

  1. 🎜Activity should inherit from AppCompatActivity.🎜2.🎜
mSScrollView.setOnScrollListener(new SScrollView.OnScrollChangedListener() {
            @Override
            public void onScrollChanged(int x, int y, int oldX, int oldY) {
                //状态栏透明度回调
                final int height = mFLViewpagerHeight - mTitleHeight;
                if (y <= 0) {   //设置标题的背景颜色
                    mVGTitle.setBackgroundColor(Color.argb((int) 0, 255,255,255));
                    mTvTitleMiddle.setTextColor(Color.argb((int) 0, 46,46,46));
                    //这是填充在状态栏的View
                    mStatusBar.setBackgroundColor(Color.argb((int) 0, 204,204,204));

                    mIvTitleLeft.setImageResource(R.drawable.ic_arrow_left_white);
                    mIvTitleShare.setImageResource(R.drawable.ic_share_pure);
                } else if (y > 0 && y <= height) { //滑动距离小于banner图的高度时,设置背景和字体颜色颜色透明度渐变
                    float scale = (float) y / height;
                    float alpha = (255 * scale);
                    mTvTitleMiddle.setTextColor(Color.argb((int) alpha, 46,46,46));
                    mVGTitle.setBackgroundColor(Color.argb((int) alpha, 255,255,255));
                    mStatusBar.setBackgroundColor(Color.argb((int) alpha, 204,204,204));

                    mIvTitleLeft.setImageResource(R.drawable.ic_arrow_left_white);
                    mIvTitleShare.setImageResource(R.drawable.ic_share_pure);
                } else {    //滑动到banner下面设置普通颜色
                    mVGTitle.setBackgroundColor(Color.argb((int) 255, 255,255,255));
                    mTvTitleMiddle.setTextColor(Color.argb((int) 255, 46,46,46));
                    mStatusBar.setBackgroundColor(Color.argb((int) 255, 204,204,204));
                    mIvTitleLeft.setImageResource(R.drawable.ic_left_arrow_dark);
                    mIvTitleShare.setImageResource(R.drawable.ic_share_dark);

                }
  1. 🎜Now you can use your status bar. You can fill the status bar with a View and then change the background transparency of this View as needed🎜
  2. ol> 🎜I used ScrollView to do it, so I monitored the scrolling distance and posted my processing: 🎜 rrreee 🎜I don’t know if this can play videos. I recorded a demo using studio. Let’s see if it can achieve your effect. It’s too slow to upload screenshots🎜http://oahzrw11n.bkt.clouddn....🎜
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template