转自:http://www.eoeandroid.com/thread-577241-1-1.html



最近看到最美等应用里面有一个特效,自己上网查了下,写了个demo如下:

 

在写这个demo之前,查了很多资料,主要是参考github上面的一个例子:
https://github.com/ksoichiro/Android-ObservableScrollView

这个例子里面写了很多特效,但是下载下来后,导入Studio里面很多错误,无法跑起来,所以,自己抠了其中的一个特效,修改了一些代码,效果如上图所示。


主要代码片段:(后面会有解释)
  1. import android.content.res.TypedArray;
  2. import android.graphics.Color;
  3. import android.os.Bundle;
  4. import android.support.v7.app.ActionBarActivity;
  5. import android.support.v7.widget.Toolbar;
  6. import android.util.TypedValue;
  7. import android.view.Menu;
  8. import android.view.MenuItem;
  9. import android.view.View;
  10. import android.widget.TextView;

  11. import com.nineoldandroids.view.ViewHelper;


  12. public class MainActivity extends ActionBarActivity implements ObservableScrollViewCallbacks{

  13.     private static final float MAX_TEXT_SCALE_DELTA = 0.3f;
  14.     private static final boolean TOOLBAR_IS_STICKY = true;

  15.     private View mToolbar;
  16.     private View mImageView;
  17.     private View mOverlayView;
  18.     private ObservableScrollView mScrollView;
  19.     private TextView mTitleView;
  20.     private View mFab;
  21.     private int mActionBarSize;
  22.     private int mFlexibleSpaceShowFabOffset;
  23.     private int mFlexibleSpaceImageHeight;
  24.     private int mToolbarColor;

  25.     @Override
  26.     protected void onCreate(Bundle savedInstanceState) {
  27.         super.onCreate(savedInstanceState);
  28.         setContentView(R.layout.activity_main);
  29.         setSupportActionBar((Toolbar) findViewById(R.id.toolbar));

  30.         mFlexibleSpaceImageHeight = getResources().getDimensionPixelSize(R.dimen.flexible_space_image_height);
  31.         mActionBarSize = getActionBarSize();
  32.         mToolbarColor = getResources().getColor(R.color.primary);

  33.         mToolbar = findViewById(R.id.toolbar);
  34.         if (!TOOLBAR_IS_STICKY) {
  35.             mToolbar.setBackgroundColor(Color.TRANSPARENT);
  36.         }
  37.         mImageView = findViewById(R.id.image);
  38.         mOverlayView = findViewById(R.id.overlay);
  39.         mScrollView = (ObservableScrollView) findViewById(R.id.scroll);
  40.         mScrollView.setScrollViewCallbacks(this);
  41.         mTitleView = (TextView) findViewById(R.id.title);
  42.         mTitleView.setText(getTitle());
  43.         setTitle(null);

  44.         ScrollUtils.addOnGlobalLayoutListener(mScrollView, new Runnable() {
  45.             @Override
  46.             public void run() {
  47.                 mScrollView.scrollTo(0, mFlexibleSpaceImageHeight - mActionBarSize);

  48.             }
  49.         });
  50.     }


  51.     @Override
  52.     public boolean onCreateOptionsMenu(Menu menu) {
  53.         // Inflate the menu; this adds items to the action bar if it is present.
  54.         getMenuInflater().inflate(R.menu.menu_main, menu);
  55.         return true;
  56.     }

  57.     @Override
  58.     public boolean onOptionsItemSelected(MenuItem item) {
  59.         // Handle action bar item clicks here. The action bar will
  60.         // automatically handle clicks on the Home/Up button, so long
  61.         // as you specify a parent activity in AndroidManifest.xml.
  62.         int id = item.getItemId();

  63.         //noinspection SimplifiableIfStatement

  64.         return super.onOptionsItemSelected(item);
  65.     }

  66.     protected int getActionBarSize() {
  67.         TypedValue typedValue = new TypedValue();
  68.         int[] textSizeAttr = new int[]{R.attr.actionBarSize};
  69.         int indexOfAttrTextSize = 0;
  70.         TypedArray a = obtainStyledAttributes(typedValue.data, textSizeAttr);
  71.         int actionBarSize = a.getDimensionPixelSize(indexOfAttrTextSize, -1);
  72.         a.recycle();
  73.         return actionBarSize;
  74.     }

  75.     @Override
  76.     public void onScrollChanged(int scrollY, boolean firstScroll, boolean dragging) {
  77.         float flexibleRange = mFlexibleSpaceImageHeight - mActionBarSize;
  78.         int minOverlayTransitionY = mActionBarSize - mOverlayView.getHeight();
  79.         ViewHelper.setTranslationY(mOverlayView, ScrollUtils.getFloat(-scrollY, minOverlayTransitionY, 0));
  80.         ViewHelper.setTranslationY(mImageView, ScrollUtils.getFloat(-scrollY / 2, minOverlayTransitionY, 0));

  81.         // Change alpha of overlay
  82.         ViewHelper.setAlpha(mOverlayView, ScrollUtils.getFloat((float) scrollY / flexibleRange, 0, 1));

  83.         // Scale title text
  84.         float scale = 1 + ScrollUtils.getFloat((flexibleRange - scrollY) / flexibleRange, 0, MAX_TEXT_SCALE_DELTA);
  85.         ViewHelper.setPivotX(mTitleView, 0);
  86.         ViewHelper.setPivotY(mTitleView, 0);
  87.         ViewHelper.setScaleX(mTitleView, scale);
  88.         ViewHelper.setScaleY(mTitleView, scale);

  89.         // Translate title text
  90.         int maxTitleTranslationY = (int) (mFlexibleSpaceImageHeight - mTitleView.getHeight() * scale);
  91.         int titleTranslationY = maxTitleTranslationY - scrollY;
  92.         if (TOOLBAR_IS_STICKY) {
  93.             titleTranslationY = Math.max(0, titleTranslationY);
  94.         }
  95.         ViewHelper.setTranslationY(mTitleView, titleTranslationY);

  96.         if (TOOLBAR_IS_STICKY) {
  97.             // Change alpha of toolbar background
  98.             if (-scrollY + mFlexibleSpaceImageHeight <= mActionBarSize) {
  99.                 mToolbar.setBackgroundColor(ScrollUtils.getColorWithAlpha(1, mToolbarColor));
  100.             } else {
  101.                 mToolbar.setBackgroundColor(ScrollUtils.getColorWithAlpha(0, mToolbarColor));
  102.             }
  103.         } else {
  104.             // Translate Toolbar
  105.             if (scrollY < mFlexibleSpaceImageHeight) {
  106.                 ViewHelper.setTranslationY(mToolbar, 0);
  107.             } else {
  108.                 ViewHelper.setTranslationY(mToolbar, -scrollY);
  109.             }
  110.         }
  111.     }

  112.     @Override
  113.     public void onDownMotionEvent() {

  114.     }

  115.     @Override
  116.     public void onUpOrCancelMotionEvent(ScrollState scrollState) {

  117.     }
  118. }
复制代码 activity_main.xml文件
  1.     xmlns:app="http://schemas.android.com/apk/res-auto"
  2.     android:layout_width="match_parent"
  3.     android:layout_height="match_parent">

  4.    
  5.         android:id="@+id/image"
  6.         android:layout_width="match_parent"
  7.         android:layout_height="@dimen/flexible_space_image_height"
  8.         android:scaleType="centerCrop"
  9.         android:src="@mipmap/ic_pic" />

  10.    
  11.         android:id="@+id/overlay"
  12.         android:layout_width="match_parent"
  13.         android:layout_height="@dimen/flexible_space_image_height"
  14.         android:background="?attr/colorPrimary" />

  15.    
  16.         android:id="@+id/scroll"
  17.         android:layout_width="match_parent"
  18.         android:layout_height="match_parent"
  19.         android:overScrollMode="never"
  20.         android:scrollbars="none">

  21.         
  22.             android:layout_width="match_parent"
  23.             android:layout_height="wrap_content"
  24.             android:orientation="vertical">

  25.             
  26.                 android:layout_width="match_parent"
  27.                 android:layout_height="@dimen/flexible_space_image_height"
  28.                 android:background="@android:color/transparent" />

  29.             
  30.                 android:layout_width="match_parent"
  31.                 android:layout_height="wrap_content"
  32.                 android:background="@android:color/white"
  33.                 android:paddingBottom="@dimen/activity_vertical_margin"
  34.                 android:paddingLeft="@dimen/activity_horizontal_margin"
  35.                 android:paddingRight="@dimen/activity_horizontal_margin"
  36.                 android:paddingTop="@dimen/activity_vertical_margin"
  37.                 android:text="@string/desc" />
  38.         
  39.    

  40.    
  41.         android:id="@+id/toolbar"
  42.         android:layout_width="match_parent"
  43.         android:layout_height="wrap_content"
  44.         android:background="?attr/colorPrimary"
  45.         android:minHeight="?attr/actionBarSize"
  46.         app:popupTheme="@style/Theme.AppCompat.Light.DarkActionBar"
  47.         />

  48.    
  49.         android:layout_width="match_parent"
  50.         android:layout_height="wrap_content"
  51.         android:orientation="vertical"
  52.         >

  53.         
  54.             android:id="@+id/title"
  55.             android:layout_width="match_parent"
  56.             android:layout_height="wrap_content"
  57.             android:ellipsize="end"
  58.             android:gravity="center_vertical"
  59.             android:maxLines="1"
  60.             android:minHeight="?attr/actionBarSize"
  61.             android:textColor="@android:color/white"
  62.             android:text="Make Attractive"
  63.             android:paddingLeft="@dimen/activity_vertical_margin"
  64.             android:textSize="20sp" />

  65.         
  66.             android:layout_width="match_parent"
  67.             android:layout_height="@dimen/flexible_space_image_height"
  68.             android:background="@android:color/transparent" />
  69.    

复制代码
其实,效果实现很简单,控件ObservableScrollView主要是继承了ScrollView并实现了Scrollable接口,在MainActivity中,重写onScrollChanged方法,在方法中,主要做了这几部操作:
1、随着滑动,让图片mImageView上移
2、随着滑动,让盖在图片上的view上移,长度是imageView上移的2倍
3、随着滑动,让盖在图片上的view的alpha值逐渐变大(就是那个渐渐变绿的效果)
4、随着滑动,让Title的字体逐渐变小并让title上移到ToolBar的位置
5、当滑动到ToolBar的位置时,让ToolBar显示

下载源码如下:


  ObservableScrollViewDemo.rar 

更多相关文章

  1. Android(安卓)利用addView 动态给Activity添加View组件
  2. 第一行代码读书笔记 Kotlin Android
  3. Android(安卓)proguard混淆编译的问题
  4. [转]Android(安卓)Music和第三方应用
  5. Android(安卓)画图之抗锯齿
  6. Android(安卓)Studio重构之路,我们重新来了解一下Google官方的And
  7. Launcher桌面点击&长按&拖动事件处理流程分析
  8. Android(安卓)init.c简析
  9. android:webView总结

随机推荐

  1. android启动画面
  2. Android(安卓)textview实现删除线
  3. [置顶] Android View系统学习文章汇总
  4. Android知识积累
  5. Android 4.3新特性——SElinux简介
  6. ubuntu16.10 AndroidStudio创建Virtual D
  7. Hybrid App开发问题记录
  8. android中设置全屏的方法
  9. Button、选择框、日期、时间控件
  10. android JNI之 'GetObjectClass' in some