高仿墨跡天氣下拉展開圖片,高仿墨跡下拉展開
簡介
最近比較閑,就多學習了下,關鍵是不看點東西,就犯困啊。墨跡天氣這個應用有不少地方需要學習的,這篇文章呢, 說一下他的“我”Tab頁下拉展開圖片展示效果,如果留意的話, 像QQ的社交動向更新也有差不多的效果。
程式碼分析
代碼比較簡單了,就重寫了一個ScrollView類,先說說他的原理吧,我是先根據id拿到這個ImageView,然後獲得他的TopMargin也就是遮掩後的位移值,在觸摸的時候,對ImageView的TopMargin進行改變產生效果,鬆手的時候搞個屬性動畫讓他還原到以前的位置就Ok了。好了。 原理就是這樣了。
先看看布局xml吧
<com.wjj.imagepull.PullScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:background="@android:color/white" android:id="@+id/scroll_view" android:scrollbars="none" android:layout_height="match_parent"> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <ImageView android:id="@+id/pull_img" android:layout_marginTop="-50dp" android:src="@drawable/personal_back" android:layout_width="match_parent" android:scaleType="fitXY" android:layout_height="250dp" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/feed_fengfeng" android:scaleType="fitXY" android:layout_centerInParent="true" /> </RelativeLayout>//此處省略...... </LinearLayout></com.wjj.imagepull.PullScrollView>
大家看到這個設定了吧android:layout_marginTop="-50dp"這個就是要遮掩的多少了,還有這個android:id="@+id/pull_img" 這個id,別亂改了, 我就是根據這個來拿的這個ImageView的。
上代碼了
public class PullScrollView extends ScrollView implements ViewTreeObserver.OnGlobalLayoutListener { View view; int srcTopMargion; float lastY; float offsetY; public PullScrollView(Context context, AttributeSet attrs) { super(context, attrs); ViewTreeObserver observer = getViewTreeObserver(); if (null != observer) observer.addOnGlobalLayoutListener(this); } @Override protected void onFinishInflate() { super.onFinishInflate(); view = findViewById(R.id.pull_img); } @Override public boolean onTouchEvent(MotionEvent ev) { int action = ev.getAction(); float y = ev.getY(); Log.d("onTouchEvent", "action=" + action + ",y=" + y); MarginLayoutParams params = (MarginLayoutParams) view.getLayoutParams(); switch (action) { case MotionEvent.ACTION_DOWN: lastY = y; break; case MotionEvent.ACTION_MOVE: //計算滑動y方向位移值 offsetY = y - lastY; //向下移動 if (offsetY > 0) { //滑動到看到所有圖片展示,交給原來的邏輯處理 if (params.topMargin == 0) { return super.onTouchEvent(ev); } //在不是下拉圖片的時候,向下移動,交給原來的邏輯處理 if (getScrollY() != 0) { return super.onTouchEvent(ev); } //可以下拉圖片的情況 params.topMargin += offsetY / 10; Log.d("onTouchEvent", "topMargin" + params.topMargin + ",lastY=" + lastY + ",y=" + y + ",offsetY" + offsetY); if (params.topMargin >= 0) { params.topMargin = 0; } view.setLayoutParams(params); invalidate(); } lastY = y; break; case MotionEvent.ACTION_UP: //不和原始margion位移一樣的時候 if (params.topMargin != -srcTopMargion) { Log.d("ACTION_UP", "moveY=" + (srcTopMargion + params.topMargin)); //滾動原始位移值和現在位移值之間的差值 eg:3~10 ObjectAnimator animator = ObjectAnimator.ofInt(this, "moveY", params.topMargin, -srcTopMargion); animator.setDuration(200); animator.setInterpolator(new LinearInterpolator()); animator.start(); } break; } return super.onTouchEvent(ev); } /** * 設定移動中的Y值 * * @param value */ public void setMoveY(int value) { MarginLayoutParams params = (MarginLayoutParams) view.getLayoutParams(); params.topMargin = value; Log.d("computeScroll", "topMargin=" + params.topMargin); view.setLayoutParams(params); invalidate(); } @Override public void onGlobalLayout() { MarginLayoutParams params = (MarginLayoutParams) view.getLayoutParams(); srcTopMargion = -params.topMargin; Log.d("srcTopMargion", "" + srcTopMargion); getViewTreeObserver() .removeGlobalOnLayoutListener(this); }}在PullScrollView構造方法裡面註冊了OnGlobalLayoutListener來監聽Tree樹改變後的回調,用來擷取ImageView的topMargin作為以後還原的目標值,在onFinishInflate裡面擷取這個view,view = findViewById(R.id.pull_img);
onTouchEvent就是我們的主戰場了, 邏輯都在這了,先判斷在ACTION_MOVE中,是不是向下滑動,如果是的話,有2種情況是需要排除的, 就是不進行topMargin改變的,是哪些呢?
第一個是topMargin都滑到0了, 就不要滑了吧。
第二個是在其他地方滑動的時候,舉個例子吧,就是正常的往下滑的時候,不做處理。
除了這2種情況,下面就是topMargin的改變了,這裡我加了一個offsetY / 10的除以10的阻值,就是防止滑的太快了, 這個你可以進行自訂的。
當我抬手的時候,假如說我的topMargin和以前的初始值不一樣的時候,還記得吧,咱們在onGlobalLayout取得的那個值,需要做的就是啟動一個動畫平移滑動到上面了。
當自動滑動的時候,這裡用了ObjectAnimator屬性動畫,其實當時用的是Scroll,為什麼又不用他了呢,因為重寫的computeScroll方法,如果在裡面處理的話,會和自己的滾動有衝突,感興趣的可以試試。
還有一個setMoveY方法,這個就是屬性動畫調用的那個方法了,根據線性插值計算後返回的值,然後在用於改變topMargin,直到恢複到剛開始的狀態。
結束語
好了,這個項目就算完了,不過我只在我的小米手機上測試了一下,其他解析度不知道會不會有問題,大家可以測測看。
Github地址
https://github.com/wu928320442/ImagePull