Android PullZoomView:PullToZoomScrollViewEx(2)

简介: Android PullZoomView:PullToZoomScrollViewEx(2)在附录文章1中,介绍了Android PullZoomView在ListView中的实现:PullToZoomListViewEx,事实上,Android PullZoomView亦可在ScrollView实现,Android PullZoomView在ScrollView的实现是:PullToZoomScrollViewEx。


Android PullZoomView:PullToZoomScrollViewEx(2)

在附录文章1中,介绍了Android PullZoomView在ListView中的实现:PullToZoomListViewEx,事实上,Android PullZoomView亦可在ScrollView实现,Android PullZoomView在ScrollView的实现是:PullToZoomScrollViewEx。本文介绍PullToZoomScrollViewEx。
PullToZoomScrollViewEx运行效果如图所示:


在附录文章1中,PullToZoomListViewEx 是在xml布局文件中,写头部View以及zoom的View,而本文要说的PullToZoomScrollViewEx则以另外一种方式在Java代码中动态的为PullZoomView装载View:

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)所有Android PullZoomView的头部及缩放效果都可以关闭或者开启,具体方式就是通过改变设置各种方法的true或false值。以下是比较重要的几个方法:

setParallax(boolean b);
true则有视差效果,false则无。


setHideHeader(boolean b);
true则隐藏自己定义的head view,false则显示。


setZoomEnabled(boolean b);
true支持缩放,false不支持缩放。


默认的,
setParallax(true);
setHideHeader(false);
setZoomEnabled(true);


(2)PullZoomView中嵌套的子View,需要通过getPullRootView().findViewById(R.id.xxxx)这样的方式找出来,而不是直接的findViewById()。


下面给出一个完整例子加以说明。
先写一个布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="zhangphil.listview.MainActivity" >

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

</RelativeLayout>



Java代码:

package zhangphil.listview;

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);//注意初始化顺序,不要弄乱,否则抛出运行时空指针

		scrollView.getPullRootView().findViewById(R.id.tv_test1).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Log.d("PullToZoomScrollViewEx", "onClick");
			}
		});

		scrollView.getPullRootView().findViewById(R.id.tv_test2).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Log.e("PullToZoomScrollViewEx", "onClick");
			}
		});

		scrollView.getPullRootView().findViewById(R.id.tv_test3).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 mScreenHeight = localDisplayMetrics.heightPixels;
		int mScreenWidth = localDisplayMetrics.widthPixels;
		LinearLayout.LayoutParams localObject = new LinearLayout.LayoutParams(mScreenWidth,
				(int) (9.0F * (mScreenWidth / 16.0F)));
		scrollView.setHeaderLayoutParams(localObject);
	}
}

java代码需要的子布局:

head_view.xml:

<RelativeLayout xmlns:android="http://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:textSize="12sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/iv_user_head"
        android:layout_centerHorizontal="true"
        android:text="http://blog.csdn.net/zhangphil"
        android:textColor="#ffffff" />

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

        <TextView
            android:id="@+id/tv_register"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="注册"
            android:layout_weight="1"
            android:textSize="12sp"
            android:gravity="center"
            android:layout_gravity="center"
            android:textColor="#ffffff" />

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


head_zoom_view.xml:

<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://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/image" />
head_zoom_view其实就放了一张可供缩放拉伸的图片。



content_view.xml:

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

    <TextView
        android:id="@+id/tv_test1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:padding="20dp"
        android:text="test1"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/tv_test2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:padding="20dp"
        android:text="test2"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/tv_test3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:padding="20dp"
        android:text="test3"
        android:textSize="20sp" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:padding="20dp"
        android:text="test4"
        android:textSize="20sp" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:padding="20dp"
        android:text="test5"
        android:textSize="20sp" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#eeeeee" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:padding="20dp"
        android:text="test1"
        android:textSize="20sp" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:padding="20dp"
        android:text="test2"
        android:textSize="20sp" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:padding="20dp"
        android:text="test3"
        android:textSize="20sp" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:padding="20dp"
        android:text="test4"
        android:textSize="20sp" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:padding="20dp"
        android:text="test5"
        android:textSize="20sp" />

</LinearLayout>

实际开发中,如果确定要用ScrollView包括自己项目中的子View,那么content_view.xml就是其他View的装载“父”布局。重点需要在content_view.xml中展开。



静态截图1,初始化状态:



下拉状态:




素材Image.jpg的原图:



代码运行效果如前文动图所示。


附录:
【1】《Android PullZoomView:PullToZoomListViewEx(1)》链接地址:http://blog.csdn.net/zhangphil/article/details/50199833
【2】Android PullZoomView在github上的项目主页:https://github.com/Frank-Zhu/PullZoomView

相关文章
|
Android开发 Java 数据格式
Android PullZoomView:PullToZoomListViewEx(1)
 Android PullZoomView:PullToZoomListViewEx(1) Android PullZoomView是github上面的一个第三方开源项目,该项目实现的功能被新浪微博的移动端广泛使用,其效果就是,当用户在下拉过程中,头部的图片会有一定的拉伸,当用户松开时候,图片又收缩复位,其效果如动态图所示: PullZoomView要实现两类,一类是典型的Android ListView,另外一类是Android 的scroll view。
1074 0
|
7月前
|
移动开发 前端开发 Android开发
【02】建立各项目录和页面标准化产品-vue+vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
【02】建立各项目录和页面标准化产品-vue+vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
1383 12
【02】建立各项目录和页面标准化产品-vue+vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
|
7月前
|
移动开发 JavaScript 应用服务中间件
【06】优化完善落地页样式内容-精度优化-vue加vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
【06】优化完善落地页样式内容-精度优化-vue加vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
1004 5
【06】优化完善落地页样式内容-精度优化-vue加vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
|
7月前
|
移动开发 Rust JavaScript
【01】首页建立-vue+vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
【01】首页建立-vue+vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
1098 4
【01】首页建立-vue+vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
|
8月前
|
开发工具 Android开发
X Android SDK file not found: adb.安卓开发常见问题-Android SDK 缺少 `adb`(Android Debug Bridge)-优雅草卓伊凡
X Android SDK file not found: adb.安卓开发常见问题-Android SDK 缺少 `adb`(Android Debug Bridge)-优雅草卓伊凡
870 11
X Android SDK file not found: adb.安卓开发常见问题-Android SDK 缺少 `adb`(Android Debug Bridge)-优雅草卓伊凡
|
7月前
|
移动开发 Android开发
【03】建立隐私关于等相关页面和内容-vue+vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
【03】建立隐私关于等相关页面和内容-vue+vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
376 0
|
8月前
|
Java 开发工具 Maven
【01】完整的安卓二次商业实战-详细的初级步骤同步项目和gradle配置以及开发思路-优雅草伊凡
【01】完整的安卓二次商业实战-详细的初级步骤同步项目和gradle配置以及开发思路-优雅草伊凡
1118 6
|
10月前
|
安全 数据库 Android开发
在Android开发中实现两个Intent跳转及数据交换的方法
总结上述内容,在Android开发中,Intent不仅是活动跳转的桥梁,也是两个活动之间进行数据交换的媒介。运用Intent传递数据时需注意数据类型、传输大小限制以及安全性问题的处理,以确保应用的健壯性和安全性。
649 11
|
JavaScript Linux 网络安全
Termux安卓终端美化与开发实战:从下载到插件优化,小白也能玩转Linux
Termux是一款安卓平台上的开源终端模拟器,支持apt包管理、SSH连接及Python/Node.js/C++开发环境搭建,被誉为“手机上的Linux系统”。其特点包括零ROOT权限、跨平台开发和强大扩展性。本文详细介绍其安装准备、基础与高级环境配置、必备插件推荐、常见问题解决方法以及延伸学习资源,帮助用户充分利用Termux进行开发与学习。适用于Android 7+设备,原创内容转载请注明来源。
4664 77
|
10月前
|
移动开发 Java 编译器
Kotlin与Jetpack Compose:Android开发生态的演进与架构思考
本文从资深Android工程师视角深入分析Kotlin与Jetpack Compose在Android系统中的技术定位。Kotlin通过空安全、协程等特性解决了Java在移动开发中的痛点,成为Android官方首选语言。Jetpack Compose则引入声明式UI范式,通过重组机制实现高效UI更新。两者结合不仅提升开发效率,更为跨平台战略和现代架构模式提供技术基础,代表了Android开发生态的根本性演进。
431 0

热门文章

最新文章