Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

新浪微博風格下拉拉伸圖片:PullToZoomScrollViewEx 在 ScrollView 上的實作(下)

編輯:關於 Android 程式設計

上一篇談的是 PullZoomView 在 ListView 上的用法,這篇接著說明它在 ScrollView 上的對應版本:PullToZoomScrollViewEx。

專案下載:https://github.com/Frank-Zhu/PullZoomView

與 ListView 版本最大的差異在於載入方式:ScrollView 版是在 Java 程式碼中動態掛載各個 View,而不是透過 XML 屬性指定。

private void loadViewForPullToZoomScrollView(PullToZoomScrollViewEx scrollView) {
    View headView = LayoutInflater.from(this).inflate(R.layout.head_view, null);
    View zoomView = LayoutInflater.from(this).inflate(R.layout.head_zoom_view, null);
    View contentView = LayoutInflater.from(this).inflate(R.layout.content_view, null);
    scrollView.setHeaderView(headView);
    scrollView.setZoomView(zoomView);
    scrollView.setScrollContentView(contentView);
}

兩個必須留意的細節

(1)效果開關:三個 boolean 方法

所有頭部與縮放效果都可以自由啟停,方式就是切換以下方法的參數:

setParallax(true);      // true 有視差效果,false 無
setHideHeader(false);   // true 隱藏自訂 head view,false 顯示
setZoomEnabled(true);   // true 支援縮放,false 不支援

以上三行即為預設值。

(2)子 View 要透過 getPullRootView() 取得

PullZoomView 內部把子 View 包了一層,所以不能直接用 findViewById(),必須寫成 getPullRootView().findViewById(R.id.xxx),否則會拿到 null。

完整範例

主布局 activity_main.xml

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    xmlns:custom="https://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.ecloud.pulltozoomview.PullToZoomScrollViewEx
        android:id="@+id/scroll_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</RelativeLayout>

MainActivity.java

package com.zzw.testpullzoomview_scrollview;

import com.ecloud.pulltozoomview.PullToZoomScrollViewEx;
import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 注意初始化順序,顛倒會拋出空指標
        PullToZoomScrollViewEx scrollView = (PullToZoomScrollViewEx) findViewById(R.id.scroll_view);
        loadViewForPullToZoomScrollView(scrollView);

        // 子 View 必須透過 getPullRootView() 取得
        scrollView.getPullRootView().findViewById(R.id.tv_test).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("PullToZoomScrollViewEx", "onClick");
            }
        });

        setPullToZoomViewLayoutParams(scrollView);
    }

    private void loadViewForPullToZoomScrollView(PullToZoomScrollViewEx scrollView) {
        View headView = LayoutInflater.from(this).inflate(R.layout.head_view, null);
        View zoomView = LayoutInflater.from(this).inflate(R.layout.head_zoom_view, null);
        View contentView = LayoutInflater.from(this).inflate(R.layout.content_view, null);
        scrollView.setHeaderView(headView);
        scrollView.setZoomView(zoomView);
        scrollView.setScrollContentView(contentView);
    }

    // 設定頭部 View 的寬高
    private void setPullToZoomViewLayoutParams(PullToZoomScrollViewEx scrollView) {
        DisplayMetrics localDisplayMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(localDisplayMetrics);
        int mScreenWidth = localDisplayMetrics.widthPixels;
        LinearLayout.LayoutParams localObject =
                new LinearLayout.LayoutParams(mScreenWidth, (int) (1.5f * mScreenWidth));
        scrollView.setHeaderLayoutParams(localObject);
    }
}

head_view.xml:頭部資訊層

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:id="@+id/layout_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="bottom"
    android:gravity="bottom">

    <ImageView
        android:id="@+id/iv_user_head"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/tv_user_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/iv_user_head"
        android:layout_centerHorizontal="true"
        android:text="新浪微博"
        android:textColor="#ffffff"
        android:textSize="16sp" />

    <LinearLayout
        android:id="@+id/ll_action_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="#33ffffff"
        android:padding="5dip">

        <TextView
            android:id="@+id/tv_register"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="註冊"
            android:textColor="#ffffff"
            android:textSize="16sp" />

        <TextView
            android:id="@+id/tv_login"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="登入"
            android:textColor="#ffffff"
            android:textSize="16sp" />
    </LinearLayout>
</RelativeLayout>

head_zoom_view.xml:真正被縮放的那張圖

<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="https://schemas.android.com/apk/res/android"
    android:id="@+id/imageView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center_horizontal"
    android:scaleType="centerCrop"
    android:src="@drawable/a" />

content_view.xml:可捲動的內容區

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:padding="10dp"
        android:text="test"
        android:textSize="16sp" />
    <!-- 重複多個 TextView 以撐出可捲動的高度 -->
</LinearLayout>

實務上的關鍵觀念

如果決定用 ScrollView 包住專案裡既有的子 View,那麼 content_view.xml 就是那些 View 的「父」容器,真正的業務布局要在這裡展開。理解 headView(正常顯示、只會位移)與 zoomView(會動態縮放、墊在底層)這兩者的分工,這個元件其實就不難用了。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved