当文本覆盖整个屏幕时,通过捏缩文本

时间:2018-03-02 20:24:57

标签: android textview zoom pinchzoom

灵感来自this

我已尝试以下代码通过捏缩放文本。它工作正常,除非字符串太长并覆盖屏幕。当文本覆盖整个屏幕时,无法通过捏缩小或放大。

我在scrollView中使用textView,然后在linearLayout中使用。没有scrollView它工作正常,但使用scrollView问题仍然存在。我需要scroolView,因为我的冗长字符串可能看起来像几段。

即使字符串太长而且覆盖整个屏幕,如何放大或缩小文本?

toggleOptions(index, currentDate) {
        // toggle the dropdown with all buttons if its new then showing options is always true
        if (currentDate === this.currentSelectedDate) {
            this.statement.showOptions = !this.statement.showOptions;
        } else {
            this.statement.showOptions = true;
        }

        this.activeRow = index;
        this.currentSelectedDate = currentDate;
    }

xml代码是 -

public class MainActivity extends Activity {

TextView textView3;
ScaleGestureDetector scaleGestureDetector;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    textView3 = (TextView) findViewById(R.id.text_body_story);
    textView3.setText("What we are seeing in Eastern Ghouta and elsewhere in Syria are likely war crimes and potentially crimes against humanity");
    scaleGestureDetector = new ScaleGestureDetector(this, new CustomOnScaleGestureListener());
}


@Override
public boolean onTouchEvent(MotionEvent event) {
    // TODO Auto-generated method stub
    scaleGestureDetector.onTouchEvent(event);
    return true;
}

public class CustomOnScaleGestureListener extends
        ScaleGestureDetector.SimpleOnScaleGestureListener {

    @Override
    public boolean onScale(ScaleGestureDetector detector) {
        // TODO Auto-generated method stub
        float size = textView3.getTextSize();
        float factor = detector.getScaleFactor();
        float product = size * factor;

        if (product >= 160) {
            product = 160;
        } else if (product <= 30) {
            product = 30;
        }

        textView3.setTextSize(TypedValue.COMPLEX_UNIT_PX, product);


        return true;
    }
}
}

1 个答案:

答案 0 :(得分:0)

由于我不能发表评论,我在这里发表评论......

也许考虑将TextView限制为单行?

android:maxLines="1"