当前位置: 移动技术网 > 移动技术>移动开发>Android > 新浪微博第三方登录界面上下拉伸图片之第三方开源PullToZoomListViewEx(二)

新浪微博第三方登录界面上下拉伸图片之第三方开源PullToZoomListViewEx(二)

2019年07月24日  | 移动技术网移动技术  | 我要评论

上篇文章给大家介绍了新浪微博第三方登录界面上下拉伸图片之第三方开源pulltozoomlistviewex(一),需要了解的朋友可以点击了解详情。

这是pullzoomview在scrollview实现,android pullzoomview在scrollview的实现是:pulltozoomscrollviewex

下载地址:https://github.com/frank-zhu/pullzoomview

本文要说的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" >
  <com.ecloud.pulltozoomview.pulltozoomscrollviewex
   android:id="@+id/scroll_view"
   android:layout_width="match_parent"
   android:layout_height="match_parent" />
 </relativelayout> 

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.menu;
 import android.view.menuitem;
 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_test).setonclicklistener(new view.onclicklistener() {
    @override
    public void onclick(view v) {
     log.d("pulltozoomscrollviewex", "onclick");
    }
   });
   scrollview.getpullrootview().findviewbyid(r.id.tv_test).setonclicklistener(new view.onclicklistener() {
    @override
    public void onclick(view v) {
     log.e("pulltozoomscrollviewex", "onclick");
    }
   });
   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 mscreenheight = localdisplaymetrics.heightpixels;
   int mscreenwidth = localdisplaymetrics.widthpixels;
   linearlayout.layoutparams localobject = new linearlayout.layoutparams(mscreenwidth,
     (int) (.f * (mscreenwidth / .f)));
   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="sp"
   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" />
  <linearlayout
   android:id="@+id/ll_action_button"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:background="#"
   android:layout_alignparentbottom="true"
   android:padding="dip">
   <textview
    android:id="@+id/tv_register"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="注册"
    android:layout_weight=""
    android:textsize="sp"
    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=""
    android:textsize="sp"
    android:gravity="center"
    android:layout_gravity="center"
    android:textcolor="#ffffff" />
  </linearlayout>
 </relativelayout> 

head_zoom_view.xml:

 <?xml version="." encoding="utf-"?>
 <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" /> 

head_zoom_view其实就放了一张可供缩放拉伸的图片。

content_view.xml:

 <?xml version="." encoding="utf-"?>
 <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_test"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:gravity="center_vertical"
   android:padding="dp"
   android:text="test"
   android:textsize="sp" />
  <textview
   android:id="@+id/tv_test"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:gravity="center_vertical"
   android:padding="dp"
   android:text="test"
   android:textsize="sp" />
  <textview
   android:id="@+id/tv_test"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:gravity="center_vertical"
   android:padding="dp"
   android:text="test"
   android:textsize="sp" />
  <textview
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:gravity="center_vertical"
   android:padding="dp"
   android:text="test"
   android:textsize="sp" />
  <textview
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:gravity="center_vertical"
   android:padding="dp"
   android:text="test"
   android:textsize="sp" />
  <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="dp"
   android:text="test"
   android:textsize="sp" />
  <textview
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:gravity="center_vertical"
   android:padding="dp"
   android:text="test"
   android:textsize="sp" />
  <textview
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:gravity="center_vertical"
   android:padding="dp"
   android:text="test"
   android:textsize="sp" />
  <textview
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:gravity="center_vertical"
   android:padding="dp"
   android:text="test"
   android:textsize="sp" />
  <textview
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:gravity="center_vertical"
   android:padding="dp"
   android:text="test"
   android:textsize="sp" />
 </linearlayout> 

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网