利用HorizontalScrollView實現(xiàn)滑動頁面時的縮放效果
在前面的文章中也有關(guān)于 HorizontalScrollView 的使用:Android使用HorizontalScrollView實現(xiàn)水平滾動 。
這里主要實現(xiàn)的是向右滑動時,左側(cè)的視圖有逐漸放大,也會越來越清晰;向左滑動時,左側(cè)的視圖逐漸減小,逐漸變的模糊,且不移出屏幕左邊緣的效果。效果如下(可以在主頁面上的右側(cè)向右滑動都可以實現(xiàn)該效果):

這里需要用到自定義的 HorizontalScrollView ,讓其作為布局文件的根標簽。HorizontalScrollView 里面只能有一個子組件,所以要把左側(cè)的視圖布局文件包含在 HorizontalScrollView 的子組件里面。
activity_main.xml :
<com.crazy.reduce.ReduceSideslip xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/reduce_lay"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@drawable/bg"
android:scrollbars="none"
tools:context="com.crazy.reduce.MainActivity" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal" >
<include layout="@layout/item" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_01" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="toggleMenu"
android:text="點擊" />
</LinearLayout>
</LinearLayout>
</com.crazy.reduce.ReduceSideslip>
在 item.xml 布局文件的右邊有個 button 按鈕,這些都在 HorizontalScrollView 的子組件當中。而 item.xml 究竟是怎樣的布局也都不會影響到整個的滑動。
item.xml :
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:orientation="vertical" > <Button android:id="@+id/bt_b" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="50dp" android:text="一個不同的按鈕" /> <ImageView android:id="@+id/img" android:layout_width="wrap_content" android:layout_height="match_parent" android:scaleType="centerCrop" android:src="@drawable/bg_03" /> </LinearLayout> </RelativeLayout>
MainActivity.java :
package com.crazy.reduce;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends Activity {
private ReduceSideslip rs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rs = (ReduceSideslip)findViewById(R.id.reduce_lay);
}
public void toggleMenu(View v) {
rs.reduce();
}
}
自定義的 ReduceSideslip.java : 需要 nineoldandroids-2.4.0.jar 包,其下載地址
package com.crazy.reduce;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import com.nineoldandroids.view.ViewHelper;
public class ReduceSideslip extends HorizontalScrollView {
private int mScreenWidth; // 屏幕寬度
private int mMnuRightPadding = 300;
private int mMenuWidth; // 視圖寬度(左邊的視圖)
private int mHalfMenuWidth;
private boolean isOpen; // 標記菜單是否打開
private boolean once; // 是否已經(jīng)初始化回收菜單
private ViewGroup mMenu; // 左邊的視圖
private ViewGroup mContent; // 右邊的視圖
public ReduceSideslip(Context context, AttributeSet attrs) {
super(context, attrs);
mScreenWidth = context.getResources().getDisplayMetrics().widthPixels;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (!once) {
// 要與布局文件當中的一致
LinearLayout temp = (LinearLayout)getChildAt(0);
mMenu = (ViewGroup)temp.getChildAt(0);
mContent = (ViewGroup)temp.getChildAt(1);
mMenuWidth = mScreenWidth - mMnuRightPadding;
mHalfMenuWidth = mMenuWidth/2;
mMenu.getLayoutParams().width = mMenuWidth;
mContent.getLayoutParams().width = mScreenWidth;
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
// 在視圖計算完自身及子視圖的寬高后,重新排版
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
if (changed) {
// 隱藏菜單
this.scrollTo(mMenuWidth, 0);
once = true;
}
}
public void reduce(){
if (isOpen) {
closeMenu();
} else {
openMenu();
}
}
private void openMenu() {
if (isOpen) {
return;
}
// 和 scrollTo() 相似,但是要緩和些,
// 不像 scrollTo() 直接移動過去
this.smoothScrollTo(0, 0);
isOpen = true;
}
private void closeMenu() {
if (isOpen) {
this.smoothScrollTo(mMenuWidth, 0);
isOpen = false;
}
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
switch (ev.getAction()){
case MotionEvent.ACTION_UP: // 松開手
int scrollX = getScrollX(); // 水平滑動的距離
if (scrollX > mHalfMenuWidth) {
this.smoothScrollTo(mMenuWidth, 0);
isOpen = false;
} else {
this.smoothScrollTo(0, 0);
isOpen = true;
}
return true;
}
return super.onTouchEvent(ev);
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
// 左右視圖切換時的漸變范圍 (注意是 l 不是1(一))
float scale = l*1.0f/mMenuWidth; // 范圍值 (0, 1)
float leftScale = 1- 0.3f*scale; // 范圍值(0.7, 1)
float rightScale = 0.8f + 0.2f*scale; // 范圍值 (0.8, 1)
ViewHelper.setScaleX(mMenu, leftScale);
ViewHelper.setScaleY(mMenu, leftScale);
// 往右滑動時,左邊的視圖逐漸變亮
ViewHelper.setAlpha(mMenu, 0.6f + 0.4f * (1 - scale)); // (0.6, 1)
// 往左滑動時,左邊的視圖不用移除屏幕左邊界(可以不要)
ViewHelper.setTranslationX(mMenu, mMenuWidth * scale * 0.7f);
ViewHelper.setScaleX(mContent, rightScale);
ViewHelper.setScaleY(mContent, rightScale);
}
}
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android自定義View圖片按Path運動和旋轉(zhuǎn)
這篇文章主要為大家詳細介紹了Android自定義View圖片按Path運動和旋轉(zhuǎn),具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-01-01
Android系統(tǒng)的五種數(shù)據(jù)存儲形式實例(二)
Android系統(tǒng)有五種數(shù)據(jù)存儲形式,分別是文件存儲、SP存儲、數(shù)據(jù)庫存儲、contentprovider 內(nèi)容提供者、網(wǎng)絡(luò)存儲。本文介紹了Android系統(tǒng)的五種數(shù)據(jù)存儲形式,有興趣的可以了解一下。2016-12-12
Android RecyclerView實現(xiàn)點擊條目刪除
這篇文章主要為大家詳細介紹了Android RecyclerView實現(xiàn)點擊條目刪除,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-11-11
Android實現(xiàn)關(guān)機重啟的方法分享
這篇文章主要介紹了Android實現(xiàn)關(guān)機重啟的方法,需要的朋友可以參考下2014-02-02

