上下拉伸图片-第三方开源-PullToZoomListViewEx 实现阻尼效果

本文要说的PullToZoomScrollViewEx 实现阻尼效果
什么是阻尼效果。简单解释就是一张图片,当你按住向下拖动,图片跟着拉升放大,当你松开时,回弹到原来的效果,在Android 上我们这种效果叫做阻尼效果。
这里我们用一个第三的 PullToZoomScrollViewEx 来实现,他的原理是自己定义的view 继承的我们ScrollView,设置我们初始的高度,然后实现下拉的动态监听,并且改写了事件分发机制,来对滑动,触摸事件的冲突处理。
下载地址:https://github.com/Frank-Zhu/PullZoomView

在Java代码中动态的为PullZoomView装载View:

该类提供了几个重要的方法。

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);//添加剩余部分

注意:添加头里边的背景布局必须设置该属性:
android:scaleType=”centerCrop”
(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()。

具体使用步骤:
一 : 在app build.gradil 里配置

  compile 'com.github.frank-zhu:pullzoomview:1.0.0'

二:在xml 文件里和其他控件一样使用 PullToZoomScrollViewEx
三: 用findViewById 在activity 里初始化;
四: 写我们需要添加的头部 布局,头部背景布局,剩余部分布局,
五 :初始化我们头部的高度

有以上步骤就可以实现,如果这时候编译出现以下异常

Error:Execution failed for task ':app:processDebugManifest'.
> Manifest merger failed : Attribute application@icon value=(@mipmap/ic_launcher) from AndroidManifest.xml:7:9-43
is also present at [com.github.frank-zhu:pullzoomview:1.0.0] AndroidManifest.xml:13:9-45 value=(@drawable/ic_launcher).
Suggestion: add 'tools:replace="android:icon"' to <application> element at AndroidManifest.xml:5:5-17:19 to override.
我们只需要在 清单文件的 application 加入 
     tools:replace="android:icon, android:theme" 

下边看具体代码:
Activity

package com.xiang.dampviewproject;

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;
import android.widget.Toast;

import com.ecloud.pulltozoomview.PullToZoomScrollViewEx;

/**
 * Created by Administrator on 2016/8/13.
 */
public class SecoundActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_secound);

        // 注意初始化顺序,不要弄乱,否则抛出运行时空指针
        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) {
                Toast.makeText(SecoundActivity.this, "我被点击了tv_test1", Toast.LENGTH_SHORT).show();
            }
        });

        scrollView.getPullRootView().findViewById(R.id.tv_test2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(SecoundActivity.this, "我被点击了tv_test2", Toast.LENGTH_SHORT).show();
            }
        });

        scrollView.getPullRootView().findViewById(R.id.tv_test3).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(SecoundActivity.this, "我被点击了tv_test3", Toast.LENGTH_SHORT).show();
            }
        });
        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);
    }

}


activity 布局

<?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">

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

activity 所需要的其他布局
head_view

<?xml version="1.0" encoding="utf-8"?>
<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: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="12sp" />

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

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

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

head_zoom_view 背景布局必须设置 android:scaleType=”centerCrop” 属性

<?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/a" />

content_view

<?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: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" />
</LinearLayout>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值