Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change the transparency of the status bar to using ImageView as the title view page #280

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,15 @@ A util for setting status bar style on Android App. It can work above API 19(Kit

![](img/set_transparent.png)

- Change the transparency of the status bar to using ImageView as the title view page

```java
StatusBarUtil.setStatusColorForImageView(Activity activity, @ColorInt int color, int colorAlpha);
StatusBarUtil.setColorWithAlpha(Activity activity, @ColorInt int color, int colorAlpha);
```

![](img/change_status_bar_for_imageview.gif)

- Set status bar color for `DrawerLayout`

```java
Expand Down
Binary file added img/change_status_bar_for_imageview.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
233 changes: 195 additions & 38 deletions library/src/main/java/com/jaeger/library/StatusBarUtil.java

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions sample/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<activity android:name="com.jaeger.statusbarutil.UseInFragmentActivity"/>
<activity android:name="com.jaeger.statusbarutil.SwipeBackActivity"/>
<activity android:name="com.jaeger.statusbarutil.SwitchModeActivity"/>
<activity android:name="com.jaeger.statusbarutil.LightModeActivity"/>
</application>

</manifest>
107 changes: 107 additions & 0 deletions sample/src/main/java/com/jaeger/statusbarutil/LightModeActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package com.jaeger.statusbarutil;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jaeger.library.StatusBarUtil;
import com.jaeger.statusbardemo.R;

/**
* Created by walter on 2019/11/7.
* Email: fengxiao1493@qq.com
* GitHub: https://github.com/walterFeng
*/
public class LightModeActivity extends BaseActivity {

private Toolbar mToolbar;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_light_mode);
mToolbar = findViewById(R.id.toolbar);
RecyclerView recyclerView = findViewById(R.id.recyclerView);

setSupportActionBar(mToolbar);
mToolbar.setTitleTextColor(Color.BLACK);
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}

recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(new RecyclerView.Adapter() {
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
TextView item = new TextView(LightModeActivity.this);
item.setTextSize(28f);
item.setPadding(50, 0, 0, 0);
ViewGroup.MarginLayoutParams params = new ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 300);
item.setLayoutParams(params);
return new ViewHolder(item);
}

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
((ViewHolder) viewHolder).onBind(i);
}

@Override
public int getItemCount() {
return 30;
}
});
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {

int scrolledY = 0;
int offsetY = 500;

@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}

@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
scrolledY += dy;
float alpha = scrolledY * 1f / offsetY;
if (alpha > 1f) {
alpha = 1f;
} else if (alpha < 0f) {
alpha = 0f;
}
int finalAlpha = (int) (alpha * 255);
StatusBarUtil.setColorWithAlpha(LightModeActivity.this, Color.WHITE, finalAlpha);
mToolbar.setBackgroundColor(StatusBarUtil.calculateColorLightMode(Color.WHITE, finalAlpha));
}
});
}

class ViewHolder extends RecyclerView.ViewHolder {
TextView textView;

ViewHolder(View itemView) {
super(itemView);
this.textView = (TextView) itemView;
}

void onBind(int position) {
textView.setText("item" + position);
textView.setGravity(Gravity.CENTER_VERTICAL);
}
}

@Override
protected void setStatusBar() {
StatusBarUtil.setStatusColorForImageView(this, Color.WHITE, 0, findViewById(R.id.toolbar));
}
}
17 changes: 14 additions & 3 deletions sample/src/main/java/com/jaeger/statusbarutil/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@
import android.widget.CheckBox;
import android.widget.SeekBar;
import android.widget.TextView;

import com.jaeger.library.StatusBarUtil;
import com.jaeger.statusbardemo.R;

/**
* Created by Jaeger on 16/2/14.
*
* <p>
* Email: chjie.jaeger@gmail.com
* GitHub: https://github.com/laobie
*/
Expand All @@ -31,6 +32,7 @@ public class MainActivity extends BaseActivity {
private Button mBtnUseInFragment;
private Button mBtnSetColorForSwipeBack;
private Button mBtnSwitchMode;
private Button mBtnLightMode;

private ViewGroup contentLayout;
private SeekBar mSbChangeAlpha;
Expand All @@ -55,12 +57,13 @@ protected void onCreate(Bundle savedInstanceState) {
mBtnUseInFragment = findViewById(R.id.btn_use_in_fragment);
mBtnSetColorForSwipeBack = findViewById(R.id.btn_set_color_for_swipe_back);
mBtnSwitchMode = findViewById(R.id.btn_switch_mode);
mBtnLightMode = findViewById(R.id.btn_light_mode);
mSbChangeAlpha = findViewById(R.id.sb_change_alpha);
mTvStatusAlpha = findViewById(R.id.tv_status_alpha);
setSupportActionBar(mToolbar);

ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, mDrawerLayout, mToolbar, R.string.navigation_drawer_open,
R.string.navigation_drawer_close);
R.string.navigation_drawer_close);
mDrawerLayout.setDrawerListener(toggle);
toggle.syncState();

Expand Down Expand Up @@ -122,6 +125,14 @@ public void onClick(View view) {
}
});

mBtnLightMode.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, LightModeActivity.class);
startActivity(intent);
}
});

mChbTranslucent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Expand All @@ -133,7 +144,7 @@ public void onClick(View v) {
contentLayout.setBackgroundDrawable(null);
mToolbar.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
StatusBarUtil.setColorForDrawerLayout(MainActivity.this, mDrawerLayout,
getResources().getColor(R.color.colorPrimary), mAlpha);
getResources().getColor(R.color.colorPrimary), mAlpha);
}
}
});
Expand Down
31 changes: 31 additions & 0 deletions sample/src/main/res/layout/activity_light_mode.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@color/white"
android:orientation="vertical">

<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="240dp"
android:scaleType="centerCrop"
android:src="@drawable/bg_monkey_king" />

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:theme="@style/ThemeOverlay.AppCompat.Light" />

<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/toolbar"
android:background="@color/colorAccent" />

</RelativeLayout>
35 changes: 21 additions & 14 deletions sample/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
Expand All @@ -24,86 +23,94 @@
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />

<CheckBox
android:id="@+id/chb_translucent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="4dp"
android:text="Translucent"/>
android:text="Translucent" />

<TextView
android:id="@+id/tv_status_alpha"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textColor="@color/colorAccent"
android:textSize="24sp"/>
android:textSize="24sp" />

<SeekBar
android:id="@+id/sb_change_alpha"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"/>
android:layout_marginRight="16dp" />

<Button
android:id="@+id/btn_set_color"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="4dp"
android:text="Set Color"/>
android:text="Set Color" />

<Button
android:id="@+id/btn_set_transparent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="4dp"
android:text="Set Transparent"/>
android:text="Set Transparent" />

<Button
android:id="@+id/btn_set_translucent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="4dp"
android:text="Set Translucent"/>
android:text="Set Translucent" />

<Button
android:id="@+id/btn_set_for_image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="4dp"
android:text="Set For ImageView"/>
android:text="Set For ImageView" />

<Button
android:id="@+id/btn_use_in_fragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="4dp"
android:text="Use In Fragment"/>
android:text="Use In Fragment" />

<Button
android:id="@+id/btn_set_color_for_swipe_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="4dp"
android:text="Set Color For Swipe Back Page "/>
android:text="Set Color For Swipe Back Page " />

<Button
android:id="@+id/btn_switch_mode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="4dp"
android:text="Switch Light or Dark Mode"/>
android:text="Switch Light or Dark Mode" />

<Button
android:id="@+id/btn_light_mode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="4dp"
android:text="light model with imageView" />

</LinearLayout>
</FrameLayout>
Expand All @@ -115,6 +122,6 @@
android:layout_gravity="start"
android:background="@color/white"
app:headerLayout="@layout/nav_header"
app:menu="@menu/activity_main_drawer"/>
app:menu="@menu/activity_main_drawer" />

</android.support.v4.widget.DrawerLayout>