我将要对包含文本的TextView
执行放大和缩小操作。我可以放大和缩小,但是我需要使缩放功能更加平滑(例如在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>
谢谢。
答案 0 :(得分:2)
我认为您应该通过这种方式使用放大镜
可以在任意视图上以编程方式使用放大镜,如下所示:
View view = findViewById(R.id.view);
Magnifier magnifier = new Magnifier(view);
magnifier.show(view.getWidth() / 2, view.getHeight() / 2);
这里的视图可以替换为TextView或任何特定的视图
答案 1 :(得分:0)
如果您不介意添加其他库,请尝试手势视图布局
基本上,您将文本视图包装在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>
答案 2 :(得分:0)
我已经为一个视图进行了放大/缩小,该视图可以有多个子视图(包括任何一种视图和textview)。
我使用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 = 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
}
}
请参阅我的问题,所有详细信息都在这里。