简体   繁体   English

如何在TEXTVIEW上平滑地放大和缩小以及放大特定的缩放位置?

[英]How to perform Zoom IN and OUT Smoothly and Zooming in specific Zoom location on a TEXTVIEW?

I am about to perform Zoom IN and OUT operations on a TextView which contains text. 我将要在包含文本的TextView上执行Zoom IN和OUT操作。 I am able to Zoom IN and OUT, but I need to make Zoom functions much Smoother(like Zooming a page in Chrome Browser). 我可以放大和缩小,但是我需要使缩放功能更加平滑(例如在Chrome浏览器中缩放页面)。 While performing Zoom-In and Zoom-Out operations (Using Pinch Zoom method), I am Zooming the Text content in a center aligned manner, I want to Zoom the content where I am Zooming in. I am attaching the code done by me, kindly suggest a solution for me. 在执行“放大”和“缩小”操作时(使用“缩进缩放”方法),我以居中对齐的方式缩放文本内容,我想在要缩放的位置缩放内容。我要附加我自己的代码,请为我提出解决方案。

Here is my activity file: 这是我的活动文件:

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

Here is my .xml file: 这是我的.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>

Thanks in advance. 提前致谢。

I think you should use magnifier, by this way 我想你应该用放大镜

The magnifier can be programmatically used on an arbitrary view as follows: 可以在任意视图上以编程方式使用放大镜,如下所示:

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

Here View can be replaced with TextView or Any specific view 这里的视图可以替换为TextView或任何特定的视图

If you don't mind adding another library, try the gesture view layout 如果您不介意添加其他库,请尝试手势视图布局

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

basically you wrap your text view inside the GestureFrameLayout in the XML. 基本上,您将文本视图包装在XML的GestureFrameLayout中。 it'll automatically detect zoom, pinch and pan based on your fingers' position and gestures. 它会根据您的手指的位置和手势自动检测缩放,收缩和平移。 no extra coding required out of the box (unless if you want to implement non-default functionality like rotation, crop etc) 无需开箱即用的额外编码(除非您要实现旋转,裁剪等非默认功能)

the usage example is here https://github.com/alexvasilkov/GestureViews/wiki/Usage 使用示例在这里https://github.com/alexvasilkov/GestureViews/wiki/使用

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

I have worked on Zoom In/Out for a View which can have multiple child(any kind of view and textview is included). 我已经为一个视图进行了放大/缩小,该视图可以有多个子视图(包括任何类型的视图和textview)。

My Project 我的项目

I use ScaleX and ScaleY api to zoom the parent view. 我使用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
    }

above method is used to apply zoom in and out, and focus of zooming is also fixed. 以上方法用于应用放大和缩小,并且缩放焦点也固定。

I use ScaleGestureListener to get the scale (pinch gesture is captured by this) 我使用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 = this@ZoomView.scaleFactor
                this@ZoomView.scaleFactor *= scaleFactor
                this@ZoomView.scaleFactor = Math.max(MIN_ZOOM, Math.min(this@ZoomView.scaleFactor, MAX_ZOOM))
                lastScaleFactor = scaleFactor
                val adjustedScaleFactor = this@ZoomView.scaleFactor / 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
        }
    }

Refer to my Question all details are here. 请参阅我的问题,所有详细信息都在这里。

Android: Zooming EditText Android Issue in translation And Getting Touch event of childView when Placed outside parentView Android:缩放EditText Android放置在parentView之外时,会发生childView的翻译和Get Touch事件中的翻译问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM