国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

利用HorizontalScrollView實現滑動頁面時的縮放效果

2019-10-21 21:35:27
字體:
來源:轉載
供稿:網友

在前面的文章中也有關于 HorizontalScrollView 的使用:Android使用HorizontalScrollView實現水平滾動 。

這里主要實現的是向右滑動時,左側的視圖有逐漸放大,也會越來越清晰;向左滑動時,左側的視圖逐漸減小,逐漸變的模糊,且不移出屏幕左邊緣的效果。效果如下(可以在主頁面上的右側向右滑動都可以實現該效果):

HorizontalScrollView,滑動,縮放

這里需要用到自定義的 HorizontalScrollView ,讓其作為布局文件的根標簽。HorizontalScrollView 里面只能有一個子組件,所以要把左側的視圖布局文件包含在 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;  // 是否已經初始化回收菜單  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); }}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 阜南县| 内江市| 南部县| 化州市| 五常市| 大余县| 丹东市| 隆林| 渝北区| 聂荣县| 宜丰县| 阳城县| 冀州市| 上犹县| 福建省| 依安县| 田东县| 五常市| 界首市| 余干县| 容城县| 邯郸县| 盐城市| 开平市| 平定县| 新野县| 自贡市| 芮城县| 当阳市| 勐海县| 梨树县| 阿坝| 鹤峰县| 淮阳县| 紫阳县| 双辽市| 洛浦县| 玉林市| 乐清市| 中牟县| 娱乐|