react-native-navigation的迁移库

CollapsingTopBarBackground.java 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package com.reactnativenavigation.views.collapsingToolbar;
  2. import android.content.Context;
  3. import android.content.res.TypedArray;
  4. import android.widget.FrameLayout;
  5. import android.widget.ImageView;
  6. import com.facebook.drawee.view.SimpleDraweeView;
  7. import com.reactnativenavigation.params.CollapsingTopBarParams;
  8. import com.reactnativenavigation.utils.ViewUtils;
  9. import com.reactnativenavigation.views.Scrim;
  10. import static android.widget.FrameLayout.LayoutParams.MATCH_PARENT;
  11. public class CollapsingTopBarBackground extends FrameLayout {
  12. public static final float MAX_HEIGHT = ViewUtils.convertDpToPixel(256);
  13. private final CollapsingTopBarParams params;
  14. private SimpleDraweeView backdrop;
  15. private Scrim scrim;
  16. private int topBarHeight = -1;
  17. public CollapsingTopBarBackground(Context context, CollapsingTopBarParams params) {
  18. super(context);
  19. this.params = params;
  20. setFitsSystemWindows(true);
  21. createBackDropImage();
  22. createScrim();
  23. setWillNotDraw(false);
  24. }
  25. private void createBackDropImage() {
  26. backdrop = new SimpleDraweeView(getContext());
  27. setImageSource();
  28. backdrop.setScaleType(ImageView.ScaleType.CENTER_CROP);
  29. backdrop.setFitsSystemWindows(true);
  30. addView(backdrop, new LayoutParams(MATCH_PARENT, MATCH_PARENT));
  31. }
  32. private void setImageSource() {
  33. if (params.imageUri != null) {
  34. backdrop.setImageURI(params.imageUri);
  35. }
  36. }
  37. private void createScrim() {
  38. scrim = new Scrim(getContext(), params.scrimColor, MAX_HEIGHT / 2);
  39. addView(scrim);
  40. }
  41. public int getCollapsedTopBarHeight() {
  42. if (topBarHeight > -1) {
  43. return topBarHeight;
  44. }
  45. calculateTopBarHeight();
  46. return topBarHeight;
  47. }
  48. private void calculateTopBarHeight() {
  49. int[] attrs = new int[] {android.R.attr.actionBarSize};
  50. TypedArray ta = getContext().obtainStyledAttributes(attrs);
  51. topBarHeight = ta.getDimensionPixelSize(0, -1);
  52. ta.recycle();
  53. }
  54. public void collapse(float collapse) {
  55. scrim.collapse(collapse);
  56. }
  57. }