Improve Printer UI

This commit is contained in:
dgis 2019-06-14 19:55:40 +02:00
parent 2a52d225dc
commit 3876afc44d
4 changed files with 201 additions and 93 deletions

View file

@ -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'
}

View file

@ -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

View file

@ -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) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_printer_simulator, container, false);
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 View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_printer_simulator, container, false);
public int getCount() {
return 2;
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
// if (mListener != null) {
// mListener.onFragmentInteraction(uri);
// }
@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.
// * <p>
// * See the Android Training lesson <a href=
// * "http://developer.android.com/training/basics/fragments/communicating.html"
// * >Communicating with Other Fragments</a> for more information.
// */
// public interface OnFragmentInteractionListener {
// // TODO: Update argument type and name
// void onFragmentInteraction(Uri uri);
// }
}

View file

@ -2,17 +2,24 @@
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".PrinterSimulatorFragment"
android:id="@+id/frameLayout">
tools:context=".PrinterSimulatorFragment">
<androidx.viewpager.widget.ViewPager
android:id="@+id/viewPagerPrinter"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.tabs.TabItem
@ -27,11 +34,32 @@
</com.google.android.material.tabs.TabLayout>
<androidx.viewpager.widget.ViewPager
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tabLayout" />
<LinearLayout
android:id="@+id/page_printer_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/printer_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="horizontal|vertical"
android:textIsSelectable="true" />
</LinearLayout>
<LinearLayout
android:id="@+id/page_printer_graphic"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/printer_graphic"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical" />
</LinearLayout>
</androidx.viewpager.widget.ViewPager>
</androidx.constraintlayout.widget.ConstraintLayout>