gpt4 book ai didi

android - ImageView - 缩放拉伸(stretch)或下拉

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:22:41 24 4
gpt4 key购买 nike

如何实现这个功能,PullToZoom仅使用 ImageView(示例中没有 ListView)?

类似question , 但没有答案:(

最佳答案

使用 Gesture Detector 怎么样? ?您可以在以下代码中找到并实现(我认为)您需要的内容:

public class MainActivity extends AppCompatActivity {

private static final String DEBUG_TAG = "Gestures";

private GestureDetectorCompat mDetector;
private ImageView imageView;
private MyGestureListener myGestureListener;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

imageView = (ImageView) findViewById(R.id.image);

myGestureListener = new MyGestureListener();
mDetector = new GestureDetectorCompat(this, myGestureListener);
imageView.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
int action = MotionEventCompat.getActionMasked(event);
if (action == MotionEvent.ACTION_UP) {
myGestureListener.upDetected();
}
return mDetector.onTouchEvent(event);
}
});

imageView.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.test));

}

class MyGestureListener extends GestureDetector.SimpleOnGestureListener {

private static final float MAX_ZOOM = 0.5f;

private static final float PCT = 300f;
private float delta;

private ValueAnimator valueAnimator;

@Override
public boolean onDown(MotionEvent event) {
return true;
}

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
if (valueAnimator != null) {
valueAnimator.cancel();
}

delta += distanceY;
float pct = getPct(delta);
imageView.setScaleX(1.0f + pct);
imageView.setScaleY(1.0f + pct);
return false;
}

void upDetected() {

float pct = getPct(delta);

valueAnimator = new ValueAnimator();
valueAnimator.setFloatValues(pct, 0.0f);
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
imageView.setScaleX(1.0f + (float) animation.getAnimatedValue());
imageView.setScaleY(1.0f + (float) animation.getAnimatedValue());
}
});
valueAnimator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {

}

@Override
public void onAnimationEnd(Animator animation) {
delta = 0f;
imageView.setScaleX(1.0f);
imageView.setScaleY(1.0f);
}

@Override
public void onAnimationCancel(Animator animation) {

}

@Override
public void onAnimationRepeat(Animator animation) {

}
});
valueAnimator.start();
}

private float getPct(float delta) {
float pct = delta / PCT;
if (pct >= MAX_ZOOM) {
pct = MAX_ZOOM;
}
else if (pct <= -MAX_ZOOM) {
pct = -MAX_ZOOM;
}
return pct;
}
}
}

MainActivity 有一个简单的 ImageView。当您在其中“滚动”时,会检测到手势并缩放图像(向上或向下)。当您将手指从屏幕上移开时,图像会通过一个简单的动画缩小到原来的大小。如果你想避免缩小,你只需要在 getPct() 方法上工作。

编辑

例如 getPtc 可能是这样的(如果您只对放大感兴趣)

private float getPct(float delta) {
float pct = -delta / PCT;
if (pct >= MAX_ZOOM) {
pct = MAX_ZOOM;
}
else if (pct <= 0) {
pct = 0;
}
return pct;
}

编辑 #2

手势检测器的第一个滚动事件似乎很宽。我添加了一些代码来忽略它

    class MyGestureListener extends GestureDetector.SimpleOnGestureListener {

private static final float MAX_ZOOM = 0.8f;

private static final float PCT = 300f;
private float delta;

private ValueAnimator valueAnimator;
private boolean mFirstEvent = true;

@Override
public boolean onDown(MotionEvent event) {
return true;
}

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
if (valueAnimator != null) {
valueAnimator.cancel();
}

if (mFirstEvent) {
mFirstEvent = false;
return false;
}

delta += distanceY;
float pct = getPct(delta);
imageView.setScaleX(1.0f + pct);
imageView.setScaleY(1.0f + pct);
textView.setScaleY(1.0f - pct);
return false;
}

void upDetected() {

mFirstEvent = true;
float pct = getPct(delta);

valueAnimator = new ValueAnimator();
valueAnimator.setFloatValues(pct, 0.0f);
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
imageView.setScaleX(1.0f + (Float) animation.getAnimatedValue());
imageView.setScaleY(1.0f + (Float) animation.getAnimatedValue());
textView.setScaleY(1.0f - (Float) animation.getAnimatedValue());
}
});
valueAnimator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {

}

@Override
public void onAnimationEnd(Animator animation) {
delta = 0f;
imageView.setScaleX(1.0f);
imageView.setScaleY(1.0f);
textView.setScaleY(1.0f);
}

@Override
public void onAnimationCancel(Animator animation) {

}

@Override
public void onAnimationRepeat(Animator animation) {

}
});
valueAnimator.start();
}

private float getPct(float delta) {
float pct = -delta / PCT;
if (pct >= MAX_ZOOM) {
pct = MAX_ZOOM;
}
else if (pct <= 0) {
pct = 0;
}
return pct;
}

/*private float getPct(float delta) {
float pct = delta / PCT;
if (pct >= MAX_ZOOM) {
pct = MAX_ZOOM;
}
else if (pct <= -MAX_ZOOM) {
pct = -MAX_ZOOM;
}
return pct;
}*/
}

关于android - ImageView - 缩放拉伸(stretch)或下拉,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31761845/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com