顶部图片放大回弹效果Scrollview ---- 各应用中常见的自定义View 解析

原理并不难.  代码量也不大.  非常简洁 .  先来个效果图

再上一波代码.

public class SpecialScrollView extends ScrollView implements ViewTreeObserver.OnPreDrawListener {
    private static final String TAG = "SpecialScrollView";
    private int mOriginalHeight;
    private int drawableHeight;
    private ImageView mImage;
    private float mLastY;
    private boolean isMeasured =false;
    public SpecialScrollView(Context context) {
        super(context);
    }

    public SpecialScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public SpecialScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    /**
     * 设置ImageView图片, 拿到引用
     * @param mImage
     */
    public void setParallaxImage(ImageView mImage) {
        this.mImage = mImage;
        getViewTreeObserver().addOnPreDrawListener(this);

    }

    @Override
    protected boolean overScrollBy(int deltaX, int deltaY, int scrollX, int scrollY, int scrollRangeX, int scrollRangeY, int maxOverScrollX, int maxOverScrollY, boolean isTouchEvent) {
        LogUtils.i(TAG, "deltaY: " + deltaY + " scrollY: " + scrollY + " scrollRangeY: " + scrollRangeY
                + " maxOverScrollY: " + maxOverScrollY + " isTouchEvent: " + isTouchEvent);
        // 手指拉动 并且 是下拉
        if(isTouchEvent && deltaY < 0 && mImage!=null ){
            // 把拉动的瞬时变化量的绝对值交给mIamge, 就可以实现放大效果
            if(mImage.getHeight() <= drawableHeight ){
                int newHeight = (int) (mImage.getHeight() + Math.abs(deltaY / 3.0f));
                // 高度不超出图片最大高度时,才让其生效
                mImage.getLayoutParams().height = newHeight;
                mImage.requestLayout();
            }
        }

        return super.overScrollBy(deltaX, deltaY, scrollX, scrollY, scrollRangeX, scrollRangeY, maxOverScrollX, maxOverScrollY, isTouchEvent);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_UP:
                // 执行回弹动画,  属性动画\值动画
                // 从当前高度mImage.getHeight(), 执行动画到原始高度mOriginalHeight
                if (mImage!=null){
                    final int startHeight = mImage.getHeight();
                    final int endHeight = mOriginalHeight;
                    valueAnimator(startHeight, endHeight);
                }

                break;
        }
        return super.onTouchEvent(ev);
    }

    private void valueAnimator(final int startHeight, final int endHeight) {
        ValueAnimator mValueAnim = ValueAnimator.ofInt(1);
        mValueAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

            @Override
            public void onAnimationUpdate(ValueAnimator mAnim) {
                float fraction = mAnim.getAnimatedFraction();
                // percent 0.0 -> 1.0
                Log.d(TAG, "fraction: " +fraction);
                Integer newHeight = evaluate(fraction, startHeight, endHeight);

                mImage.getLayoutParams().height = newHeight;
                mImage.requestLayout();
            }
        });

        mValueAnim.setInterpolator(new OvershootInterpolator());
        mValueAnim.setDuration(500);
        mValueAnim.start();
    }

    public Integer evaluate(float fraction, Integer startValue, Integer endValue) {
        int startInt = startValue;
        return (int)(startInt + fraction * (endValue - startInt));
    }

    @Override
    public boolean onPreDraw() {
        if (!isMeasured) {
            mOriginalHeight = mImage.getHeight(); //
            drawableHeight = mImage.getDrawable().getIntrinsicHeight(); //
            isMeasured = true;
            LogUtils.i(TAG, "height: " + mOriginalHeight + " drawableHeight: " + drawableHeight);
        }
        return true;
    }
}

Layout布局

<?xml version="1.0" encoding="utf-8"?>
<com.Imy.Fuli.View.SpecialScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/sp_scrollview"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <ImageView
            android:src="@mipmap/bizhi"
            android:scaleType="centerCrop"
            android:id="@+id/infor_icon_bg"
            android:layout_width="match_parent"
            android:layout_height="250dp" />
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <include layout="@layout/fragment_me_having_line"></include>

            <View
                android:layout_width="match_parent"
                android:layout_height="1px"
                android:background="@color/halving_line"/>
            <RelativeLayout
                android:background="@drawable/selector_fragment_me_item_bg"
                android:id="@+id/head_icon_setting"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="8dp">

                <TextView
                    android:paddingTop="1dp"
                    android:layout_gravity="center"
                    android:paddingLeft="15dp"
                    android:textColor="@color/color_grey_e0555555"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textSize="16dp"
                    android:text="头像"
                    android:layout_centerVertical="true"
                    android:layout_toRightOf="@+id/collect_icon"
                    android:layout_toEndOf="@+id/collect_icon" />
                <FrameLayout
                    android:layout_marginRight="15dp"
                    android:layout_centerVertical="true"
                    android:layout_alignRight="@+id/arrow_info"
                    android:layout_width="50dp"
                    android:layout_height="50dp">
                    <com.Imy.Fuli.View.CircleImageView
                        android:id="@+id/my_uer_icon"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:src="@mipmap/user_default_photo"
                        app:civ_border_color="@color/white"
                        app:civ_border_width="2dp" />
                </FrameLayout>

                <ImageView
                    android:id="@+id/arrow_info"
                    android:layout_marginRight="10dp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@mipmap/right_arrow"
                    android:layout_centerVertical="true"
                    android:layout_alignParentRight="true"
                    android:layout_alignParentEnd="true" />
            </RelativeLayout>
            <View
                android:layout_width="match_parent"
                android:layout_height="1px"
                android:background="@color/halving_line"/>
            <include layout="@layout/fragment_me_having_line"></include>
            <View
                android:layout_width="match_parent"
                android:layout_height="1px"
                android:background="@color/halving_line"/>
            <LinearLayout
                android:orientation="vertical"
                android:background="@color/white"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
                <RelativeLayout
                    android:background="@drawable/selector_fragment_me_item_bg"
                    android:id="@+id/personal_info_name_layout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:padding="8dp">

                    <TextView
                        android:paddingTop="1dp"
                        android:layout_gravity="center"
                        android:paddingLeft="15dp"
                        android:textColor="@color/color_grey_e0555555"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textSize="16dp"
                        android:text="昵称"
                        android:layout_centerVertical="true"
                        android:layout_toRightOf="@+id/collect_icon"
                        android:layout_toEndOf="@+id/collect_icon" />
                    <ImageView
                        android:id="@+id/right_arrow_name"
                        android:layout_marginRight="10dp"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@mipmap/right_arrow"
                        android:layout_centerVertical="true"
                        android:layout_alignParentRight="true"
                        android:layout_alignParentEnd="true" />
                    <TextView
                        android:id="@+id/personal_info_user_name"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Imy"
                        style="@style/text_style"
                        android:layout_marginRight="15dp"
                        android:layout_centerVertical="true"
                        android:layout_toLeftOf="@+id/right_arrow_name"
                        android:layout_toStartOf="@+id/right_arrow_name" />

                </RelativeLayout>
                <View
                    android:layout_width="match_parent"
                    android:layout_height="1px"
                    android:layout_marginLeft="24dp"
                    android:background="@color/halving_line"/>
                <RelativeLayout
                    android:background="@drawable/selector_fragment_me_item_bg"
                    android:id="@+id/personal_info_area_layout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:padding="8dp">

                    <TextView
                        android:paddingTop="1dp"
                        android:layout_gravity="center"
                        android:paddingLeft="15dp"
                        android:textColor="@color/color_grey_e0555555"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textSize="16dp"
                        android:text="地区"
                        android:layout_centerVertical="true"
                        android:layout_toRightOf="@+id/collect_icon"
                        android:layout_toEndOf="@+id/collect_icon" />
                    <ImageView
                        android:id="@+id/right_arrow_area"
                        android:layout_marginRight="10dp"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@mipmap/right_arrow"
                        android:layout_centerVertical="true"
                        android:layout_alignParentRight="true"
                        android:layout_alignParentEnd="true" />
                    <TextView
                        android:id="@+id/personal_info_address"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text=""
                        style="@style/text_style"
                        android:layout_marginRight="15dp"
                        android:layout_centerVertical="true"
                        android:layout_toLeftOf="@+id/right_arrow_area"
                        android:layout_toStartOf="@+id/right_arrow_area" />

                </RelativeLayout>
                <View
                    android:layout_width="match_parent"
                    android:layout_height="1px"
                    android:layout_marginLeft="24dp"
                    android:background="@color/halving_line"/>
                <RelativeLayout
                    android:background="@drawable/selector_fragment_me_item_bg"
                    android:id="@+id/personal_info_sex_layout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:padding="8dp">

                    <TextView
                        android:paddingTop="1dp"
                        android:layout_gravity="center"
                        android:paddingLeft="15dp"
                        android:textColor="@color/color_grey_e0555555"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textSize="16dp"
                        android:text="性别"
                        android:layout_centerVertical="true"
                        android:layout_toRightOf="@+id/collect_icon"
                        android:layout_toEndOf="@+id/collect_icon" />
                    <ImageView
                        android:id="@+id/right_arrow_sex"
                        android:layout_marginRight="10dp"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@mipmap/right_arrow"
                        android:layout_centerVertical="true"
                        android:layout_alignParentRight="true"
                        android:layout_alignParentEnd="true" />
                    <TextView
                        android:id="@+id/personal_info_user_sex"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        style="@style/text_style"
                        android:layout_marginBottom="1dp"
                        android:layout_marginRight="15dp"
                        android:layout_centerVertical="true"
                        android:layout_toLeftOf="@+id/right_arrow_sex"
                        android:layout_toStartOf="@+id/right_arrow_sex" />
                </RelativeLayout>
                <View
                    android:layout_width="match_parent"
                    android:layout_height="1px"
                    android:layout_marginLeft="24dp"
                    android:background="@color/halving_line"/>
                <RelativeLayout
                    android:background="@drawable/selector_fragment_me_item_bg"
                    android:id="@+id/personal_info_age_layout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:padding="8dp">

                    <TextView
                        android:paddingTop="1dp"
                        android:layout_gravity="center"
                        android:paddingLeft="15dp"
                        android:textColor="@color/color_grey_e0555555"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textSize="16dp"
                        android:text="年龄"
                        android:layout_centerVertical="true"
                        android:layout_toRightOf="@+id/collect_icon"
                        android:layout_toEndOf="@+id/collect_icon" />
                    <ImageView
                        android:id="@+id/right_arrow_birthday"
                        android:layout_marginRight="10dp"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@mipmap/right_arrow"
                        android:layout_centerVertical="true"
                        android:layout_alignParentRight="true"
                        android:layout_alignParentEnd="true" />
                    <TextView
                        android:id="@+id/personal_info_age_tv"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text=""
                        style="@style/text_style"
                        android:layout_marginRight="15dp"
                        android:layout_centerVertical="true"
                        android:layout_toLeftOf="@+id/right_arrow_birthday"
                        android:layout_toStartOf="@+id/right_arrow_birthday" />
                </RelativeLayout>
                <View
                    android:layout_width="match_parent"
                    android:layout_height="1px"
                    android:background="@color/halving_line"/>
            </LinearLayout>

            <include layout="@layout/fragment_me_having_line"></include>
            <View
                android:layout_width="match_parent"
                android:layout_height="1px"
                android:background="@color/halving_line"/>
            <RelativeLayout
                android:background="@drawable/selector_fragment_me_item_bg"
                android:id="@+id/personal_info_signature_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="8dp">
                <TextView
                    android:paddingTop="1dp"
                    android:layout_gravity="center"
                    android:paddingLeft="15dp"
                    android:textColor="@color/color_grey_e0555555"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textSize="16dp"
                    android:text="个性签名"
                    android:layout_centerVertical="true"
                    android:layout_toRightOf="@+id/collect_icon"
                    android:layout_toEndOf="@+id/collect_icon" />
                <ImageView
                    android:id="@+id/right_arrow_signature"
                    android:layout_marginRight="10dp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@mipmap/right_arrow"
                    android:layout_centerVertical="true"
                    android:layout_alignParentRight="true"
                    android:layout_alignParentEnd="true" />
                <RelativeLayout
                    android:layout_marginRight="15dp"
                    android:layout_centerVertical="true"
                    android:layout_toLeftOf="@+id/right_arrow_signature"
                    android:layout_toStartOf="@+id/right_arrow_signature"
                    android:layout_width="200dp"
                    android:layout_height="wrap_content">
                    <TextView
                        android:id="@+id/personal_info_signature"
                        android:layout_alignParentRight="true"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="介绍下自己吧"
                        style="@style/text_style"
                        />
                </RelativeLayout>

            </RelativeLayout>
            <View
                android:layout_width="match_parent"
                android:layout_height="1px"
                android:background="@color/halving_line"/>
        </LinearLayout>

    </LinearLayout>
</com.Imy.Fuli.View.SpecialScrollView>

  

初始化:

private void iniID() {    mSpecialScrollView = (SpecialScrollView) findViewById(R.id.sp_scrollview);    mImage = (ImageView) findViewById(R.id.infor_icon_bg);    mSpecialScrollView.setParallaxImage(mImage);}

快过年了 码代码的心思都没了.~ 哎呀  布局文件 可无视. 懒得写demo了.

时间: 2024-08-24 02:23:10

顶部图片放大回弹效果Scrollview ---- 各应用中常见的自定义View 解析的相关文章

Android仿IOS回弹效果 ScrollView回弹 总结

Android仿IOS回弹效果  ScrollView回弹 总结 应项目中的需求  需要仿IOS 下拉回弹的效果 , 我在网上搜了很多 大多数都是拿scrollview 改吧改吧 试了一些  发现总有点小问题 下面的代码是我对大家发布的做了点小修改   觉得没太大问题 package com.example.myscrollview; import android.content.Context; import android.graphics.Rect; import android.util

js+jquery+html实现在三种不通的情况下,点击图片放大的效果

js+jquery+html实现在三种不通的情况下,点击图片放大的效果. 三种情况分别是:图片的父元素宽高固定;  图片的宽高固定;  图片的父元素宽固定,高度不固定 第一种情况:图片的父元素宽高固定: 1 <!DOCTYPE html> 2 <html lang="en"> 3 4 <head> 5 <meta charset="UTF-8"> 6 <title>Title</title> 7

css3伪放大镜(图片放大动画)效果(鼠标移入圆形区域放大图片)

源码: <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>css3伪放大镜(图片放大动画)效果</title><style>.gallery{list-style:none}.gallery:before,.gallery__item:last-child{position:fixed;top:50

如何实现鼠标悬停图片放大的效果。

在网页上我们经常看到鼠标悬停在一个图片上,这张图片会慢慢的放大,感觉是像放大镜放大的效果,当鼠标移开的时候,图片有恢复原来的样子,今天就实现这种效果. 实现原理以思路: 1,首先这是一张图片在悬停时放大也就是改变大小(宽,高)实现的. 2,一张图片在放大的时候会根据其定位(如在div里面的图片会以div的左上角为基准扩大宽和高)来放大的,因此如果我们不去为图片添加相对定位并且不去调节扩大后的位置,他的放大会是向一边的,因此我们必须考虑其放大后的位置. 3,放大的效果是要用动画实现的. 代码: <

Android -- 自定义ScrollView实现放大回弹效果

1,刚刚在别人开源的项目中看到了一个挺不错的用户体验,效果图如下: 2,那下面我们就来实现一下,首先看一下布局,由于一般只是我们包含头像的那部分方法,所以这里我们要把布局分成两部分,对应的布局文件效果图如下: 3,自定义ScrollView 第一步:创建一个类,继承自ScrollView,重写相应的构造函数 public class ZoomInScrollView extends ScrollView { public ZoomInScrollView(Context context) { t

iOS----实现scrollView或者scrollView的子类下拉图片放大的效果

代码是通过Tableview来说明的,用在其他情况下同样适用 - (void)viewDidLoad { [super viewDidLoad]; _imageview = [[UIImageView alloc]init]; _imageview.image = [UIImage imageNamed:@"F2.jpg"]; self.imageview.frame =CGRectMake(0, -150, self.tableView.frame.size.width, 150);

第六十七篇、OC_UITableView head下拉图片放大的效果

(一) 布置UITableview 我们首先要通过设置UITableview的内容偏移 self.tableView.contentInset 来为图片视图留出位置,这里我们的图片高度暂定为280 const CGFloat contentInset = 280; @interface ViewController ()<UITableViewDelegate,UITableViewDataSource> @property (nonatomic, strong) UITableView *t

Android PullToZoomListView实现放大回弹效果

版本号:1.0 日期:2014.8.4 版权:? 2014 kince 转载注明出处 之前看过一篇文章,链接是:能够下拉缩放HeaderView的ListView:PullToZoomInListView. 说的就是PullToZoomListView.只是这篇文章有个地方须要勘误,就是PullToZoomListView这个控件尽管是github上一个开源项目.只是最美应用并非使用这个开源项目,而是这个开源项目反编译了最美应用的代码. 可是,它这个代码是有问题的,当手指在屏幕上滑动的时候会引发

浅谈CSS和JQuery实现鼠标悬浮图片放大效果

对于刚刚学习网页前台设计的同学一定对图片的处理非常苦恼,那么这里简单的讲解一下几个图片处理的实例. 以.net为平台,微软的Visual Studio 2013为开发工具,当然前台技术还是采用CSS3和HTML,Java的小伙伴不要绕道~~~ 言归正传,那么我们首先要完成什么样的图片处理呢? 一.CSS3图片的放大 css3中,有一个属性transform,官方的解释是:允许向元素应用2D或3D的转换.这些转换当然就包含旋转.缩放.移动或倾斜了.有兴趣的同学可以继续了解http://www.w3