a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 系統 > 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開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 日本高清中文字幕 | 国产在线一级视频 | 九一视频在线播放 | 国产精品国产成人国产三级 | 日韩在线视频二区 | 日韩视频一区在线观看 | 在线观看av免费 | 亚洲综合99| 精品久久久久久久久久久久久久 | 在线亚洲自拍 | 精品婷婷 | 盗摄精品av一区二区三区 | 亚洲一区二区三区四区的 | 蜜桃视频在线观看www社区 | 色噜噜噜噜| 91亚洲精品一区 | 91精品国产综合久久精品 | 精品1区 | 久久午夜影院 | 成人亚洲 | 亚洲欧美日韩在线 | 国产成人av一区二区 | 日日爱视频 | 日本一区二区精品 | 狠狠操综合网 | 欧美精品第十页 | 亚洲最大成人免费视频 | 日韩黄色小视频 | 韩国av一区二区 | 中文字幕不卡在线观看 | 日本va | 久久精品免费一区二区三区 | 新超碰在线 | 国产艳妇av视国产精选av一区 | 日日躁夜夜躁狠狠躁aⅴ蜜 成人免费在线观看 | 99久久久国产精品 | 日本成人在线看 | a级淫片 | 91麻豆精品国产91久久久久久久久 | 超碰日韩在线 | h在线观看 |