11

PdfRenderer を使用しようとしていますが、ズームとスクロールを使用できるようにする必要がありますが、Android PdfRenderer ではズームとスクロールのサポートは提供されず、ページ ナビゲーションのサポートのみが利用可能です。

しかし、PdfRendererはビットマップを使用してimageviewを使用してコンテンツを表示するため、ズームとスクロールのサポートを実装できると思います。

Google PdfRenderer サンプルでズームとスクロールのサポートを実装するには?

PS: Google が提供するこの PdfRenderer サンプルを使用しています https://github.com/googlesamples/android-PdfRendererBasic

4

4 に答える 4

5

この状況に直面したときに私が使用した解決策は次のとおりです。

  • ImageView に pdfRenderer ページをロードする

  • 私のImageViewをScrollView(タダムスクロール管理)に入れ、このScrollViewをFrameLayoutに入れます

  • 2 つのボタン (スクロール ビューの外側) を追加して、ズームインとズームアウトを管理します (各ボタンは、ImageView でスケール アニメーションをトリガーします)。ジェスチャー検出器で管理することもできますが、そうするとスクロール動作に苦労しました

  • ページの変更を管理するための 2 つのボタンを追加します (まだ ScrollView の外にあります)。

  • 素敵な効果のために、ボタンに FadeIn/FadeOut アニメーションを追加し、OnTouchEvents で FadeIn トリガー (アニメーションが再生されていない場合)、および FadeIn アニメーションが終了したときに FadeOut トリガーを追加しました。

お役に立てば幸いです。より詳細な情報が必要な場合は申し訳ありませんが、今すぐどこから始めればよいかを知っておく必要があります

ここにコードサンプルがあります(ページナビゲーションなどは含まれませんが、ズーム動作とスクロールのみです。残りはリンクしたGoogleコードサンプルにあります)コード:C#(ただし、Javaに変換するのは非常に簡単です)

private Button _zoomInButton;
private Button _zoomOutButton;
private ImageView _pdfViewContainer;
private float _currentZoomLevel;
private float _zoomFactor;
private float _maxZoomLevel;
private float _minZoomLevel;

private void Init(View view) // the content of this method must go in your OnViewCreated method, here the view being the frameLayout you will find in xml
{
     _zoomInButton = view.FindViewById<Button>(Resource.Id.PdfZoomInButton);
     _zoomOutButton = view.FindViewById<Button>(Resource.Id.PdfZoomOutButton);
     _pdfViewContainer = view.FindViewById<ImageView>(Resource.Id.PdfViewContainer);

    _zoomInButton.Click += delegate { ZoomIn(); }; //for you (in Java) this must looks like setOnClickListener(this); and in the onClick metghod you just have to add a case for R.id.PdfZoomInButton containing a call to ZoomIn();
    _zoomOutButton.Click += delegate { ZoomOut(); };

    _minZoomLevel = 0.9f;
    _maxZoomLevel = 1.2f;
    _zoomFactor = 0.1f;
}

private void ZoomIn()
{
    if (_currentZoomLevel + _zoomFactor < _maxZoomLevel)
    {
        ScaleAnimation scale = new ScaleAnimation(_currentZoomLevel, _currentZoomLevel + _zoomFactor, _currentZoomLevel, _currentZoomLevel + _zoomFactor, Dimension.RelativeToSelf, 0.5f, Dimension.RelativeToSelf, 0.5f);
        scale.Duration = 50;
        scale.FillAfter = true;
        _pdfViewContainer.StartAnimation(scale);
        _currentZoomLevel += _zoomFactor;
    }
}

private void ZoomOut()
{
    if (_currentZoomLevel - _zoomFactor > _minZoomLevel)
    {
        ScaleAnimation scale = new ScaleAnimation(_currentZoomLevel, _currentZoomLevel - _zoomFactor, _currentZoomLevel, _currentZoomLevel - _zoomFactor, Dimension.RelativeToSelf, 0.5f, Dimension.RelativeToSelf, 0.5f);
        scale.Duration = 50;
        scale.FillAfter = true;
        _pdfViewContainer.StartAnimation(scale);
        _currentZoomLevel -= _zoomFactor;
    }
}

XML

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/PdfContainer">
    <ScrollView xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbarAlwaysDrawVerticalTrack="true"
        android:id="@+id/PdfScrollView">
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="fitCenter"
            android:adjustViewBounds="true"
            android:scrollbars="vertical"
            android:src="@drawable/mediaIconPDF"
            android:id="@+id/PdfViewContainer" />
    </ScrollView>
    <LinearLayout
        android:id="@+id/PdfRightLayout"
        android:layout_gravity="right"
        android:orientation="vertical"
        android:gravity="center"
        android:layout_width="50dp"
        android:layout_height="match_parent"
        android:weightSum="1">
        <Button
            android:id="@+id/PdfZoomInButton"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:text="+" />
        <space
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.2" />
        <Button
            android:id="@+id/PdfZoomOutButton"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:text="-" />
    </LinearLayout>
    <LinearLayout
        android:id="@+id/PdfBottomLayout"
        android:layout_gravity="bottom"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@color/vogofTransparentGrey"
        android:weightSum="1">
        <Button
            android:id="@+id/PdfPreviousPage"
            android:layout_width="0dp"
            android:layout_weight="0.15"
            android:layout_height="match_parent"
            android:text="Prev" />
        <TextView
            android:id="@+id/PdfCurrentPageLabel"
            android:layout_width="0dp"
            android:layout_weight="0.7"
            android:gravity="center"
            android:layout_height="match_parent"
             />
        <Button
            android:id="@+id/PdfNextPage"
            android:layout_width="0dp"
            android:layout_weight="0.15"
            android:layout_height="match_parent"
            android:text="Next" />
    </LinearLayout>
</FrameLayout>

これで、それを理解するための時間と少しの努力で、望ましい結果を得ることができるはずです. 良い1日を

于 2016-06-28T07:19:36.010 に答える