Advertisement
Guest User

Untitled

a guest
Aug 6th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 28.96 KB | None | 0 0
  1. <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:app="http://schemas.android.com/apk/res-auto"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:fitsSystemWindows="true">
  6. <android.support.design.widget.AppBarLayout
  7. android:id="@+id/appbar"
  8. android:layout_width="match_parent"
  9. android:layout_height="300dp"
  10. android:fitsSystemWindows="true"
  11. android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
  12. <android.support.design.widget.CollapsingToolbarLayout
  13. android:id="@+id/collapsing_toolbar"
  14. android:layout_width="match_parent"
  15. android:layout_height="match_parent"
  16. android:fitsSystemWindows="true"
  17. app:contentScrim="?attr/colorPrimary"
  18. app:expandedTitleMarginBottom="160dp"
  19. app:expandedTitleMarginEnd="64dp"
  20. app:expandedTitleMarginStart="48dp"
  21. app:layout_scrollFlags="scroll|exitUntilCollapsed">
  22. <ImageView
  23. android:id="@+id/header"
  24. android:layout_width="match_parent"
  25. android:layout_height="match_parent"
  26. android:background="@drawable/quila2"
  27. android:fitsSystemWindows="true"
  28. android:scaleType="centerCrop"
  29. app:layout_collapseMode="parallax" />
  30. <TextView
  31. android:layout_width="match_parent"
  32. android:layout_height="wrap_content"
  33. android:textSize="16sp"
  34. android:textColor="@android:color/white"
  35. android:layout_marginBottom="32dp"
  36. android:layout_marginEnd="64dp"
  37. android:layout_marginStart="48dp"
  38. app:layout_collapseMode="parallax"
  39. android:layout_gravity="bottom"
  40. android:text="Lorem Ipsum Iran Lorem Ipsum Iran Lorem Ipsum Iran Lorem Ipsum Iran Lorem Ipsum Iran Lorem Ipsum Iran Lorem Ipsum Iran Lorem Ipsum Iran "/>
  41. <android.support.v7.widget.Toolbar
  42. android:id="@+id/anim_toolbar"
  43. android:layout_width="match_parent"
  44. android:layout_height="?attr/actionBarSize"
  45. app:layout_collapseMode="pin"
  46. app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
  47. </android.support.design.widget.CollapsingToolbarLayout>
  48. </android.support.design.widget.AppBarLayout>
  49.  
  50. <android.support.v4.widget.NestedScrollView
  51. android:layout_width="match_parent"
  52. android:layout_height="match_parent"
  53. android:scrollbars="none"
  54. app:layout_behavior="@string/appbar_scrolling_view_behavior">
  55.  
  56. <TextView
  57. android:layout_width="match_parent"
  58. android:layout_height="wrap_content"
  59. android:layout_margin="16dp"
  60. android:lineSpacingExtra="8dp"
  61. android:text="@string/lorem"
  62. android:textSize="18sp"/>
  63.  
  64. </android.support.v4.widget.NestedScrollView>
  65.  
  66. <android.support.design.widget.FloatingActionButton
  67. android:layout_width="wrap_content"
  68. android:layout_height="wrap_content"
  69. android:layout_margin="10dp"
  70. android:clickable="true"
  71. android:src="@drawable/abc_ic_search_api_mtrl_alpha"
  72. app:layout_anchor="@+id/appbar"
  73. app:layout_anchorGravity="bottom|right|end" />
  74.  
  75. </android.support.design.widget.CoordinatorLayout>
  76.  
  77. @Override
  78. public boolean layoutDependsOn(CoordinatorLayout parent, HeaderView child, View dependency) {
  79. return dependency instanceof AppBarLayout;
  80. }
  81.  
  82. @Override
  83. public boolean onDependentViewChanged(CoordinatorLayout parent, HeaderView child, View dependency) {
  84. shouldInitProperties(child, dependency);
  85.  
  86. int maxScroll = ((AppBarLayout) dependency).getTotalScrollRange();
  87. float percentage = Math.abs(dependency.getY()) / (float) maxScroll;
  88.  
  89. float childPosition = dependency.getHeight()
  90. + dependency.getY()
  91. - child.getHeight()
  92. - (getToolbarHeight() - child.getHeight()) * percentage / 2;
  93.  
  94.  
  95. childPosition = childPosition - mStartMarginBottom * (1f - percentage);
  96.  
  97. CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams) child.getLayoutParams();
  98. lp.leftMargin = (int) (percentage * mEndMargintLeft) + mStartMarginLeft;
  99. lp.rightMargin = mMarginRight;
  100. child.setLayoutParams(lp);
  101.  
  102. child.setY(childPosition);
  103.  
  104. ...
  105. return true;
  106. }
  107.  
  108. <android.support.design.widget.CoordinatorLayout
  109. xmlns:android="http://schemas.android.com/apk/res/android"
  110. xmlns:app="http://schemas.android.com/apk/res-auto"
  111. android:layout_width="match_parent"
  112. android:layout_height="match_parent"
  113. android:fitsSystemWindows="true"
  114. >
  115.  
  116.  
  117. <android.support.design.widget.AppBarLayout
  118. android:id="@+id/app_bar_layout"
  119. android:layout_width="match_parent"
  120. android:layout_height="wrap_content"
  121. android:fitsSystemWindows="true"
  122. android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
  123.  
  124. <android.support.design.widget.CollapsingToolbarLayout
  125. android:id="@+id/collapsing_toolbar"
  126. android:layout_width="match_parent"
  127. android:layout_height="wrap_content"
  128. android:fitsSystemWindows="true"
  129. app:contentScrim="?attr/colorPrimary"
  130. app:layout_scrollFlags="scroll|exitUntilCollapsed"
  131. >
  132.  
  133. <ImageView
  134. android:id="@+id/image"
  135. android:layout_width="match_parent"
  136. android:layout_height="wrap_content"
  137. android:fitsSystemWindows="true"
  138. android:scaleType="centerCrop"
  139. android:src="@drawable/img_nature"
  140. app:layout_collapseMode="parallax"
  141. />
  142.  
  143. <android.support.v7.widget.Toolbar
  144. android:id="@+id/toolbar"
  145. android:layout_width="match_parent"
  146. android:layout_height="?attr/actionBarSize"
  147. app:layout_collapseMode="pin"
  148. >
  149.  
  150.  
  151. <include
  152. android:id="@+id/toolbar_header_view"
  153. layout="@layout/header_view"
  154. android:layout_height="wrap_content"
  155. android:layout_width="match_parent"
  156. android:layout_marginRight="@dimen/header_view_end_margin_right"
  157. android:visibility="gone"
  158. />
  159.  
  160. </android.support.v7.widget.Toolbar>
  161.  
  162. </android.support.design.widget.CollapsingToolbarLayout>
  163.  
  164. </android.support.design.widget.AppBarLayout>
  165.  
  166. <android.support.v4.widget.NestedScrollView
  167. android:id="@+id/scroll"
  168. android:layout_width="match_parent"
  169. android:layout_height="match_parent"
  170. android:clipToPadding="false"
  171. app:layout_behavior="@string/appbar_scrolling_view_behavior"
  172. >
  173.  
  174. <LinearLayout
  175. android:layout_width="match_parent"
  176. android:layout_height="wrap_content"
  177. android:orientation="vertical"
  178. >
  179.  
  180. ...
  181.  
  182. </LinearLayout>
  183.  
  184. </android.support.v4.widget.NestedScrollView>
  185.  
  186. <include
  187. android:id="@+id/float_header_view"
  188. layout="@layout/header_view"
  189. android:layout_width="match_parent"
  190. android:layout_height="wrap_content"
  191. app:layout_behavior="com.subtitlebehavoir.harcopro.simple.ViewBehavior"
  192. />
  193.  
  194. </android.support.design.widget.CoordinatorLayout>
  195.  
  196. <?xml version="1.0" encoding="utf-8"?>
  197. <com.subtitlebehavoir.harcopro.simple.HeaderView
  198. xmlns:android="http://schemas.android.com/apk/res/android"
  199. android:layout_width="match_parent"
  200. android:layout_height="wrap_content"
  201. android:orientation="vertical"
  202. >
  203.  
  204. <!-- Title -->
  205. <TextView
  206. android:id="@+id/header_view_title"
  207. android:layout_width="wrap_content"
  208. android:layout_height="wrap_content"
  209. android:textColor="@android:color/white"
  210. android:textSize="18sp"
  211. />
  212.  
  213. <!-- Subtitle -->
  214. <TextView
  215. android:id="@+id/header_view_sub_title"
  216. android:layout_width="wrap_content"
  217. android:layout_height="wrap_content"
  218. android:textColor="@android:color/white"
  219. android:textSize="16sp"
  220. />
  221.  
  222.  
  223. </com.subtitlebehavoir.harcopro.simple.HeaderView>
  224.  
  225. public class ViewBehavior extends CoordinatorLayout.Behavior<HeaderView> {
  226.  
  227. private static final float MAX_SCALE = 0.5f;
  228.  
  229. private Context mContext;
  230.  
  231. private int mStartMarginLeft;
  232. private int mEndMargintLeft;
  233. private int mMarginRight;
  234. private int mStartMarginBottom;
  235. private boolean isHide;
  236.  
  237. public ViewBehavior(Context context, AttributeSet attrs) {
  238. mContext = context;
  239. }
  240.  
  241. @Override
  242. public boolean layoutDependsOn(CoordinatorLayout parent, HeaderView child, View dependency) {
  243. return dependency instanceof AppBarLayout;
  244. }
  245.  
  246. @Override
  247. public boolean onDependentViewChanged(CoordinatorLayout parent, HeaderView child, View dependency) {
  248. shouldInitProperties(child, dependency);
  249.  
  250. int maxScroll = ((AppBarLayout) dependency).getTotalScrollRange();
  251. float percentage = Math.abs(dependency.getY()) / (float) maxScroll;
  252.  
  253. // Set scale for the title
  254. float size = ((1 - percentage) * MAX_SCALE) + 1;
  255. child.setScaleXTitle(size);
  256. child.setScaleYTitle(size);
  257.  
  258. // Set position for the header view
  259. float childPosition = dependency.getHeight()
  260. + dependency.getY()
  261. - child.getHeight()
  262. - (getToolbarHeight() - child.getHeight()) * percentage / 2;
  263.  
  264. childPosition = childPosition - mStartMarginBottom * (1f - percentage);
  265.  
  266. CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams) child.getLayoutParams();
  267. lp.leftMargin = (int) (percentage * mEndMargintLeft) + mStartMarginLeft;
  268. lp.rightMargin = mMarginRight;
  269. child.setLayoutParams(lp);
  270.  
  271. child.setY(childPosition);
  272.  
  273. if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
  274. if (isHide && percentage < 1) {
  275. child.setVisibility(View.VISIBLE);
  276. isHide = false;
  277. } else if (!isHide && percentage == 1) {
  278. child.setVisibility(View.GONE);
  279. isHide = true;
  280. }
  281. }
  282. return true;
  283. }
  284.  
  285. private void shouldInitProperties(HeaderView child, View dependency) {
  286.  
  287. if (mStartMarginLeft == 0)
  288. mStartMarginLeft = mContext.getResources().getDimensionPixelOffset(R.dimen.header_view_start_margin_left);
  289.  
  290. if (mEndMargintLeft == 0)
  291. mEndMargintLeft = mContext.getResources().getDimensionPixelOffset(R.dimen.header_view_end_margin_left);
  292.  
  293. if (mStartMarginBottom == 0)
  294. mStartMarginBottom = mContext.getResources().getDimensionPixelOffset(R.dimen.header_view_start_margin_bottom);
  295.  
  296. if (mMarginRight == 0)
  297. mMarginRight = mContext.getResources().getDimensionPixelOffset(R.dimen.header_view_end_margin_right);
  298. }
  299.  
  300.  
  301. public int getToolbarHeight() {
  302. int result = 0;
  303. TypedValue tv = new TypedValue();
  304. if (mContext.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
  305. result = TypedValue.complexToDimensionPixelSize(tv.data, mContext.getResources().getDisplayMetrics());
  306. }
  307. return result;
  308. }
  309. }
  310.  
  311. public class HeaderView extends LinearLayout {
  312.  
  313. @Bind(R.id.header_view_title)
  314. TextView title;
  315.  
  316. @Bind(R.id.header_view_sub_title)
  317. TextView subTitle;
  318.  
  319. public HeaderView(Context context) {
  320. super(context);
  321. }
  322.  
  323. public HeaderView(Context context, AttributeSet attrs) {
  324. super(context, attrs);
  325. }
  326.  
  327. public HeaderView(Context context, AttributeSet attrs, int defStyleAttr) {
  328. super(context, attrs, defStyleAttr);
  329. }
  330.  
  331. @TargetApi(Build.VERSION_CODES.LOLLIPOP)
  332. public HeaderView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
  333. super(context, attrs, defStyleAttr, defStyleRes);
  334. }
  335.  
  336. @Override
  337. protected void onFinishInflate() {
  338. super.onFinishInflate();
  339. ButterKnife.bind(this);
  340. }
  341.  
  342. public void bindTo(String title) {
  343. bindTo(title, "");
  344. }
  345.  
  346. public void bindTo(String title, String subTitle) {
  347. hideOrSetText(this.title, title);
  348. hideOrSetText(this.subTitle, subTitle);
  349. }
  350.  
  351. private void hideOrSetText(TextView tv, String text) {
  352. if (text == null || text.equals(""))
  353. tv.setVisibility(GONE);
  354. else
  355. tv.setText(text);
  356. }
  357.  
  358. public void setScaleXTitle(float scaleXTitle) {
  359. title.setScaleX(scaleXTitle);
  360. title.setPivotX(0);
  361. }
  362.  
  363. public void setScaleYTitle(float scaleYTitle) {
  364. title.setScaleY(scaleYTitle);
  365. title.setPivotY(30);
  366. }
  367. }
  368.  
  369. <android.support.design.widget.CoordinatorLayout
  370. xmlns:android="http://schemas.android.com/apk/res/android"
  371. xmlns:app="http://schemas.android.com/apk/res-auto"
  372. android:id="@+id/main_content"
  373. android:layout_width="match_parent"
  374. android:layout_height="match_parent">
  375. <android.support.design.widget.AppBarLayout
  376. android:id="@+id/appbar"
  377. android:layout_width="match_parent"
  378. android:layout_height="@dimen/series_detail_header_height"
  379. android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
  380. app:layout_scrollFlags="scroll|exitUntilCollapsed">
  381. <android.support.design.widget.CollapsingToolbarLayout
  382. android:id="@+id/collapsing_toolbar"
  383. android:layout_width="match_parent"
  384. android:layout_height="match_parent"
  385. android:fitsSystemWindows="true"
  386. app:contentScrim="?attr/colorPrimary"
  387. app:expandedTitleMarginEnd="64dp"
  388. app:expandedTitleMarginStart="48dp"
  389. app:expandedTitleTextAppearance="@style/TransparentText"
  390. app:layout_scrollFlags="scroll|exitUntilCollapsed">
  391. <FrameLayout
  392. android:layout_width="match_parent"
  393. android:layout_height="match_parent"
  394. app:layout_collapseMode="parallax">
  395. <ImageView
  396. android:id="@+id/image_view"
  397. android:layout_width="match_parent"
  398. android:layout_height="@dimen/series_detail_header_image_height"
  399. android:scaleType="centerCrop"/>
  400. <LinearLayout
  401. android:id="@+id/header_text_layout"
  402. android:layout_width="match_parent"
  403. android:layout_height="wrap_content"
  404. android:layout_gravity="bottom"
  405. android:background="?attr/colorPrimary"
  406. android:gravity="center_vertical"
  407. android:minHeight="@dimen/series_detail_text_layout_height"
  408. android:orientation="vertical"
  409. android:paddingLeft="20dp"
  410. android:paddingRight="10dp"
  411. android:paddingTop="10dp">
  412. <TextView
  413. android:id="@+id/title"
  414. android:layout_width="match_parent"
  415. android:layout_height="wrap_content"
  416. android:text="My title"
  417. android:textColor="@android:color/white"
  418. android:textSize="24sp"/>
  419. <TextView
  420. android:id="@+id/subtitle"
  421. android:layout_width="match_parent"
  422. android:layout_height="wrap_content"
  423. android:textColor="@android:color/white"
  424. android:textSize="17sp"/>
  425. </LinearLayout>
  426. </FrameLayout>
  427. <View
  428. android:layout_width="match_parent"
  429. android:layout_height="?attr/actionBarSize"
  430. android:background="@drawable/shape_toolbar_black_gradient"
  431. app:layout_collapseMode="pin"/>
  432. <android.support.v7.widget.Toolbar
  433. android:id="@+id/toolbar"
  434. android:layout_width="match_parent"
  435. android:layout_height="?attr/actionBarSize"
  436. android:layout_gravity="top"
  437. app:layout_collapseMode="pin"
  438. app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
  439. />
  440. </android.support.design.widget.CollapsingToolbarLayout>
  441. </android.support.design.widget.AppBarLayout>
  442. <android.support.v4.view.ViewPager
  443. android:id="@+id/show_view_pager"
  444. android:layout_width="match_parent"
  445. android:layout_height="match_parent"
  446. android:background="@android:color/white"
  447. android:paddingTop="@dimen/toolbar_height"
  448. app:layout_behavior="@string/appbar_scrolling_view_behavior"/></android.support.design.widget.CoordinatorLayout>
  449.  
  450. <style name="TransparentText" parent="@android:style/TextAppearance">
  451. <item name="android:textColor">#00666666</item>
  452. </style>
  453.  
  454. <android.support.design.widget.CoordinatorLayout
  455. xmlns:android="http://schemas.android.com/apk/res/android"
  456. xmlns:app="http://schemas.android.com/apk/res-auto"
  457. android:layout_width="match_parent"
  458. android:layout_height="match_parent">
  459.  
  460. <android.support.design.widget.AppBarLayout
  461. android:layout_width="match_parent"
  462. android:layout_height="wrap_content">
  463.  
  464. <com.hendraanggrian.widget.SubtitleCollapsingToolbarLayout
  465. android:id="@+id/subtitlecollapsingtoolbarlayout"
  466. android:layout_width="match_parent"
  467. android:layout_height="wrap_content"
  468. app:contentScrim="?colorPrimary"
  469. app:layout_scrollFlags="scroll|exitUntilCollapsed"
  470. app:subtitle="CollapsingToolbarLayout"
  471. app:title="Subtitle">
  472.  
  473. <!-- collapsing toolbar content goes here -->
  474.  
  475. <android.support.v7.widget.Toolbar
  476. android:layout_width="match_parent"
  477. android:layout_height="?actionBarSize"
  478. app:layout_collapseMode="pin"/>
  479. </com.hendraanggrian.widget.SubtitleCollapsingToolbarLayout>
  480. </android.support.design.widget.AppBarLayout>
  481.  
  482. <!-- content goes here -->
  483.  
  484. </android.support.design.widget.CoordinatorLayout>
  485.  
  486. <?xml version="1.0" encoding="utf-8"?>
  487. <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
  488. xmlns:app="http://schemas.android.com/apk/res-auto"
  489. android:layout_width="match_parent"
  490. android:layout_height="match_parent"
  491. android:fitsSystemWindows="true">
  492.  
  493.  
  494. <android.support.design.widget.AppBarLayout
  495. android:id="@+id/app_bar_layout"
  496. android:layout_width="match_parent"
  497. android:layout_height="wrap_content"
  498. android:fitsSystemWindows="true"
  499. android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
  500.  
  501. <android.support.design.widget.CollapsingToolbarLayout
  502. android:id="@+id/collapsing_toolbar"
  503. android:layout_width="match_parent"
  504. android:layout_height="wrap_content"
  505. android:fitsSystemWindows="true"
  506. app:contentScrim="?attr/colorPrimary"
  507. app:layout_scrollFlags="scroll|exitUntilCollapsed">
  508.  
  509. <ImageView
  510. android:id="@+id/image"
  511. android:layout_width="match_parent"
  512. android:layout_height="wrap_content"
  513. android:fitsSystemWindows="true"
  514. android:scaleType="centerCrop"
  515. android:src="@drawable/img_nature"
  516. app:layout_collapseMode="parallax" />
  517.  
  518. <android.support.v7.widget.Toolbar
  519. android:id="@+id/toolbar"
  520. android:layout_width="match_parent"
  521. android:layout_height="?attr/actionBarSize"
  522. app:layout_collapseMode="pin">
  523.  
  524.  
  525. <include
  526. android:id="@+id/toolbar_header_view"
  527. layout="@layout/header_view"
  528. android:layout_width="wrap_content"
  529. android:layout_height="wrap_content"
  530. android:layout_marginRight="@dimen/header_view_end_margin_right"
  531. android:visibility="gone" />
  532.  
  533. </android.support.v7.widget.Toolbar>
  534.  
  535. </android.support.design.widget.CollapsingToolbarLayout>
  536.  
  537. </android.support.design.widget.AppBarLayout>
  538.  
  539. <android.support.v4.widget.NestedScrollView
  540. android:id="@+id/scroll"
  541. android:layout_width="match_parent"
  542. android:layout_height="match_parent"
  543. android:clipToPadding="false"
  544. app:layout_behavior="@string/appbar_scrolling_view_behavior">
  545.  
  546. <LinearLayout
  547. android:layout_width="match_parent"
  548. android:layout_height="wrap_content"
  549. android:orientation="vertical">
  550.  
  551. <TextView
  552. android:layout_width="wrap_content"
  553. android:layout_height="wrap_content"
  554. android:text="Hello World!" />
  555.  
  556. <TextView
  557. android:layout_width="wrap_content"
  558. android:layout_height="wrap_content"
  559. android:text="Hello World!" />
  560.  
  561. </LinearLayout>
  562.  
  563.  
  564. </android.support.v4.widget.NestedScrollView>
  565.  
  566. <include
  567. android:id="@+id/float_header_view"
  568. layout="@layout/header_view"
  569. android:layout_width="wrap_content"
  570. android:layout_height="wrap_content"
  571. app:layout_behavior="com.subtitlebehavoir.harcopro.simple.ViewBehavior" />
  572.  
  573. </android.support.design.widget.CoordinatorLayout>
  574.  
  575. <resources>
  576. <!-- Default screen margins, per the Android Design guidelines. -->
  577. <dimen name="activity_horizontal_margin">16dp</dimen>
  578. <dimen name="activity_vertical_margin">16dp</dimen>
  579.  
  580. <dimen name="header_view_end_margin_left">56dp</dimen>
  581. <dimen name="header_view_end_margin_right">14dp</dimen>
  582.  
  583. <dimen name="header_view_start_margin_bottom">14dp</dimen>
  584.  
  585. </resources>
  586.  
  587. <?xml version="1.0" encoding="utf-8"?>
  588. <com.subtitlebehavoir.harcopro.simple.HeaderView xmlns:android="http://schemas.android.com/apk/res/android"
  589. android:layout_width="wrap_content"
  590. android:layout_height="wrap_content">
  591.  
  592. <!-- Title -->
  593. <TextView
  594. android:id="@+id/header_view_title"
  595. android:layout_width="match_parent"
  596. android:layout_height="wrap_content"
  597. android:maxLines="1"
  598. android:textColor="@android:color/white"
  599. android:textSize="18sp" />
  600.  
  601. <!-- Subtitle -->
  602. <TextView
  603. android:id="@+id/header_view_sub_title"
  604. android:layout_width="match_parent"
  605. android:layout_height="wrap_content"
  606. android:layout_below="@+id/header_view_title"
  607. android:maxLines="1"
  608. android:textColor="@android:color/white"
  609. android:textSize="16sp" />
  610.  
  611.  
  612. </com.subtitlebehavoir.harcopro.simple.HeaderView>
  613.  
  614. public class HeaderView extends RelativeLayout {
  615.  
  616. @Bind(R.id.header_view_title)
  617. TextView title;
  618.  
  619. @Bind(R.id.header_view_sub_title)
  620. TextView subTitle;
  621.  
  622. Context context;
  623.  
  624. public HeaderView(Context context) {
  625. super(context);
  626. this.context = context;
  627. }
  628.  
  629. public HeaderView(Context context, AttributeSet attrs) {
  630. super(context, attrs);
  631. this.context = context;
  632. }
  633.  
  634. public HeaderView(Context context, AttributeSet attrs, int defStyleAttr) {
  635. super(context, attrs, defStyleAttr);
  636. this.context = context;
  637. }
  638.  
  639. @TargetApi(Build.VERSION_CODES.LOLLIPOP)
  640. public HeaderView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
  641. super(context, attrs, defStyleAttr, defStyleRes);
  642. this.context = context;
  643. }
  644.  
  645. @Override
  646. protected void onFinishInflate() {
  647. super.onFinishInflate();
  648. ButterKnife.bind(this);
  649. }
  650.  
  651. public void bindTo(String title) {
  652. bindTo(title, "");
  653. }
  654.  
  655. public void bindTo(String title, String subTitle) {
  656. hideOrSetText(this.title, title);
  657. hideOrSetText(this.subTitle, subTitle);
  658. }
  659.  
  660. private void hideOrSetText(TextView tv, String text) {
  661. if (text == null || text.equals(""))
  662. tv.setVisibility(GONE);
  663. else
  664. tv.setText(text);
  665. }
  666.  
  667. public void setScaleXTitle(float scaleXTitle) {
  668. title.setScaleX(scaleXTitle);
  669. title.setPivotX(0);
  670. }
  671.  
  672. public void setScaleYTitle(float scaleYTitle) {
  673. title.setScaleY(scaleYTitle);
  674. title.setPivotY(30);
  675. }
  676.  
  677. public TextView getTitle() {
  678. return title;
  679. }
  680.  
  681. public TextView getSubTitle() {
  682. return subTitle;
  683. }
  684. }
  685.  
  686. public class ViewBehavior extends CoordinatorLayout.Behavior<HeaderView> {
  687.  
  688. private static final float MAX_SCALE = 0.5f;
  689.  
  690. private Context mContext;
  691.  
  692. private int mStartMarginLeftTitle;
  693. private int mStartMarginLeftSubTitle;
  694. private int mEndMargintLeft;
  695. private int mMarginRight;
  696. private int mStartMarginBottom;
  697. private boolean isHide;
  698.  
  699. public ViewBehavior(Context context, AttributeSet attrs) {
  700. mContext = context;
  701. }
  702.  
  703. @Override
  704. public boolean layoutDependsOn(CoordinatorLayout parent, HeaderView child, View dependency) {
  705. return dependency instanceof AppBarLayout;
  706. }
  707.  
  708. @Override
  709. public boolean onDependentViewChanged(CoordinatorLayout parent, HeaderView child, View dependency) {
  710. shouldInitProperties(child, dependency);
  711.  
  712. int maxScroll = ((AppBarLayout) dependency).getTotalScrollRange();
  713. float percentage = Math.abs(dependency.getY()) / (float) maxScroll;
  714.  
  715. // Set scale for the title
  716. float size = ((1 - percentage) * MAX_SCALE) + 1;
  717. child.setScaleXTitle(size);
  718. child.setScaleYTitle(size);
  719.  
  720. // Set position for the header view
  721. float childPosition = dependency.getHeight()
  722. + dependency.getY()
  723. - child.getHeight()
  724. - (getToolbarHeight() - child.getHeight()) * percentage / 2;
  725.  
  726. childPosition = childPosition - mStartMarginBottom * (1f - percentage);
  727. child.setY(childPosition);
  728.  
  729. // Set Margin for title
  730. RelativeLayout.LayoutParams lpTitle = (RelativeLayout.LayoutParams) child.getTitle().getLayoutParams();
  731. lpTitle.leftMargin = (int) ((mStartMarginLeftTitle) - (percentage * (mStartMarginLeftTitle - mEndMargintLeft)));
  732.  
  733. if (lpTitle.leftMargin < 20) {
  734. lpTitle.leftMargin = 20;
  735. }
  736. lpTitle.rightMargin = mMarginRight;
  737. child.getTitle().setLayoutParams(lpTitle);
  738.  
  739. // Set Margin for subtitle
  740. RelativeLayout.LayoutParams lpSubTitle = (RelativeLayout.LayoutParams) child.getSubTitle().getLayoutParams();
  741. lpSubTitle.leftMargin = (int) ((mStartMarginLeftSubTitle) - (percentage * (mStartMarginLeftSubTitle - mEndMargintLeft)));
  742.  
  743. if (lpSubTitle.leftMargin < 20) {
  744. lpSubTitle.leftMargin = 20;
  745. }
  746. lpSubTitle.rightMargin = mMarginRight;
  747. child.getSubTitle().setLayoutParams(lpSubTitle);
  748.  
  749. if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
  750. if (isHide && percentage < 1) {
  751. child.setVisibility(View.VISIBLE);
  752. isHide = false;
  753. } else if (!isHide && percentage == 1) {
  754. child.setVisibility(View.GONE);
  755. isHide = true;
  756. }
  757. }
  758. return true;
  759. }
  760.  
  761. private void shouldInitProperties(HeaderView child, View dependency) {
  762.  
  763. if (mStartMarginLeftTitle == 0)
  764. mStartMarginLeftTitle = getStartMarginLeftTitle(child);
  765.  
  766. if (mStartMarginLeftSubTitle == 0)
  767. mStartMarginLeftSubTitle = getStartMarginLeftSubTitle(child);
  768.  
  769. if (mEndMargintLeft == 0)
  770. mEndMargintLeft = mContext.getResources().getDimensionPixelOffset(R.dimen.header_view_end_margin_left);
  771.  
  772. if (mStartMarginBottom == 0)
  773. mStartMarginBottom = mContext.getResources().getDimensionPixelOffset(R.dimen.header_view_start_margin_bottom);
  774.  
  775. if (mMarginRight == 0)
  776. mMarginRight = mContext.getResources().getDimensionPixelOffset(R.dimen.header_view_end_margin_right);
  777. }
  778.  
  779. public int getStartMarginLeftTitle(HeaderView headerView) {
  780. TextView title = headerView.getTitle();
  781. DisplayMetrics displaymetrics = new DisplayMetrics();
  782. WindowManager windowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
  783. windowManager.getDefaultDisplay().getMetrics(displaymetrics);
  784. int width = displaymetrics.widthPixels;
  785.  
  786. int stringWidth = getStingWidth(title);
  787.  
  788. int marginLeft = (int) ((width / 2) - ((stringWidth + (stringWidth * MAX_SCALE)) / 2));
  789. return marginLeft;
  790. }
  791.  
  792. public int getStartMarginLeftSubTitle(HeaderView headerView) {
  793. TextView subTitle = headerView.getSubTitle();
  794. DisplayMetrics displaymetrics = new DisplayMetrics();
  795. WindowManager windowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
  796. windowManager.getDefaultDisplay().getMetrics(displaymetrics);
  797. int width = displaymetrics.widthPixels;
  798.  
  799. int stringWidth = getStingWidth(subTitle);
  800.  
  801. int marginLeft = ((width / 2) - (stringWidth / 2));
  802. return marginLeft;
  803. }
  804.  
  805. public int getStingWidth(TextView textView) {
  806. Rect bounds = new Rect();
  807. Paint textPaint = textView.getPaint();
  808. textPaint.getTextBounds(textView.getText().toString(), 0, textView.getText().toString().length(), bounds);
  809. return bounds.width();
  810. }
  811.  
  812. public int getToolbarHeight() {
  813. int result = 0;
  814. TypedValue tv = new TypedValue();
  815. if (mContext.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
  816. result = TypedValue.complexToDimensionPixelSize(tv.data, mContext.getResources().getDisplayMetrics());
  817. }
  818. return result;
  819. }
  820. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement