【FullscreenFragment.java】
public class FullscreenFragment extends Fragment {
private final int DISPLAY_WIDTH = 0;
private final int DISPLAY_HEIGHT = 1;
private final ArrayList<MyNumbersInString> mFileNameList = new ArrayList<>();
private AssetManager mAssetManager;
private int targetIndexNo;
private boolean viewFlag, realSizeFlag, pageModeFlag, openPageFlag;
private ImageView imageView1, imageView2;
private LinearLayout mL_Layout1, mL_Layout2;
private ViewSwitcher mViewSwitcher;
public GestureDetectorCompat mDetector;
private final MyGestureListener mGestureListener = new MyGestureListener();
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater,
@Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_fullscreen, container, false);
}
@SuppressLint("InflateParams")
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// 中略 //
MyScrollView mMSV1 = (MyScrollView) getLayoutInflater().inflate(R.layout.image1, null);
MyScrollView mMSV2 = (MyScrollView) getLayoutInflater().inflate(R.layout.image2, null);
// fragment で記述するにあたり、getActivity().を付ける作法。nullチェックが必要
mViewSwitcher = (ViewSwitcher) getActivity().findViewById( R.id.viewSwither );
mViewSwitcher.addView(mMSV1, 0 );
mViewSwitcher.addView(mMSV2, 1 );
mViewSwitcher.setInAnimation( AnimationUtils.loadAnimation( this.getContext(), android.R.anim.fade_in));
mViewSwitcher.setOutAnimation( AnimationUtils.loadAnimation( this.getContext(), android.R.anim.fade_out));
pageModeFlag = true; // true(1page) false(2page)
openPageFlag = true; // true(右開き) false(左開き)
realSizeFlag = true; // true(real mode) false(fit mode)
viewFlag = true; // true(sw0) false(sw1)
targetIndexNo = 0;
mAssetManager = getResources().getAssets();
setFileNameList();
imageView1 = getActivity().findViewById(R.id.image_view1); // sw0
imageView2 = getActivity().findViewById(R.id.image_view2); // sw1
setImage( imageView1, "image/" + mFileNameList.get(targetIndexNo).getName() );
setImage( imageView2, "image/" + mFileNameList.get(targetIndexNo).getName() );
mDetector = new GestureDetectorCompat(this.getContext(), mGestureListener);
mDetector.setOnDoubleTapListener( mGestureListener );
/* タッチイベントを受け取ることができないので苦肉の策で横スクロールにidを設定して
情報をタッチイベントで吸い出している。ビュースイッチャーで横スクロールを二か所
使っているから二か所分吸い出す
*/
View mView = view.findViewById(R.id.h_scr1);
mView.setOnTouchListener(new View.OnTouchListener() {
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouch(View v, MotionEvent event) {
return mDetector.onTouchEvent( event );
}
});
mView = view.findViewById(R.id.h_scr2);
mView.setOnTouchListener(new View.OnTouchListener() {
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouch(View v, MotionEvent event) {
return mDetector.onTouchEvent( event );
}
});
}
@Override
public void onResume() {
super.onResume();
if (getActivity() != null && getActivity().getWindow() != null) {
getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
}
hide();
}
@Override
public void onPause() {
super.onPause();
if (getActivity() != null && getActivity().getWindow() != null) {
getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
getActivity().getWindow().getDecorView().setSystemUiVisibility(0);
}
show();
}
@Override
public void onDestroy() {
super.onDestroy();
mAssetManager.close();
mFileNameList.clear();
imageView1= null;
imageView2 = null;
mViewSwitcher = null;
mL_Layout1 = null;
mL_Layout2 = null;
mDetector = null;
}
private void hide() {
// Hide UI first
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.hide();
}
// フルスクリーン表示設定 省略
}
@SuppressLint("InlinedApi")
private void show() {
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.show();
}
}
@Nullable
private ActionBar getSupportActionBar() {
ActionBar actionBar = null;
if (getActivity() instanceof AppCompatActivity) {
AppCompatActivity activity = (AppCompatActivity) getActivity();
actionBar = activity.getSupportActionBar();
}
return actionBar;
}
private float movementPlus = 0, movementMinus = 0;
private boolean doubleTapFlag = false;
class MyGestureListener extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
float mWidth = getScreenSize(DISPLAY_WIDTH);
if( !doubleTapFlag && mWidth > 0 ) {
if ( (int) e.getRawX() > ( mWidth / 2 ) ) {
if ( ++targetIndexNo >= mFileNameList.size() ) {
targetIndexNo = 0;
}
if ( viewFlag ) { // image1が表示されているので image2にセット
setImage( imageView2, "image/" + mFileNameList.get(targetIndexNo).getName() );
viewFlag = false;
} else {
setImage( imageView1, "image/" + mFileNameList.get(targetIndexNo).getName() );
viewFlag = true;
}
mViewSwitcher.showNext();
} else {
if ( --targetIndexNo < 0 ) {
targetIndexNo = mFileNameList.size() - 1;
}
if ( viewFlag ) { // image1が表示されているので image2にセット
setImage( imageView2, "image/" + mFileNameList.get(targetIndexNo).getName() );
viewFlag = false;
} else {
setImage( imageView1, "image/" + mFileNameList.get(targetIndexNo).getName() );
viewFlag = true;
}
mViewSwitcher.showPrevious();
}
}
doubleTapFlag = false;
return false;
}
@Override
public boolean onDoubleTap(MotionEvent e) {
doubleTapFlag = true;
return false;
}
@Override
public boolean onDown(MotionEvent e) {
movementPlus = 0;
movementMinus = 0;
return false;
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
doubleTapFlag = false;
return false;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
if ( distanceX > 0 ) movementPlus += distanceX;
if ( distanceX < 0 ) movementMinus += distanceX;
float x = (float) getScreenSize(DISPLAY_WIDTH);
if ( x < 0 ) return false;
if ( movementPlus > ( x -100f ) && movementMinus < -( x - 100f ) ) {
realSizeFlag = !realSizeFlag;
if ( viewFlag ) {
setImage( imageView1, "image/" + mFileNameList.get(targetIndexNo).getName() );
} else {
setImage( imageView2, "image/" + mFileNameList.get(targetIndexNo).getName() );
}
movementPlus = 0;
movementMinus = 0;
return true;
}
return false;
}
};
private int getScreenSize( int mode ) {
int ret = -1;
if ( getActivity() != null ) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {// OSにより求め方が違う
WindowMetrics mWindowMetrics = getActivity().getWindowManager().getCurrentWindowMetrics();
switch (mode) {
case DISPLAY_WIDTH:
ret = mWindowMetrics.getBounds().width();
break;
case DISPLAY_HEIGHT:
ret = mWindowMetrics.getBounds().height();
break;
}
} else {
DisplayMetrics mDisplay = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics( mDisplay );
switch (mode) {
case DISPLAY_WIDTH:
ret = mDisplay.widthPixels;
break;
case DISPLAY_HEIGHT:
ret = mDisplay.heightPixels;
break;
}
}
}
return ret;
}
private void setFileNameList() {
変更ないので省略
}
private void setImage( ImageView iView, String fName ) {
変更ないので省略
}
private void setPaddingImageView( ImageView iView, int bx, int by ) {
int displayWidth = getScreenSize(DISPLAY_WIDTH);
int displayHeight = getScreenSize(DISPLAY_HEIGHT);
int paddingX = 0, paddingY = 0;
if ( displayWidth > 0 && bx < displayWidth ) {
paddingX = (displayWidth - bx) / 2;
}
if ( displayHeight > 0 && by < displayHeight ) {
paddingY = ( displayHeight - by ) / 2;
}
iView.setPaddingRelative( paddingX, paddingY, paddingX, paddingY );
}
private float setImageViewFit( ImageView iView, int bx, int by ) {
int displayWidth = getScreenSize(DISPLAY_WIDTH);
int displayHeight = getScreenSize(DISPLAY_HEIGHT);
float ret = 1f;
if ( displayWidth < 0 || displayHeight < 0 ) return ret;
float fX = (float) displayWidth / (float) bx;
float fY = (float) displayHeight / (float) by;
if ( ( ( bx > displayWidth ) && ( by > displayHeight ) )
|| ( ( bx < displayWidth ) && ( by < displayHeight ) ) ) {
ret = Math.min(fX, fY);
} else if ( ( bx > displayWidth ) && ( by < displayHeight ) ){
ret = fX;
} else if ( ( bx < displayWidth ) && ( by > displayHeight ) ) {
ret = fY;
}
return ret;
}
public int getOrientation() {
変更ないので省略
}
public int getImageShape( int x, int y ) {
変更ないので省略
}
}