return width;
}
/**
- 获得屏幕高度
*/
public static int getScreenHeight(Context context) {
WindowManager wm = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics outMetrics = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(outMetrics);
int height = outMetrics.heightPixels;
return height;
}
需要写一个自定义的ScrollView 来获取滚动的高度
import android.content.Context;
import android.util.AttributeSet; 《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》无偿开源 徽信搜索公众号【编程进阶路】
import android.widget.ScrollView;
//重写ScrollView
public class NotifyingScrollView extends ScrollView {
/**
- @author Cyril Mottier
*/
public interface OnScrollChangedListener {
void onScrollChanged(ScrollView who, int l, int t, int oldl, int oldt);
}
private OnScrollChangedListener mOnScrollChangedListener;
public NotifyingScrollView(Context context) {
super(context);
}
public NotifyingScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public NotifyingScrollView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
/**
- t 为目前滑动的高度
*/
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
if (mOnScrollChangedListener != null) {
mOnScrollChangedListener.onScrollChanged(this, l, t, oldl, oldt);
}
}
public void setOnScrollChangedListener(OnScrollChangedListener listener) {
mOnScrollChangedListener = listener;
}
}
MainActivity代码
package com.yqy.cmd;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import com.yqy.cmd.NotifyingScrollView.OnScrollChangedListener;
public class MainActivity extends Activity {
private ImageView topIv;
private NotifyingScrollView mySv;
private View view;
private LinearLayout myLl;
private int width;
private int height;
private int allowHeight;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
topIv = (ImageView) findViewById(R.id.topIv);
mySv = (NotifyingScrollView) findViewById(R.id.mySv);
view = findViewById(R.id.view);
myLl = (LinearLayout) findViewById(R.id.myLl);
//屏幕的宽高单位为px,所以我们在布局中需要转换为dp
width = ScreenUtils.getScreenWidth(this);
height = ScreenUtils.getScreenHeight(this);
allowHeight = Utils.px2dip(this, height) / 3;
topIv.setLayoutParams(new RelativeLayout.LayoutParams(
LayoutParams.MATCH_PARENT, Utils.px2dip(this, height) / 3));
view.setLayoutParams(new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, Utils.px2dip(this, height) / 3));
mySv.setOnScrollChangedListener(new OnScrollChangedListener() {
@Override
public void onScrollChanged(ScrollView who, int l, int t, int oldl, int oldt) {
if(t < allowHeight){
int height = allowHeight - t;
topIv.setLayoutParams(new RelativeLayout.LayoutParams(height*3, height));
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
activity_main布局
<RelativeLayout xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:tools=“http://schemas.android.com/tools”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:background=“@android:color/holo_red_light”
tools:context=“.MainActivity”
tools:ignore=“NewApi” >
<ImageView
android:id=“@+id/topIv”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:background=“@drawable/ic_launcher” />