|
| 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 | +} |
0 commit comments