Foggy day
Android(java) - Depending on seekBar progress num, change view size using BottomSheet 본문
Android
Android(java) - Depending on seekBar progress num, change view size using BottomSheet
jinhan38 2021. 3. 4. 14:15
sample video
1. activity_change_view_size_by_seek_bar.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".customView.changeViewSizeBySeekBar.">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom"
android:orientation="vertical">
<TextView
android:id="@+id/tvShowBottomSheet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="30dp"
android:text="SHOW"
android:textColor="@color/black"
android:textSize="30dp"
android:textStyle="bold" />
</LinearLayout>
<include
android:id="@+id/custom_change_vertical_size_view"
layout="@layout/change_vertical_size_layout"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
2. change_vertical_size_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/changeLayoutWrap"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:behavior_hideable="true"
app:behavior_peekHeight="150dp"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">
<LinearLayout
android:id="@+id/llChangeView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="bottom"
android:layout_marginBottom="100dp"
android:orientation="vertical" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:orientation="vertical">
<LinearLayout
android:id="@+id/llDefaultView"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#009688"
android:gravity="center"
android:orientation="vertical">
<androidx.appcompat.widget.AppCompatSeekBar
android:id="@+id/seekBar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginHorizontal="30dp"
android:max="100"
android:progressBackgroundTint="#fff"
android:progressTint="@color/black"
android:thumb="@drawable/ic_baseline_album_24" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="false"
android:focusable="false"
android:gravity="center"
android:orientation="vertical">
<androidx.appcompat.widget.AppCompatTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change view size"
android:textColor="@color/black"
android:textSize="25dp"
android:textStyle="bold" />
<TextView
android:id="@+id/tvProgressNum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="0"
android:textSize="30dp"
android:textStyle="bold" />
</LinearLayout>
</FrameLayout>
3. ChangeViewHeightBottomSheet.java
public class ChangeViewHeightBottomSheet {
private BottomSheetBehavior behavior;
private Activity activity;
private int screenHeight = 0;
private LinearLayout llDefaultView;
private LinearLayout llChangeView;
private SeekBar seekBar;
private TextView tvProgressNum;
public ChangeViewHeightBottomSheet(BottomSheetBehavior behavior, Activity activity) {
this.behavior = behavior;
this.activity = activity;
llDefaultView = activity.findViewById(R.id.llDefaultView);
llChangeView = activity.findViewById(R.id.llChangeView);
seekBar = activity.findViewById(R.id.seekBar);
tvProgressNum = activity.findViewById(R.id.tvProgressNum);
//get display height
Display display = activity.getWindowManager().getDefaultDisplay();
this.screenHeight = display.getHeight();
}
/**
* Show this bottomSheet
*
* @param color
* @param seekBarProgress
*/
public void show(int color, int seekBarProgress) {
llDefaultView.setBackgroundColor(color);
llChangeView.setBackgroundColor(color);
llChangeView.getLayoutParams().height = 0;
llChangeView.requestLayout();
seekBar.setProgress(seekBarProgress);
behavior.setState(BottomSheetBehavior.STATE_EXPANDED);
changeSeekBar();
}
/**
* seekBar lisetenr
*/
public void changeSeekBar() {
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@SuppressLint("UseCompatLoadingForDrawables")
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
tvProgressNum.setText(String.valueOf(progress));
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
Handler handler = new Handler();
new Thread(() -> {
long maxHeight = screenHeight;
long increaseCount = maxHeight / 150;
long maxNum = 150;
boolean loopEnd = false;
long increaseMount = 1;
long status = 0;
while (!loopEnd) {
status += increaseMount;
if (status > maxNum) {
status = maxNum;
loopEnd = true;
}
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
int count = (int) (status * increaseCount);
handler.post(() -> {
llChangeView.getLayoutParams().height = count;
llChangeView.requestLayout();
});
}
}).start();
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
hidden();
}
});
}
/**
* Hidden this bottomSheet
*/
public void hidden() {
Handler handler = new Handler();
new Timer().schedule(new TimerTask() {
@Override
public void run() {
handler.post(() -> behavior.setState(BottomSheetBehavior.STATE_HIDDEN));
}
}, 200);
}
}
4. ChangeViewSizeBySeekBarJava.java
public class ChangeViewSizeBySeekBarJava extends AppCompatActivity {
private ActivityChangeViewSizeBySeekBarBinding b;
private View viewBottomSheet;
private BottomSheetBehavior behavior;
private ChangeViewHeightBottomSheet changeViewHeightBottomSheet;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
b = ActivityChangeViewSizeBySeekBarBinding.inflate(getLayoutInflater());
setContentView(b.getRoot());
viewBottomSheet = findViewById(R.id.custom_change_vertical_size_view);
behavior = (BottomSheetBehavior) BottomSheetBehavior.from(viewBottomSheet);
behavior.setState(BottomSheetBehavior.STATE_HIDDEN);
changeViewHeightBottomSheet = new ChangeViewHeightBottomSheet(behavior, this);
b.tvShowBottomSheet.setOnClickListener(view -> {
changeViewHeightBottomSheet.show(getResources().getColor(R.color.brand_purple), 0);
});
}
}
'Android' 카테고리의 다른 글
Android(java) - exoplayer2 (0) | 2021.03.05 |
---|---|
Android(java) - Activity popup(background dim) (0) | 2021.03.04 |
Android - Arraylist sort, collection sort (0) | 2021.02.23 |
Android - MediaPlayer with surfaceView (0) | 2021.02.17 |
Android - screen orientation rotation error (0) | 2021.02.11 |