下面列出了android.view.View#LAYOUT_DIRECTION_LTR 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void init() {
int backgroundColor = getResources().getColor(R.color.text_color_dark_grey);
int triggerColor = getResources().getColor(R.color.trigger_fire_color);
triggerPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
triggerPaint.setColor(triggerColor);
triggerPaint.setStyle(Paint.Style.FILL);
backgroundPaint = new Paint();
backgroundPaint.setColor(backgroundColor);
backgroundPaint.setStyle(Paint.Style.FILL);
triggerOutline = new RectF();
triggerPath = new Path();
isLtrLayout =
getResources().getConfiguration().getLayoutDirection() == View.LAYOUT_DIRECTION_LTR;
}
private static void setViewLayoutDirection(View view, ViewNodeInfo viewNodeInfo) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
return;
}
final int viewLayoutDirection;
switch (viewNodeInfo.getLayoutDirection()) {
case LTR:
viewLayoutDirection = View.LAYOUT_DIRECTION_LTR;
break;
case RTL:
viewLayoutDirection = View.LAYOUT_DIRECTION_RTL;
break;
default:
viewLayoutDirection = View.LAYOUT_DIRECTION_INHERIT;
}
view.setLayoutDirection(viewLayoutDirection);
}
private Drawable resolveLeftShadow() {
int layoutDirection = getLayoutDirection(this);
// Prefer shadows defined with start/end gravity over left and right.
if (layoutDirection == View.LAYOUT_DIRECTION_LTR) {
if (mShadowStart != null) {
// Correct drawable layout direction, if needed.
mirror(mShadowStart, layoutDirection);
return mShadowStart;
}
} else {
if (mShadowEnd != null) {
// Correct drawable layout direction, if needed.
mirror(mShadowEnd, layoutDirection);
return mShadowEnd;
}
}
return mShadowLeft;
}
private void applyLeftMargin(int margin, View... targetViews) {
for (View view : targetViews) {
final LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams();
boolean isLeftToRight = TextUtils.getLayoutDirectionFromLocale(Locale.getDefault())==View.LAYOUT_DIRECTION_LTR;
if (isLeftToRight)
params.setMargins(margin, params.topMargin, params.rightMargin, params.bottomMargin);
else
params.setMargins(params.leftMargin, params.topMargin, margin, params.bottomMargin);
view.setLayoutParams(params);
}
}
static int getLayoutDirection(Drawable drawable) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
return drawable.getLayoutDirection();
else if (SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
return View.LAYOUT_DIRECTION_LTR;
else
return 0;
}
@SuppressLint("NewApi")
static int getLayoutDirection(View v) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
return v.getLayoutDirection();
}
return View.LAYOUT_DIRECTION_LTR;
}
public void setLayoutDirection(int layoutDirection) {
if (layoutDirection == View.LAYOUT_DIRECTION_RTL) {
this.layoutDirection = layoutDirection;
} else {
this.layoutDirection = View.LAYOUT_DIRECTION_LTR;
}
}
@SuppressWarnings("")
public static int getLayoutDirection(View v) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
return v.getLayoutDirection();
}
return View.LAYOUT_DIRECTION_LTR;
}
@VisibleForTesting
static boolean isPastLastItem(
int top, int left, int right, @NonNull MotionEvent e, int direction) {
if (direction == View.LAYOUT_DIRECTION_LTR) {
return e.getX() > right && e.getY() > top;
} else {
return e.getX() < left && e.getY() > top;
}
}
private static void addAction1(
@NonNull final Context context,
@NonNull final NotificationCompat.Builder b) {
final int direction = context.getResources().getInteger(R.integer.layoutDirection);
switch (direction) {
case View.LAYOUT_DIRECTION_RTL:
addActionNext(context, b);
break;
case View.LAYOUT_DIRECTION_LTR:
default:
addActionPrev(context, b);
break;
}
}
@SuppressLint("NewApi")
static int getLayoutDirection(View v) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
return v.getLayoutDirection();
}
return View.LAYOUT_DIRECTION_LTR;
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public int getGravity() {
if(Utils.hasJellyBeanMR1()){
Configuration config = getResources().getConfiguration();
if(config.getLayoutDirection() != View.LAYOUT_DIRECTION_LTR){
return Gravity.LEFT;
}
}
return Gravity.RIGHT;
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public int getGravity() {
if(Utils.hasJellyBeanMR1()){
Configuration config = getResources().getConfiguration();
if(config.getLayoutDirection() != View.LAYOUT_DIRECTION_LTR){
return Gravity.LEFT;
}
}
return Gravity.RIGHT;
}
private static void addAction3(@NonNull final Context context,
@NonNull final NotificationCompat.Builder b) {
final int direction = context.getResources().getInteger(R.integer.layoutDirection);
switch (direction) {
case View.LAYOUT_DIRECTION_RTL:
addActionPrev(context, b);
break;
case View.LAYOUT_DIRECTION_LTR:
default:
addActionNext(context, b);
break;
}
}
/**
* Sets window layout rtl.
*/
public static void setWindowLayoutRtl(Activity activity) {
if (activity.getWindow()
.getDecorView()
.getLayoutDirection() == View.LAYOUT_DIRECTION_LTR) {
activity.getWindow()
.getDecorView()
.setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
}
}
@Override
public int getLayoutDirection() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
return super.getLayoutDirection();
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
return View.LAYOUT_DIRECTION_LTR;
}
//noinspection WrongConstant
return 0; // LAYOUT_DIRECTION_LTR
}
private static int getStart(@NonNull CoordinatorLayout parent, @NonNull Rect rect) {
return parent.getLayoutDirection() == View.LAYOUT_DIRECTION_LTR ? rect.left : rect.right;
}
protected void cropImageAndSetWallpaper(Uri uri,
OnBitmapCroppedHandler onBitmapCroppedHandler, final boolean finishActivityWhenDone) {
boolean centerCrop = getResources().getBoolean(R.bool.center_crop);
// Get the crop
boolean ltr = mCropView.getLayoutDirection() == View.LAYOUT_DIRECTION_LTR;
Display d = getWindowManager().getDefaultDisplay();
Point displaySize = new Point();
d.getSize(displaySize);
boolean isPortrait = displaySize.x < displaySize.y;
Point defaultWallpaperSize = getDefaultWallpaperSize(getResources(),
getWindowManager());
// Get the crop
RectF cropRect = mCropView.getCrop();
Point inSize = mCropView.getSourceDimensions();
int cropRotation = mCropView.getImageRotation();
float cropScale = mCropView.getWidth() / (float) cropRect.width();
Matrix rotateMatrix = new Matrix();
rotateMatrix.setRotate(cropRotation);
float[] rotatedInSize = new float[] { inSize.x, inSize.y };
rotateMatrix.mapPoints(rotatedInSize);
rotatedInSize[0] = Math.abs(rotatedInSize[0]);
rotatedInSize[1] = Math.abs(rotatedInSize[1]);
// due to rounding errors in the cropview renderer the edges can be slightly offset
// therefore we ensure that the boundaries are sanely defined
cropRect.left = Math.max(0, cropRect.left);
cropRect.right = Math.min(rotatedInSize[0], cropRect.right);
cropRect.top = Math.max(0, cropRect.top);
cropRect.bottom = Math.min(rotatedInSize[1], cropRect.bottom);
// ADJUST CROP WIDTH
// Extend the crop all the way to the right, for parallax
// (or all the way to the left, in RTL)
float extraSpace;
if (centerCrop) {
extraSpace = 2f * Math.min(rotatedInSize[0] - cropRect.right, cropRect.left);
} else {
extraSpace = ltr ? rotatedInSize[0] - cropRect.right : cropRect.left;
}
// Cap the amount of extra width
float maxExtraSpace = defaultWallpaperSize.x / cropScale - cropRect.width();
extraSpace = Math.min(extraSpace, maxExtraSpace);
if (centerCrop) {
cropRect.left -= extraSpace / 2f;
cropRect.right += extraSpace / 2f;
} else {
if (ltr) {
cropRect.right += extraSpace;
} else {
cropRect.left -= extraSpace;
}
}
// ADJUST CROP HEIGHT
if (isPortrait) {
cropRect.bottom = cropRect.top + defaultWallpaperSize.y / cropScale;
} else { // LANDSCAPE
float extraPortraitHeight =
defaultWallpaperSize.y / cropScale - cropRect.height();
float expandHeight =
Math.min(Math.min(rotatedInSize[1] - cropRect.bottom, cropRect.top),
extraPortraitHeight / 2);
cropRect.top -= expandHeight;
cropRect.bottom += expandHeight;
}
final int outWidth = (int) Math.round(cropRect.width() * cropScale);
final int outHeight = (int) Math.round(cropRect.height() * cropScale);
Runnable onEndCrop = new Runnable() {
public void run() {
updateWallpaperDimensions(outWidth, outHeight);
if (finishActivityWhenDone) {
setResult(Activity.RESULT_OK);
finish();
}
}
};
BitmapCropTask cropTask = new BitmapCropTask(this, uri,
cropRect, cropRotation, outWidth, outHeight, true, false, onEndCrop);
if (onBitmapCroppedHandler != null) {
cropTask.setOnBitmapCropped(onBitmapCroppedHandler);
}
cropTask.execute();
}
private void setAmPmStart(boolean isAmPmAtStart) {
final RelativeLayout.LayoutParams params =
(RelativeLayout.LayoutParams) mAmPmLayout.getLayoutParams();
if (params.getRule(RelativeLayout.RIGHT_OF) != 0
|| params.getRule(RelativeLayout.LEFT_OF) != 0) {
// Horizontal mode, with AM/PM appearing to left/right of hours and minutes.
final boolean isAmPmAtLeft;
if (TextUtils.getLayoutDirectionFromLocale(mLocale) == View.LAYOUT_DIRECTION_LTR) {
isAmPmAtLeft = isAmPmAtStart;
} else {
isAmPmAtLeft = !isAmPmAtStart;
}
if (mIsAmPmAtLeft == isAmPmAtLeft) {
// AM/PM is already at the correct location. No change needed.
return;
}
if (isAmPmAtLeft) {
params.removeRule(RelativeLayout.RIGHT_OF);
params.addRule(RelativeLayout.LEFT_OF, mHourView.getId());
} else {
params.removeRule(RelativeLayout.LEFT_OF);
params.addRule(RelativeLayout.RIGHT_OF, mMinuteView.getId());
}
mIsAmPmAtLeft = isAmPmAtLeft;
} else if (params.getRule(RelativeLayout.BELOW) != 0
|| params.getRule(RelativeLayout.ABOVE) != 0) {
// Vertical mode, with AM/PM appearing to top/bottom of hours and minutes.
if (mIsAmPmAtTop == isAmPmAtStart) {
// AM/PM is already at the correct location. No change needed.
return;
}
final int otherViewId;
if (isAmPmAtStart) {
otherViewId = params.getRule(RelativeLayout.BELOW);
params.removeRule(RelativeLayout.BELOW);
params.addRule(RelativeLayout.ABOVE, otherViewId);
} else {
otherViewId = params.getRule(RelativeLayout.ABOVE);
params.removeRule(RelativeLayout.ABOVE);
params.addRule(RelativeLayout.BELOW, otherViewId);
}
// Switch the top and bottom paddings on the other view.
final View otherView = mRadialTimePickerHeader.findViewById(otherViewId);
final int top = otherView.getPaddingTop();
final int bottom = otherView.getPaddingBottom();
final int left = otherView.getPaddingLeft();
final int right = otherView.getPaddingRight();
otherView.setPadding(left, bottom, right, top);
mIsAmPmAtTop = isAmPmAtStart;
}
mAmPmLayout.setLayoutParams(params);
}
/**
* Utility method to check the screen direction
* @return true if direction is LTR false else wise
*/
public static boolean isDirectionLeftToRight() {
Configuration config = MainApplication.instance().getResources().getConfiguration();
return config.getLayoutDirection() == View.LAYOUT_DIRECTION_LTR;
}