我想缩放和翻译自定义列表视图。我已经成功完成了缩放,但是当我在缩放后水平移动时,它超出了屏幕宽度(意味着我看到了白屏),当我在对面水平移动时,我再次看到了视图。
问题是仅在 x 方向上使用 canvas.translate(x,0) 后,它没有固定到视图宽度
这是我使用的代码-
公共类 CustomListView 扩展 ListView{
private ScaleGestureDetector mScaleDetector;
private float mScaleFactor = 1.f;
private float x = 0;
private float y = 0;
private float startx = 0;
private float starty = 0;
private boolean canClick = true;
private float xtranslation = 0;
private float xPos = 0;
public CustomListView(Context context, AttributeSet attrs) {
super(context, attrs);
mScaleDetector = new ScaleGestureDetector(context, new ScaleListener());
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
if (ev.getAction() == MotionEvent.ACTION_DOWN) {
startx = ev.getX();
starty = ev.getY();
}
if (ev.getPointerCount() > 1) {
y = ev.getY();
x = ev.getX();
} else if (ev.getAction() == MotionEvent.ACTION_UP) {
canClick = (ev.getX() - startx) == 0 && (ev.getY() - starty) == 0 && mScaleFactor == 1;
} else if((ev.getAction() == MotionEvent.ACTION_MOVE)){
xtranslation = ev.getX() - startx;
}
mScaleDetector.onTouchEvent(ev);
return super.onTouchEvent(ev);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.scale(mScaleFactor, mScaleFactor, x , y);
System.out.println("trans : "+xtranslation+" XPOS : "+xPos);
if(mScaleFactor > 1){
canvas.translate(xtranslation/2, 0);
}
super.onDraw(canvas);
}
private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener{
@Override
public boolean onScale(ScaleGestureDetector detector) {
canClick = false;
mScaleFactor *= detector.getScaleFactor();
// Don't let the object get too small or too large.
mScaleFactor = Math.max(0.1f, Math.min(mScaleFactor, 2.0f));
if (mScaleFactor <= 1) {
mScaleFactor = 1;
canClick = true;
}
xPos = CustomListView.this.getWidth() - detector.getFocusX();
CustomListView.this.invalidate();
return true;
}
}
public boolean canClick(){
return canClick;
}
}
如果可能,请解决我的问题