Skip to content

Commit 3fa99c1

Browse files
committed
feat: sample for custom controller bar
1 parent aeac314 commit 3fa99c1

File tree

11 files changed

+275
-0
lines changed

11 files changed

+275
-0
lines changed

app/src/main/AndroidManifest.xml

+4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
<category android:name="android.intent.category.LAUNCHER" />
1717
</intent-filter>
1818
</activity>
19+
20+
<activity android:name=".CustomPdfViewActivity">
21+
22+
</activity>
1923
</application>
2024

2125
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package com.zjy.androidpdfhelper;
2+
3+
import static android.view.Gravity.CENTER;
4+
import static android.widget.LinearLayout.HORIZONTAL;
5+
6+
import android.content.Context;
7+
import android.graphics.Color;
8+
import android.graphics.Typeface;
9+
import android.os.Build;
10+
import android.util.AttributeSet;
11+
import android.util.TypedValue;
12+
import android.view.View;
13+
import android.view.ViewGroup;
14+
import android.widget.Button;
15+
import android.widget.LinearLayout;
16+
import android.widget.TextView;
17+
18+
import androidx.annotation.Nullable;
19+
20+
import com.zjy.pdfview.R;
21+
import com.zjy.pdfview.widget.AbsControllerBar;
22+
23+
/**
24+
* Date: 2022/2/10
25+
* Author: Yang
26+
* Describe: 自定义PDF控制栏视图
27+
*/
28+
public class CustomControllerBar extends AbsControllerBar implements View.OnClickListener {
29+
30+
private Button previousBtn, nextBtn;
31+
private TextView pageIndexTv;
32+
33+
public CustomControllerBar(Context context) {
34+
this(context, null);
35+
}
36+
37+
public CustomControllerBar(Context context, @Nullable AttributeSet attrs) {
38+
this(context, attrs, 0);
39+
}
40+
41+
public CustomControllerBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
42+
super(context, attrs, defStyleAttr);
43+
}
44+
45+
@Override
46+
public View getView() {
47+
LinearLayout rootView= new LinearLayout(getContext());
48+
rootView.setOrientation(HORIZONTAL);
49+
rootView.setGravity(CENTER);
50+
setBackgroundColor(Color.WHITE);
51+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
52+
setElevation(dip2px(getContext(), 8));
53+
}
54+
55+
previousBtn = new Button(getContext());
56+
previousBtn.setBackgroundResource(R.drawable.bg_operate_btn);
57+
previousBtn.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
58+
previousBtn.setText("Previous");
59+
rootView.addView(previousBtn, new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, dip2px(getContext(), 36)));
60+
61+
pageIndexTv = new TextView(getContext());
62+
pageIndexTv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
63+
pageIndexTv.setPadding(dip2px(getContext(), 16), 0, dip2px(getContext(), 16), 0);
64+
rootView.addView(pageIndexTv, new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
65+
pageIndexTv.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
66+
pageIndexTv.setText("1/1");
67+
68+
nextBtn = new Button(getContext());
69+
nextBtn.setBackgroundResource(R.drawable.bg_operate_btn);
70+
nextBtn.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
71+
nextBtn.setText("Next");
72+
rootView.addView(nextBtn, new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, dip2px(getContext(), 36)));
73+
74+
previousBtn.setOnClickListener(this);
75+
nextBtn.setOnClickListener(this);
76+
return rootView;
77+
}
78+
79+
@Override
80+
public void onClick(View view) {
81+
if (view == previousBtn) {
82+
clickPrevious();
83+
} else if (view == nextBtn) {
84+
clickNext();
85+
}
86+
}
87+
88+
@Override
89+
public void setPreviousText(String previousText) {
90+
if (previousBtn != null && previousText != null) {
91+
previousBtn.setText(previousText);
92+
}
93+
}
94+
95+
@Override
96+
public void setNextText(String nextText) {
97+
if (nextBtn != null && nextText != null) {
98+
nextBtn.setText(nextText);
99+
}
100+
}
101+
102+
private static int dip2px(Context context, float dpValue) {
103+
final float scale = context.getResources().getDisplayMetrics().density;
104+
return (int) (dpValue * scale + 0.5f);
105+
}
106+
107+
@Override
108+
public void setPageIndexText(String text) {
109+
if (pageIndexTv != null && text != null) {
110+
pageIndexTv.setText(text);
111+
}
112+
}
113+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.zjy.androidpdfhelper;
2+
3+
import android.app.Activity;
4+
import android.content.Context;
5+
import android.content.Intent;
6+
import android.os.Bundle;
7+
import android.view.View;
8+
import android.widget.ImageView;
9+
import com.zjy.pdfview.PdfView;
10+
11+
/**
12+
* Date: 2022/2/10
13+
* Author: Yang
14+
* Describe: 自定义PDF视图
15+
*/
16+
public class CustomPdfViewActivity extends Activity {
17+
18+
private static final String PDF_URL_KEY = "PDF_URL_KEY";
19+
20+
PdfView pdfView;
21+
ImageView backIv;
22+
23+
public static void startPreview(Context context, String url) {
24+
Intent intent = new Intent(context, CustomPdfViewActivity.class);
25+
intent.putExtra(PDF_URL_KEY, url);
26+
context.startActivity(intent);
27+
}
28+
29+
@Override
30+
protected void onCreate(Bundle savedInstanceState) {
31+
super.onCreate(savedInstanceState);
32+
setContentView(R.layout.activity_custom_pdf_view);
33+
34+
backIv = findViewById(com.zjy.pdfview.R.id.back_iv);
35+
backIv.setOnClickListener(new View.OnClickListener() {
36+
@Override
37+
public void onClick(View v) {
38+
finish();
39+
}
40+
});
41+
42+
String url = getIntent().getStringExtra(PDF_URL_KEY);
43+
pdfView = findViewById(com.zjy.pdfview.R.id.pdf_view);
44+
pdfView.loadPdf(url);
45+
CustomControllerBar controllerBar = new CustomControllerBar(this);
46+
pdfView.setPDFController(controllerBar);
47+
}
48+
49+
50+
@Override
51+
protected void onDestroy() {
52+
super.onDestroy();
53+
pdfView.release();
54+
}
55+
}

app/src/main/java/com/zjy/androidpdfhelper/MainActivity.java

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ protected void onCreate(Bundle savedInstanceState) {
1414
super.onCreate(savedInstanceState);
1515
setContentView(R.layout.activity_main);
1616

17+
//CustomPdfViewActivity.startPreview(this, "file:///android_asset/test.pdf");
18+
1719
PdfPreviewUtils.previewPdf(this, "file:///android_asset/test.pdf");
1820
}
1921

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
xmlns:tools="http://schemas.android.com/tools"
5+
android:orientation="vertical"
6+
android:layout_width="match_parent"
7+
android:layout_height="match_parent"
8+
android:fitsSystemWindows="true"
9+
android:clipToPadding="false">
10+
11+
<androidx.constraintlayout.widget.ConstraintLayout
12+
android:layout_width="match_parent"
13+
android:layout_height="60dp"
14+
android:background="#FFFFFF"
15+
android:elevation="4dp"
16+
tools:targetApi="lollipop">
17+
<ImageView
18+
android:id="@+id/back_iv"
19+
android:layout_width="wrap_content"
20+
android:layout_height="wrap_content"
21+
android:paddingStart="16dp"
22+
android:paddingEnd="16dp"
23+
android:paddingTop="8dp"
24+
android:paddingBottom="8dp"
25+
app:layout_constraintStart_toStartOf="parent"
26+
app:layout_constraintTop_toTopOf="parent"
27+
app:layout_constraintBottom_toBottomOf="parent"
28+
android:src="@drawable/pdf_ic_left_arrow"/>
29+
<TextView
30+
android:layout_width="wrap_content"
31+
android:layout_height="wrap_content"
32+
app:layout_constraintStart_toEndOf="@+id/back_iv"
33+
app:layout_constraintTop_toTopOf="parent"
34+
app:layout_constraintBottom_toBottomOf="parent"
35+
android:textSize="18sp"
36+
android:textColor="#000000"
37+
android:text="PDF"/>
38+
</androidx.constraintlayout.widget.ConstraintLayout>
39+
40+
<com.zjy.pdfview.PdfView
41+
android:id="@+id/pdf_view"
42+
android:layout_width="match_parent"
43+
android:layout_height="match_parent"
44+
app:quality="medium">
45+
46+
</com.zjy.pdfview.PdfView>
47+
48+
</LinearLayout>

pdfview/src/main/java/com/zjy/pdfview/PdfRendererActivity.java

+2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ public void onClick(View v) {
4545
String url = getIntent().getStringExtra(PDF_URL_KEY);
4646
pdfView = findViewById(R.id.pdf_view);
4747
pdfView.loadPdf(url);
48+
pdfView.setPreviousText(getResources().getString(R.string.previous));
49+
pdfView.setNextText(getResources().getString(R.string.next));
4850
}
4951

5052

pdfview/src/main/java/com/zjy/pdfview/PdfView.java

+18
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,24 @@ public void clickNext() {
274274
}
275275
}
276276

277+
public void setPreviousText(String previousText) {
278+
if (controllerContainer != null && previousText != null) {
279+
AbsControllerBar controllerBar = (AbsControllerBar) controllerContainer.getChildAt(0);
280+
if (controllerBar != null) {
281+
controllerBar.setPreviousText(previousText);
282+
}
283+
}
284+
}
285+
286+
public void setNextText(String nextText) {
287+
if (controllerContainer != null && nextText != null) {
288+
AbsControllerBar controllerBar = (AbsControllerBar) controllerContainer.getChildAt(0);
289+
if (controllerBar != null) {
290+
controllerBar.setNextText(nextText);
291+
}
292+
}
293+
}
294+
277295
@Override
278296
public void downloadSuccess(String path) {
279297

pdfview/src/main/java/com/zjy/pdfview/widget/AbsControllerBar.java

+9
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@ public AbsControllerBar(@NonNull Context context, @Nullable AttributeSet attrs,
3636

3737
protected abstract View getView();
3838

39+
public void setPreviousText(String previousText) {
40+
41+
}
42+
43+
public void setNextText(String nextText) {
44+
45+
}
46+
47+
3948
private void initView(Context context) {
4049
View view = getView();
4150
if (view == null) {

pdfview/src/main/java/com/zjy/pdfview/widget/PDFControllerBar.java

+14
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,20 @@ public void onClick(View view) {
8484
}
8585
}
8686

87+
@Override
88+
public void setPreviousText(String previousText) {
89+
if (previousBtn != null && previousText != null) {
90+
previousBtn.setText(previousText);
91+
}
92+
}
93+
94+
@Override
95+
public void setNextText(String nextText) {
96+
if (nextBtn != null && nextText != null) {
97+
nextBtn.setText(nextText);
98+
}
99+
}
100+
87101
private static int dip2px(Context context, float dpValue) {
88102
final float scale = context.getResources().getDisplayMetrics().density;
89103
return (int) (dpValue * scale + 0.5f);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
<string name="next">下一页</string>
4+
<string name="previous">上一页</string>
5+
</resources>
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
<string name="next">next</string>
4+
<string name="previous">previous</string>
5+
</resources>

0 commit comments

Comments
 (0)