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

這里需要用到自定義的 HorizontalScrollView ,讓其作為布局文件的根標(biāo)簽。HorizontalScrollView 里面只能有一個(gè)子組件,所以要把左側(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="點(diǎn)擊" />
</LinearLayout>
</LinearLayout>
</com.crazy.reduce.ReduceSideslip>
在 item.xml 布局文件的右邊有個(gè) button 按鈕,這些都在 HorizontalScrollView 的子組件當(dāng)中。而 item.xml 究竟是怎樣的布局也都不會(huì)影響到整個(gè)的滑動(dòng)。
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="一個(gè)不同的按鈕" /> <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; // 標(biāo)記菜單是否打開
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) {
// 要與布局文件當(dāng)中的一致
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);
}
// 在視圖計(jì)算完自身及子視圖的寬高后,重新排版
@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() 直接移動(dòng)過去
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(); // 水平滑動(dòng)的距離
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);
// 左右視圖切換時(shí)的漸變范圍 (注意是 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);
// 往右滑動(dòng)時(shí),左邊的視圖逐漸變亮
ViewHelper.setAlpha(mMenu, 0.6f + 0.4f * (1 - scale)); // (0.6, 1)
// 往左滑動(dòng)時(shí),左邊的視圖不用移除屏幕左邊界(可以不要)
ViewHelper.setTranslationX(mMenu, mMenuWidth * scale * 0.7f);
ViewHelper.setScaleX(mContent, rightScale);
ViewHelper.setScaleY(mContent, rightScale);
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android開發(fā)之資源文件用法實(shí)例總結(jié)
這篇文章主要介紹了Android開發(fā)之資源文件用法,結(jié)合實(shí)例形式總結(jié)分析了Android開發(fā)過程中針對(duì)資源文件的常見操作技巧,需要的朋友可以參考下2016-02-02
Android自定義View圖片按Path運(yùn)動(dòng)和旋轉(zhuǎn)
這篇文章主要為大家詳細(xì)介紹了Android自定義View圖片按Path運(yùn)動(dòng)和旋轉(zhuǎn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01
Android系統(tǒng)的五種數(shù)據(jù)存儲(chǔ)形式實(shí)例(二)
Android系統(tǒng)有五種數(shù)據(jù)存儲(chǔ)形式,分別是文件存儲(chǔ)、SP存儲(chǔ)、數(shù)據(jù)庫存儲(chǔ)、contentprovider 內(nèi)容提供者、網(wǎng)絡(luò)存儲(chǔ)。本文介紹了Android系統(tǒng)的五種數(shù)據(jù)存儲(chǔ)形式,有興趣的可以了解一下。2016-12-12
Android RecyclerView實(shí)現(xiàn)點(diǎn)擊條目刪除
這篇文章主要為大家詳細(xì)介紹了Android RecyclerView實(shí)現(xiàn)點(diǎn)擊條目刪除,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11
Android實(shí)現(xiàn)關(guān)機(jī)重啟的方法分享
這篇文章主要介紹了Android實(shí)現(xiàn)關(guān)機(jī)重啟的方法,需要的朋友可以參考下2014-02-02
詳解Android性能優(yōu)化之啟動(dòng)優(yōu)化
一款應(yīng)用的第一印象很重要,第一印象往往決定了用戶的去留。打開一款應(yīng)用,如果速度很快,很順暢,那么很容易讓人覺得這款應(yīng)用背后的技術(shù)實(shí)力很強(qiáng),用戶潛意識(shí)中會(huì)對(duì)這款應(yīng)用更加的信賴。本文將詳細(xì)介紹Android性能優(yōu)化之啟動(dòng)優(yōu)化。2021-06-06

