如果我试图在textview中捏一下,则缩放不起作用

时间:2017-07-28 18:32:40

标签: java android scrollview pinchzoom

我正在尝试实现缩放以缩放TextView。如果我将内容宽度和高度保持为包装内容,即缩放到缩放时,缩放和滚动工作正常,当我在TextView外部进行缩放时,但如果在TextView中完成,则无效。

我想在TextView中完成它。即宽度和高度设置为与父级匹配时。

这是我的代码:

活动代码:

public class sukharta extends Activity {

TextView scaleGesture;
RelativeLayout relative;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sukharta);
    scaleGesture = (TextView) findViewById(R.id.article);
    scaleGesture.setText(R.string.twentyfive);
    relative = (RelativeLayout) findViewById(R.id.sv) ;

    // ******Set gesture on your TextView******
    relative.setOnTouchListener(new simpleOnScaleGestureListener(this));

}


public class simpleOnScaleGestureListener implements View.OnTouchListener,
        ScaleGestureDetector.OnScaleGestureListener {

    private ScaleGestureDetector relative;

    public simpleOnScaleGestureListener(Context context) {
        relative = new ScaleGestureDetector(context, this);
    }

    @Override
    public boolean onScale(ScaleGestureDetector detector) {
        // TODO Auto-generated method stub

        relative = detector;

        float size = scaleGesture.getTextSize();
        Log.d("TextSizeStart", String.valueOf(size));

        float factor = detector.getScaleFactor();
        Log.d("Factor", String.valueOf(factor));


        float product = size * factor;
        Log.d("TextSize", String.valueOf(product));
        scaleGesture.setTextSize(TypedValue.COMPLEX_UNIT_PX, product);

        size = scaleGesture.getTextSize();
        Log.d("TextSizeEnd", String.valueOf(size));
        return true;
    }

    @Override
    public boolean onScaleBegin(ScaleGestureDetector detector) {
        return true;
    }

    @Override
    public void onScaleEnd(ScaleGestureDetector detector) {

    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        relative.onTouchEvent(event);
        return true;
    }
}
}

XML代码:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/sv">

            <TextView
                android:id="@+id/article"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
    </RelativeLayout>

</ScrollView>

1 个答案:

答案 0 :(得分:0)

您需要修改手势,现在您的TextView抓住了完整的可用空间。因此,您必须在TextView上设置手势。

public class sukharta extends Activity {

    TextView scaleGesture;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.content_main);
        scaleGesture = (TextView) findViewById(R.id.article);
        scaleGesture.setText("25");
        scaleGesture.setMovementMethod(new ScrollingMovementMethod());

        // ******Set gesture on your TextView******
        scaleGesture.setOnTouchListener(new simpleOnScaleGestureListener(this));

    }


    public class simpleOnScaleGestureListener implements View.OnTouchListener,
            ScaleGestureDetector.OnScaleGestureListener {

        private ScaleGestureDetector gestureScale;

        public simpleOnScaleGestureListener(Context context) {
            gestureScale = new ScaleGestureDetector(context, this);
        }

        @Override
        public boolean onScale(ScaleGestureDetector detector) {
            // TODO Auto-generated method stub

            gestureScale = detector;

            float size = scaleGesture.getTextSize();
            Log.d("TextSizeStart", String.valueOf(size));

            float factor = detector.getScaleFactor();
            Log.d("Factor", String.valueOf(factor));


            float product = size * factor;
            Log.d("TextSize", String.valueOf(product));
            scaleGesture.setTextSize(TypedValue.COMPLEX_UNIT_PX, product);

            size = scaleGesture.getTextSize();
            Log.d("TextSizeEnd", String.valueOf(size));
            return true;
        }

        @Override
        public boolean onScaleBegin(ScaleGestureDetector detector) {
            return true;
        }

        @Override
        public void onScaleEnd(ScaleGestureDetector detector) {

        }

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            gestureScale.onTouchEvent(event);
            return true;
        }
    }

}

注意 - 小建议,从大写字符开始你的班级名称,尽量避免内部班级,并专注于单一责任类设计。