如何在TEXTVIEW上的特定缩放位置平稳地进行放大和缩放?

问题描述 投票:1回答:3

我即将在包含文本的TextView上执行Zoom IN和OUT操作。我能够放大和缩小,但我需要使缩放功能更加平滑(比如在Chrome浏览器中缩放页面)。在执行放大和缩小操作(使用缩放缩放方法)时,我以中心对齐的方式缩放文本内容,我想缩放我放大的内容。我附加了由我完成的代码,请为我提出一个解决方案。

这是我的活动文件:

 public class ZoomTextView 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 ZoomTextView(Context context) {
         super(context);
         initialize();
     }

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

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

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

     }

     /***
      * @param zoomLimit
      * Default value is 3, 3 means text can zoom 3 times the default size
      */

     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;
     }

     /*Scale Gesture listener class,
     mScaleFactor is getting the scaling value
     and mScaleFactor is mapped between 1.0 and and zoomLimit
     that is 3.0 by default. You can also change it. 3.0 means text
     can zoom to 3 times the default value.*/

     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;
         }
     } }

这是我的.xml文件:

 <noman.zoomtextview.ZoomTextView
     android:layout_width="match_parent"
     android:layout_height="match_parent"

     android:layout_alignParentTop="true"
     android:layout_alignParentLeft="true"
     android:text="@string/sample_string"/> </RelativeLayout>

提前致谢。

java android textview zooming pinchzoom
3个回答
0
投票

我曾为一个可以有多个孩子的视图进行放大/缩小(包括任何类型的视图和文本视图)。

My Project

我使用ScaleX和ScaleY api来缩放父视图。

private fun applyScaleAndTranslationToChild() {
        child().scaleX = scaleFactor
        child().scaleY = scaleFactor
        child().pivotX = 0f  // default is to pivot at view center
        child().pivotY = 0f  // default is to pivot at view center
        child().translationX = translateX
        child().translationY = translateY
    }

上述方法用于应用放大和缩小,缩放的焦点也是固定的。

我使用ScaleGestureListener来获得比例(由此捕获捏手势)

inner class ScaleListner : ScaleGestureDetector.SimpleOnScaleGestureListener() {

        override fun onScale(scaleDetector: ScaleGestureDetector?): Boolean {
            val scaleFactor = scaleDetector!!.scaleFactor
            setScaleAndTranslation(scaleFactor, scaleDetector.focusX, scaleDetector.focusY)
            return true
        }

        override fun onScaleEnd(detector: ScaleGestureDetector?) {
            super.onScaleEnd(detector)
            setLayoutParamOfChild()
        }

        private fun setScaleAndTranslation(scaleFactor: Float, focusX: Float, focusY: Float) {
            if (lastScaleFactor == 0f || Math.signum(scaleFactor) == Math.signum(lastScaleFactor)) {
                val prevScale = [email protected]
                [email protected] *= scaleFactor
                [email protected] = Math.max(MIN_ZOOM, Math.min([email protected], MAX_ZOOM))
                lastScaleFactor = scaleFactor
                val adjustedScaleFactor = [email protected] / prevScale
                // added logic to adjust translateX and translateY for pinch/zoom pivot point
                translateX += (translateX - focusX) * (adjustedScaleFactor - 1)
                translateY += (translateY - focusY) * (adjustedScaleFactor - 1)
            } else {
                lastScaleFactor = 0f
            }
            isSingleTapConfirmed = false
        }
    }

请参阅我的问题所有详细信息都在这里。

Android: Zooming EditText Android Issue in translation And Getting Touch event of childView when Placed outside parentView


2
投票

我想你应该用放大镜这样做

放大镜可以在任意视图上以编程方式使用,如下所示:

View view = findViewById(R.id.view);
Magnifier magnifier = new Magnifier(view);
magnifier.show(view.getWidth() / 2, view.getHeight() / 2);

此处可以使用TextView或任何特定视图替换View


0
投票

如果您不介意添加其他库,请尝试手势视图布局

更具体地说,https://static.javadoc.io/com.alexvasilkov/gesture-views/2.5.2/com/alexvasilkov/gestures/views/GestureFrameLayout.html

基本上你将文本视图包装在XML中的GestureFrameLayout中。它会根据手指的位置和手势自动检测变焦,捏合和平移。无需开箱即用的额外编码(除非您想实现非默认功能,如旋转,裁剪等)

用法示例在这里https://github.com/alexvasilkov/GestureViews/wiki/Usage

<com.alexvasilkov.gestures.views.GestureFrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<!-- GestureFrameLayout can contain only one child -->

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- Layout content goes here -->

</FrameLayout>

© www.soinside.com 2019 - 2024. All rights reserved.