From 3876afc44d9f5eef35bed86b7c6c44d4beb23ce8 Mon Sep 17 00:00:00 2001 From: dgis Date: Fri, 14 Jun 2019 19:55:40 +0200 Subject: [PATCH] Improve Printer UI --- app/build.gradle | 1 + .../forty/eight/PrinterSimulator.java | 5 +- .../forty/eight/PrinterSimulatorFragment.java | 214 ++++++++++++------ .../res/layout/fragment_printer_simulator.xml | 74 ++++-- 4 files changed, 201 insertions(+), 93 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 3b9a41b..c2f45f5 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -84,4 +84,5 @@ dependencies { testImplementation 'junit:junit:4.12' androidTestImplementation 'androidx.test:runner:1.2.0-alpha04' androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0-alpha04' + implementation 'androidx.viewpager:viewpager:1.0.0' } diff --git a/app/src/main/java/org/emulator/forty/eight/PrinterSimulator.java b/app/src/main/java/org/emulator/forty/eight/PrinterSimulator.java index d304843..be82fc8 100644 --- a/app/src/main/java/org/emulator/forty/eight/PrinterSimulator.java +++ b/app/src/main/java/org/emulator/forty/eight/PrinterSimulator.java @@ -230,7 +230,7 @@ public class PrinterSimulator { } while (false); } - String getText() { + public String getText() { return m_Text.toString(); } @@ -238,6 +238,9 @@ public class PrinterSimulator { private final Bitmap mBitmap; + public Bitmap getImage() { + return mBitmap; + } private final int MAXPRTLINES = 32768; // maximum printable lines (out of paper) // private final int SCROLL_HEIGHT = 1; // no. of pixel rows to scroll diff --git a/app/src/main/java/org/emulator/forty/eight/PrinterSimulatorFragment.java b/app/src/main/java/org/emulator/forty/eight/PrinterSimulatorFragment.java index 18391ad..38ba011 100644 --- a/app/src/main/java/org/emulator/forty/eight/PrinterSimulatorFragment.java +++ b/app/src/main/java/org/emulator/forty/eight/PrinterSimulatorFragment.java @@ -1,111 +1,187 @@ package org.emulator.forty.eight; +import android.annotation.SuppressLint; import android.content.Context; -import android.net.Uri; +import android.content.res.ColorStateList; import android.os.Bundle; - -import androidx.fragment.app.DialogFragment; -import androidx.fragment.app.Fragment; - +import android.text.Layout; +import android.text.method.ScrollingMovementMethod; +import android.view.GestureDetector; import android.view.LayoutInflater; +import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; +import android.widget.ImageView; +import android.widget.LinearLayout; +import android.widget.Scroller; +import android.widget.TextView; + +import androidx.annotation.NonNull; +import androidx.fragment.app.DialogFragment; +import androidx.fragment.app.Fragment; +import androidx.viewpager.widget.PagerAdapter; +import androidx.viewpager.widget.ViewPager; -/** - * A simple {@link Fragment} subclass. - */ public class PrinterSimulatorFragment extends DialogFragment { - // TODO: Rename parameter arguments, choose names that match - // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER - private static final String ARG_PARAM1 = "param1"; - private static final String ARG_PARAM2 = "param2"; - // TODO: Rename and change types of parameters - private String mParam1; - private String mParam2; - -// private OnFragmentInteractionListener mListener; private PrinterSimulator printer; public PrinterSimulatorFragment() { - // Required empty public constructor - } - /** - * Use this factory method to create a new instance of - * this fragment using the provided parameters. - * - * @param param1 Parameter 1. - * @param param2 Parameter 2. - * @return A new instance of fragment PrinterSimulatorFragment. - */ - // TODO: Rename and change types and number of parameters - public static PrinterSimulatorFragment newInstance(String param1, String param2) { - PrinterSimulatorFragment fragment = new PrinterSimulatorFragment(); - Bundle args = new Bundle(); - args.putString(ARG_PARAM1, param1); - args.putString(ARG_PARAM2, param2); - fragment.setArguments(args); - return fragment; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - if (getArguments() != null) { - mParam1 = getArguments().getString(ARG_PARAM1); - mParam2 = getArguments().getString(ARG_PARAM2); - } } @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, - Bundle savedInstanceState) { + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment - return inflater.inflate(R.layout.fragment_printer_simulator, container, false); - } + View view = inflater.inflate(R.layout.fragment_printer_simulator, container, false); - // TODO: Rename method, update argument and hook method into UI event - public void onButtonPressed(Uri uri) { -// if (mListener != null) { -// mListener.onFragmentInteraction(uri); -// } + ViewPager pager = view.findViewById(R.id.viewPagerPrinter); + pager.setAdapter(new PagerAdapter() { + + @SuppressLint("ClickableViewAccessibility") + public Object instantiateItem(ViewGroup container, int position) { + switch (position) { + case 0: { + LinearLayout linearLayout = container.findViewById(R.id.page_printer_text); + final TextView textViewPrinterText = container.findViewById(R.id.printer_text); + if(textViewPrinterText != null) { + final Context context = getActivity(); + +// final ColorStateList colors = textViewPrinterText.getTextColors(); +// textViewPrinterText.setTextColor(colors.withAlpha(255)); + + textViewPrinterText.setHorizontallyScrolling(true); + + // http://stackoverflow.com/a/34316896 + final Scroller scroller = new Scroller(context); + textViewPrinterText.setMovementMethod(new ScrollingMovementMethod()); + textViewPrinterText.setScroller(scroller); + //final Layout textViewResultLayout = textViewPrinterText.getLayout(); + textViewPrinterText.setOnTouchListener(new View.OnTouchListener() { + + // Could make this a field member on your activity + GestureDetector gesture = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() { + @Override + public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { + + Layout textViewResultLayout = textViewPrinterText.getLayout(); + if(textViewResultLayout != null) { + int scrollWidth = (int) textViewResultLayout.getLineWidth(0); + int maxX = scrollWidth - textViewPrinterText.getWidth(); + int scrollHeight = textViewResultLayout.getHeight(); + //int scrollHeight = textViewPrinterText.getLineCount() * textViewPrinterText.getLineHeight(); + int maxY = scrollHeight - textViewPrinterText.getHeight(); + scroller.fling( + textViewPrinterText.getScrollX(), textViewPrinterText.getScrollY(), // int startX, int startY + (int) -velocityX, (int) -velocityY, // int velocityX, int velocityY, + 0, maxX, // int minX, int maxX + 0, maxY // int minY, int maxY + ); + } + + return super.onFling(e1, e2, velocityX, velocityY); + } + + }); + + @Override + public boolean onTouch(View v, MotionEvent event) { + gesture.onTouchEvent(event); + return false; + } + + }); + textViewPrinterText.addOnLayoutChangeListener(new View.OnLayoutChangeListener() { + @Override + public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) { + Layout textViewResultLayout = textViewPrinterText.getLayout(); + if(textViewResultLayout != null) { + int scrollWidth = (int) textViewResultLayout.getLineWidth(0); + int maxX = Math.max(0, scrollWidth - textViewPrinterText.getWidth()); + int scrollHeight = textViewResultLayout.getHeight(); + //int scrollHeight = textViewPrinterText.getLineCount() * textViewPrinterText.getLineHeight(); + int maxY = Math.max(0, scrollHeight - textViewPrinterText.getHeight()); + + int scrollX = textViewPrinterText.getScrollX(); + if(scrollX > maxX) + textViewPrinterText.setScrollX(maxX); + int scrollY = textViewPrinterText.getScrollY(); + if(scrollY > maxY) + textViewPrinterText.setScrollY(maxY); + } + } + }); + } + textViewPrinterText.setText(printer.getText()); + //textViewPrinterText.scrollTo(0, -1); + return linearLayout; + } + case 1: { + LinearLayout linearLayout = container.findViewById(R.id.page_printer_graphic); + final ImageView imageViewPrinterGraphic = container.findViewById(R.id.printer_graphic); + if(imageViewPrinterGraphic != null) { + imageViewPrinterGraphic.setImageBitmap(printer.getImage()); + } + return linearLayout; + } + } + return null; + } + + @Override + public CharSequence getPageTitle(int position) { + // Generate title based on item position + switch (position) { + case 0: + return "Text"; + case 1: + return "Graphical"; + default: + return null; + } + } + + @Override + public int getCount() { + return 2; + } + + @Override + public boolean isViewFromObject(@NonNull View view, @NonNull Object object) { + return view == object; + } + }); + + return view; } @Override public void onAttach(Context context) { super.onAttach(context); -// if (context instanceof OnFragmentInteractionListener) { -// mListener = (OnFragmentInteractionListener) context; -// } else { -// throw new RuntimeException(context.toString() -// + " must implement OnFragmentInteractionListener"); -// } } @Override public void onDetach() { super.onDetach(); - //mListener = null; + } + + @Override + public void onResume() { + super.onResume(); + ViewGroup.LayoutParams params = getDialog().getWindow().getAttributes(); + params.width = ViewGroup.LayoutParams.MATCH_PARENT; + params.height = ViewGroup.LayoutParams.MATCH_PARENT; + getDialog().getWindow().setAttributes((android.view.WindowManager.LayoutParams) params); } public void setPrinter(PrinterSimulator printer) { this.printer = printer; } -// /** -// * This interface must be implemented by activities that contain this -// * fragment to allow an interaction in this fragment to be communicated -// * to the activity and potentially other fragments contained in that -// * activity. -// *

-// * See the Android Training lesson Communicating with Other Fragments for more information. -// */ -// public interface OnFragmentInteractionListener { -// // TODO: Update argument type and name -// void onFragmentInteraction(Uri uri); -// } } diff --git a/app/src/main/res/layout/fragment_printer_simulator.xml b/app/src/main/res/layout/fragment_printer_simulator.xml index 5e50f0f..5059137 100644 --- a/app/src/main/res/layout/fragment_printer_simulator.xml +++ b/app/src/main/res/layout/fragment_printer_simulator.xml @@ -2,36 +2,64 @@ - - - - - - - - + tools:context=".PrinterSimulatorFragment"> + app:layout_constraintTop_toTopOf="parent"> + + + + + + + + + + + + + + + + + + + + \ No newline at end of file