Webview中的顶部和底部文本剪切

时间:2017-01-23 09:52:36

标签: android text webview height paging

我正在编写电子书阅读器。 对于分页,我有一个聪明(愚蠢?)的想法来使用自定义WebView。我改变了它的行为,当我向左滑动时它向下滚动了1页。它在一点上的效果非常好......顶部和底部文本经常被切断。我得到了我的WebView的高度和我的字体大小,但后来我不知道该怎么做...有没有办法在我的WebView的顶部和底部没有剪切文本?看起来他们使用了相同的技术,但在opensource PageTurner app中使用了ScrollView,但代码非常复杂,我无法获得可能对我感兴趣的代码部分。

Problem with top and bottom lines

public class BookWebView extends WebView {
    private final String LOG_TAG = BookView.class.getSimpleName();
    private final int SWIPE_LIMIT = 80;
    private float startX,diffX;

    public BookWebView(Context context) {
        super(context);
    }

    public BookWebView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public BookWebView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    @Override
    public boolean onTouchEvent(MotionEvent event)
    {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                startX = event.getX();
                break;
            case MotionEvent.ACTION_UP:

                if(Math.abs(diffX) > SWIPE_LIMIT) {
                    int visibleTextHeight = this.getHeight();
                    if(diffX < 0) {
                        Log.d(LOG_TAG,"SCROLLING DOWN");
                        this.scrollTo(0, getScrollY() + visibleTextHeight);
                    } else {
                        Log.d(LOG_TAG,"SCROLLING UP");
                        this.scrollTo(0, getScrollY() - visibleTextHeight);
                    }

                } else {
                    Log.d(LOG_TAG, "==== NO SCROLL ====");
                    return false;
                }
                break;
            case MotionEvent.ACTION_MOVE:
                float endX = event.getX();
                diffX = endX - startX;
                break;
            case MotionEvent.ACTION_POINTER_UP:

                break;
        }

        return true;
    }
}

MainActivity

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        BookWebView bwvText = (BookWebView)findViewById(R.id.bwv_text);
        String text = "<html><body style=\"text-align:justify\"> %s </body></Html>";

        bwvText.loadData(String.format(text,getString(R.string.lorem_ipsum)),"text/html; charset=utf-8", "utf-8");

        WebSettings webSettings = bwvText.getSettings();
        int fontSize = webSettings.getDefaultFontSize();

        bwvText.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    bwvText.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                } else {
                    bwvText.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                }
                int webViewHeight = bwvText.getMeasuredHeight();
                adjustHeight(webViewHeight,fontSize);
            }
        });
    }


    private void adjustHeight(int webViewHeight, int fontSize) {
        // Is there something I can do here ?
    }

activity_main.xml中

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.android.severalpagestext.MainActivity">

    <com.example.android.severalpagestext.BookWebView
        android:id="@+id/bwv_text"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</FrameLayout>

0 个答案:

没有答案