ViewFlipper自用 左右滑動切屏、上下滑動調整亮度’
阿新 • • 發佈:2018-11-11
1、xml佈局檔案 為ViewFlipper新增多個檢視
<ViewFlipper android:id="@+id/screen_view_flipper" android:layout_width="match_parent" android:layout_height="match_parent"> <RelativeLayout android:id="@+id/view1" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"android:background="@color/light_white"> </RelativeLayout> <RelativeLayout android:id="@+id/view2" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@color/light_red"> </RelativeLayout></ViewFlipper>
2、MainActivity 中觸控事件,上下調整螢幕亮度,左右切換view。
private ViewFlipper mViewFlipper; float downX; float downY; float moveY; mViewFlipper = (ViewFlipper) findViewById(R.id.screen_view_flipper); @Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { caseMotionEvent.ACTION_DOWN: downY = event.getY(); downX = event.getX(); // 獲取當前亮度 screenBrightness = ViewUtil .getScreenBrightness(ScreenLightActivity.this); break; case MotionEvent.ACTION_MOVE: moveY = event.getY(); float disY = downY - moveY; int heightPixels = getResources().getDisplayMetrics().heightPixels / 2; float percentLight = disY / heightPixels; // 亮度偏移量 int offsetLight = (int) (percentLight * 100); Logger.i("MainActivity", offsetLight+""); // 最終亮度 int endLight = screenBrightness + offsetLight; Logger.i("MainActivity", endLight+""); if (endLight >= 0 && endLight <= 255) { ViewUtil.setBrightness(ScreenLightActivity.this, endLight); ViewUtil.saveBrightness(getContentResolver(), endLight); } break; case MotionEvent.ACTION_UP: //向右滑動 if(event.getX()-downX>100){ mViewFlipper.setInAnimation(this,R.anim.left_in); mViewFlipper.setOutAnimation(this,R.anim.right_out); mViewFlipper.showNext(); } if (downX-event.getX()>100){ mViewFlipper.setInAnimation(this,R.anim.right_in); mViewFlipper.setOutAnimation(this,R.anim.left_out); mViewFlipper.showPrevious(); } break; default: break; } return super.onTouchEvent(event); }