如图下拉时顶部拉伸效果 代码如下:
创建:DampView
public class DampView extends ScrollView {
private static final int LEN = 0xc8;
private static final int DURATION = 500;
private static final int MAX_DY = 200;
private Scroller mScroller;
TouchTool tool;
int left, top;
float startX, startY, currentX, currentY;
int imageViewH;
int rootW, rootH;
ImageView imageView;
boolean scrollerType;
public DampView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public DampView(Context context, AttributeSet attrs) {
super(context, attrs);
mScroller = new Scroller(context);
}
public DampView(Context context) {
super(context);
}
public void setImageView(ImageView imageView) {
this.imageView = imageView;
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
int action = event.getAction();
if (!mScroller.isFinished()) {
return super.onTouchEvent(event);
}
currentX = event.getX();
currentY = event.getY();
imageView.getTop();
switch (action) {
case MotionEvent.ACTION_DOWN:
left = imageView.getLeft();
top = imageView.getBottom();
rootW = getWidth();
rootH = getHeight();
imageViewH = imageView.getHeight();
startX = currentX;
startY = currentY;
tool = new TouchTool(imageView.getLeft(), imageView.getBottom(),
imageView.getLeft(), imageView.getBottom() + LEN);
break;
case MotionEvent.ACTION_MOVE:
if (imageView.isShown() && imageView.getTop() >= 0) {
if (tool != null) {
int t = tool.getScrollY(currentY - startY);
if (t >= top && t <= imageView.getBottom() + LEN) {
android.view.ViewGroup.LayoutParams params = imageView
.getLayoutParams();
params.height = t;
imageView.setLayoutParams(params);
}
}
scrollerType = false;
}
break;
case MotionEvent.ACTION_UP:
scrollerType = true;
mScroller.startScroll(imageView.getLeft(), imageView.getBottom(),
0 - imageView.getLeft(),
imageViewH - imageView.getBottom(), DURATION);
invalidate();
break;
}
return super.dispatchTouchEvent(event);
}
@Override
public void computeScroll() {
if (mScroller.computeScrollOffset()) {
int x = mScroller.getCurrX();
int y = mScroller.getCurrY();
imageView.layout(0, 0, x + imageView.getWidth(), y);
invalidate();
if (!mScroller.isFinished() && scrollerType && y > MAX_DY) {
android.view.ViewGroup.LayoutParams params = imageView
.getLayoutParams();
params.height = y;
imageView.setLayoutParams(params);
}
}
}
public class TouchTool {
private int startX, startY;
public TouchTool(int startX, int startY, int endX, int endY) {
super();
this.startX = startX;
this.startY = startY;
}
public int getScrollX(float dx) {
int xx = (int) (startX + dx / 2.5F);
return xx;
}
public int getScrollY(float dy) {
int yy = (int) (startY + dy / 2.5F);
return yy;
}
}
}
main.xml 创建
<com.example.dampview.DampView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/dampview"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<!--此处必须设置imageview的scaleType为centerCrop,当然在代码中设置也可以-->
<ImageView
android:id="@+id/img"
android:layout_width="match_parent"
android:layout_height="160dp"
android:scaleType="centerCrop"
android:onClick="onImgClick"
android:src="@drawable/image" />
<ImageView
android:id="@+id/iv_photo"
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_marginTop="-32dp"
android:src="@drawable/ic_launcher"
android:onClick="onPhotoClick"
/>
<ListView
android:id="@+id/iv_lvi"
android:layout_width="match_parent"
android:layout_height="1000dip"
></ListView>
</LinearLayout>
</com.example.dampview.DampView>
创建 mainActivity.java
public class MainActivity extends Activity {
private ImageView img;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupView();
}
public void setupView() {
img = (ImageView) findViewById(R.id.img);
DampView view = (DampView) findViewById(R.id.dampview);
view.setImageView(img);
}
public void onImgClick(View view){
Toast.makeText(this, "单击背景", Toast.LENGTH_SHORT).show();
}
public void onPhotoClick(View view){
Toast.makeText(this, "单击图像", Toast.LENGTH_SHORT).show();
}
}
图片:image.jpg