我想用android studio创建一个pdf阅读器。我在资源文件夹中添加了一个pdf文件。我用了seekbar。但代码没有显示任何内容。怎么了?
我分享所有前卫
Mainactivity.java
package enespolat.polat.com.kuranahmethusrev;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.graphics.pdf.PdfRenderer;
import android.os.Environment;
import android.os.ParcelFileDescriptor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import java.io.File;
import uk.co.senab.photoview.PhotoViewAttacher;
public class MainActivity extends AppCompatActivity {
LinearLayout pagerSection;
SeekBar pageSeekbar;
ImageView pageView;
TextView tvCounter;
PdfRenderer.Page sayfa;
PhotoViewAttacher pva;
int pageCount=0;
int currPage=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pageSeekbar=(SeekBar)findViewById(R.id.sbPager);
pagerSection=(LinearLayout)findViewById(R.id.llPageCounter);
pageView=(ImageView)findViewById(R.id.iv);
tvCounter=(TextView)findViewById(R.id.tvCounter);
render();
pva = new PhotoViewAttacher(pageView);
pva.setMaximumScale(10.0f);
pageSeekbar.setMax(605);
pageSeekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b)
{
currPage=i;
showCurrentPageIndicator();
}
@Override
public void onStartTrackingTouch(SeekBar seekBar)
{
}
@Override
public void onStopTrackingTouch(SeekBar seekBar)
{
currPage=seekBar.getProgress();
render();
}
});
}
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (pageView != null)
pageView.invalidate();
}
void showCurrentPageIndicator()
{
tvCounter.setText(""+(currPage+1));
pagerSection.setVisibility(View.VISIBLE);
pagerSection.postDelayed(new Runnable() {
@Override
public void run() {
pagerSection.setVisibility(View.GONE);
}
},700);
}
private void render()
{
try {
File dlFolder = new File(Environment.getExternalStorageDirectory(), "Download");
File pdfFile = new File(dlFolder, "kuran.pdf");
PdfRenderer renderer = new PdfRenderer(ParcelFileDescriptor.open(pdfFile,ParcelFileDescriptor.MODE_READ_ONLY));
sayfa = renderer.openPage(currPage);
Bitmap bitmap=Bitmap.createBitmap(sayfa.getWidth(),sayfa.getHeight(), Bitmap.Config.ARGB_8888);
Matrix m=pageView.getImageMatrix();
m.setScale(1.0f, 1.0f);
Rect rect=new Rect(0,0,sayfa.getWidth(),sayfa.getHeight());
sayfa.render(bitmap,rect,m,PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY);
//img.setImageMatrix(m);
pageView.setImageBitmap(bitmap);
pageView.invalidate();
/**/
sayfa.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
布局文件夹
<?xml version="1.0" encoding="utf-8" ?>
- <RelativeLayout 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:background="#FFFFFF" android:layout_height="match_parent">
<ImageView android:id="@+id/iv" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/ly" />
- <LinearLayout android:background="#88333333" android:layout_width="match_parent" android:paddingRight="5dp" android:paddingLeft="5dp" android:id="@+id/ly" android:paddingBottom="12dp" android:paddingTop="12dp" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true">
<SeekBar android:id="@+id/sbPager" android:layout_width="match_parent" android:layout_height="wrap_content" />
</LinearLayout>
- <LinearLayout android:visibility="gone" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="40dp" android:background="#88333333" android:id="@+id/llPageCounter" android:layout_centerVertical="true" android:layout_centerHorizontal="true">
<TextView android:layout_width="match_parent" android:layout_height="match_parent" android:textColor="#FFFFFF" android:textStyle="bold" android:id="@+id/tvCounter" android:text="355" android:textSize="40sp" android:gravity="center" />
</LinearLayout>
</RelativeLayout>
清单
<?xml version="1.0" encoding="utf-8" ?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="enespolat.polat.com.kuranahmethusrev">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
- <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme">
- <activity android:name=".MainActivity">
- <intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>