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

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

灵感来自this

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

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

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

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

xml代码是 -

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.pinchzoom.MainActivity">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:id="@+id/text_story_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp"
                android:layout_marginTop="10dp"
                android:fontFamily="sans-serif-condensed"
                android:gravity="center_horizontal"
                android:text="text_story_name"
                android:textAllCaps="true"
                android:textSize="16sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/text_author_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="text_author_name"
                android:textAlignment="center"
                android:textSize="14sp"
                android:textStyle="bold|italic" />

            <Space
                android:layout_width="match_parent"
                android:layout_height="5dp"
                android:background="#000000"

                />

            <ImageView
                android:id="@+id/imageView2"
                android:layout_width="match_parent"
                android:layout_height="5dp"
                android:background="#000000"
                app:srcCompat="?attr/actionModeSplitBackground" />

            <TextView
                android:id="@+id/text_body_story"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp"
                android:padding="10dp"
                android:text="text_body_story"
                android:textColorHighlight="#ffee04"

                android:textSize="14sp" />

        </LinearLayout>


    </ScrollView>


</RelativeLayout>
android textview zoom pinchzoom
1个回答
0
投票

我不能发表评论,生病了我的评论。

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

android:maxLines="1"
© www.soinside.com 2019 - 2024. All rights reserved.