如何平滑滚动和捏缩放垂直和水平方向

时间:2017-08-04 06:45:37

标签: android scrollview zooming pinchzoom

我已经实现了缩放以缩放我的textview。没有滚动它工作正常。但我想要缩放和滚动,因为我有很长的内容。当我滚动时,缩放和滚动工作但不顺畅加上我只能在水平方向上移动我的手指。如果我将手指上下颠倒,那么它会滚动。

ZoomView.java

public class ZoomView extends TextView {
private static final String TAG = "ZoomTextView";
private ScaleGestureDetector mScaleDetector;

private float mScaleFactor = 1.f;
private float defaultSize;

private float zoomLimit = 3.0f;


public ZoomView(Context context) {
    super(context);
    initialize();
}

public ZoomView(Context context, AttributeSet attrs) {
    super(context, attrs);
    initialize();
}

public ZoomView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    initialize();
}

private void initialize() {
    defaultSize = getTextSize();
    mScaleDetector = new ScaleGestureDetector(getContext(), new ScaleListener());

}

public void setZoomLimit(float zoomLimit) {
    this.zoomLimit = zoomLimit;
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

@Override
public boolean onTouchEvent(@NonNull MotionEvent ev) {
    super.onTouchEvent(ev);
    mScaleDetector.onTouchEvent(ev);
    return true;
}

private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
    @Override
    public boolean onScale(ScaleGestureDetector detector) {
        mScaleFactor *= detector.getScaleFactor();
        mScaleFactor = Math.max(1.0f, Math.min(mScaleFactor, zoomLimit));
        setTextSize(TypedValue.COMPLEX_UNIT_PX, defaultSize * mScaleFactor);
        Log.e(TAG, String.valueOf(mScaleFactor));
        return true;
    }
}
}

Vithale.java

public class vithale extends AppCompatActivity {

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

activity_vithale.xml

<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="wrap_content">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/sc">
<com.example.acer.myapplication.ZoomView
    android:id="@+id/iop"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="@string/twentyfive"/>
</ScrollView>
</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

通过将这些行添加到vithale.java修复了缩放和滚动问题,但唯一的问题是滚动不顺畅

ZoomView iop =(ZoomView)findViewById(R.id.iop);         iop.setMovementMethod(new ScrollingMovementMethod());