androidx
Zhanty 5 years ago
parent bc7ca1b9a5
commit 90df0fded0
  1. 2
      config/repository.gradle
  2. 13
      lib_base/src/main/java/com/android/base/app/fragment/Fragments.kt
  3. 104
      lib_base/src/main/java/com/android/base/widget/ratio/CenterDrawableTextView.java
  4. 58
      lib_base/src/main/java/com/android/base/widget/ratio/TopDrawableCenterTextView.java

@ -54,7 +54,7 @@ static Repository innerNewRepository() {
] ]
def anko_version = '0.10.7' def anko_version = '0.10.7'
def kotlin_version = '1.3.11' def kotlin_version = '1.3.41'
def kotlinLibraries = [ def kotlinLibraries = [
//Kotlin //Kotlin

@ -254,7 +254,7 @@ class EnhanceFragmentTransaction constructor(
fragmentTransaction.add(confirmLayoutId(containerId), fragment, nonnullTag) fragmentTransaction.add(confirmLayoutId(containerId), fragment, nonnullTag)
if (transition) { if (transition) {
//set a transition //set a transition
setTransitionOpen() setOpeningTransition()
} }
return this return this
} }
@ -271,7 +271,7 @@ class EnhanceFragmentTransaction constructor(
fragmentTransaction.replace(confirmLayoutId(containerId), fragment, nonnullTag) fragmentTransaction.replace(confirmLayoutId(containerId), fragment, nonnullTag)
//set a transition //set a transition
if (transition) { if (transition) {
setTransitionOpen() setOpeningTransition()
} }
return this return this
} }
@ -298,7 +298,7 @@ class EnhanceFragmentTransaction constructor(
fun replaceWithDefaultContainer(fragment: Fragment, tag: String? = null, transition: Boolean = true): FragmentTransaction { fun replaceWithDefaultContainer(fragment: Fragment, tag: String? = null, transition: Boolean = true): FragmentTransaction {
val nonnullTag = (tag ?: fragment.javaClassName()) val nonnullTag = (tag ?: fragment.javaClassName())
if (transition) { if (transition) {
setTransitionOpen() setOpeningTransition()
} }
return fragmentTransaction.replace(FragmentConfig.defaultContainerId(), fragment, nonnullTag) return fragmentTransaction.replace(FragmentConfig.defaultContainerId(), fragment, nonnullTag)
} }
@ -319,15 +319,16 @@ class EnhanceFragmentTransaction constructor(
} }
} }
fun setTransitionOpen(): FragmentTransaction { @Suppress
fun setOpeningTransition(): FragmentTransaction {
return fragmentTransaction.setTransition(TRANSIT_FRAGMENT_OPEN) return fragmentTransaction.setTransition(TRANSIT_FRAGMENT_OPEN)
} }
fun setTransitionClose(): FragmentTransaction { fun setClosingTransition(): FragmentTransaction {
return fragmentTransaction.setTransition(TRANSIT_FRAGMENT_CLOSE) return fragmentTransaction.setTransition(TRANSIT_FRAGMENT_CLOSE)
} }
fun setTransitionFade(): FragmentTransaction { fun setFadingTransition(): FragmentTransaction {
return fragmentTransaction.setTransition(TRANSIT_FRAGMENT_FADE) return fragmentTransaction.setTransition(TRANSIT_FRAGMENT_FADE)
} }

@ -0,0 +1,104 @@
package com.android.base.widget.ratio;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.Gravity;
/**
* 应用场景比如 TextView width match 此时使用的 leftDrawable 是靠左的且无法通过 Gravity 属性来修改其位置该控件用于类似此种情况下使 Drawable 居中仅支持单个方向的 Drawable
* 优先级为
*
* @author Ztiany
* Email: 1169654504@qq.com
*/
public class CenterDrawableTextView extends RatioTextView {
private Paint.FontMetrics metrics = new Paint.FontMetrics();
public CenterDrawableTextView(Context context) {
super(context);
}
public CenterDrawableTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CenterDrawableTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onDraw(Canvas canvas) {
Drawable[] compoundDrawables = getCompoundDrawables();
setToCenterPadding(compoundDrawables, canvas);
super.onDraw(canvas);
}
private void setToCenterPadding(Drawable[] compoundDrawables, Canvas canvas) {
if (compoundDrawables == null) {
return;
}
Drawable leftDrawable = compoundDrawables[0];
Drawable topDrawable = compoundDrawables[1];
Drawable rightDrawable = compoundDrawables[2];
Drawable bottomDrawable = compoundDrawables[3];
if (leftDrawable != null) {
setLeftDrawableCenter(canvas, leftDrawable);
} else if (topDrawable != null) {
setTopDrawableCenter(canvas, topDrawable);
} else if (rightDrawable != null) {
setRightDrawableCenter(canvas, rightDrawable);
} else if (bottomDrawable != null) {
setBottomDrawableCenter(canvas, bottomDrawable);
}
}
private void setBottomDrawableCenter(Canvas canvas, Drawable downDrawable) {
setGravity(Gravity.BOTTOM | getGravity());
int compoundDrawablePadding = getCompoundDrawablePadding();
TextPaint paint = getPaint();
paint.getFontMetrics(metrics);
float contentHeight = (metrics.bottom - metrics.top + compoundDrawablePadding + downDrawable.getIntrinsicHeight());
int measuredHeight = getMeasuredHeight();
canvas.translate(0F, -(measuredHeight / 2.0F - contentHeight / 2));
}
private void setRightDrawableCenter(Canvas canvas, Drawable rightDrawable) {
setGravity(Gravity.END | getGravity());
int compoundDrawablePadding = getCompoundDrawablePadding();
TextPaint paint = getPaint();
float measureText = paint.measureText(getText().toString());
float contentWidth = (measureText + compoundDrawablePadding + rightDrawable.getIntrinsicHeight());
int measuredWidth = getMeasuredWidth();
canvas.translate(-(measuredWidth / 2.0F - contentWidth / 2), 0F);
}
private void setLeftDrawableCenter(Canvas canvas, Drawable leftDrawable) {
setGravity(Gravity.START | getGravity());
int compoundDrawablePadding = getCompoundDrawablePadding();
TextPaint paint = getPaint();
float measureText = paint.measureText(getText().toString());
float contentWidth = (measureText + compoundDrawablePadding + leftDrawable.getIntrinsicHeight());
int measuredWidth = getMeasuredWidth();
canvas.translate((measuredWidth / 2.0F - contentWidth / 2), 0F);
}
private void setTopDrawableCenter(Canvas canvas, Drawable topDrawable) {
int compoundDrawablePadding = getCompoundDrawablePadding();
TextPaint paint = getPaint();
paint.getFontMetrics(metrics);
float contentHeight = (metrics.bottom - metrics.top + compoundDrawablePadding + topDrawable.getIntrinsicHeight());
int measuredHeight = getMeasuredHeight();
canvas.translate(0F, (measuredHeight / 2.0F - contentHeight / 2));
}
}

@ -1,58 +0,0 @@
package com.android.base.widget.ratio;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.text.TextPaint;
import android.util.AttributeSet;
/**
* 使 TopDrawable 居中不要设置 gravity 属性为 center center_vertical
*
* @author Ztiany
* Email: 1169654504@qq.com
*/
public class TopDrawableCenterTextView extends RatioTextView {
private Paint.FontMetrics metrics = new Paint.FontMetrics();
public TopDrawableCenterTextView(Context context) {
super(context);
}
public TopDrawableCenterTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public TopDrawableCenterTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onDraw(Canvas canvas) {
Drawable[] compoundDrawables = getCompoundDrawables();
setToCenterPadding(compoundDrawables, canvas);
super.onDraw(canvas);
}
private void setToCenterPadding(Drawable[] compoundDrawables, Canvas canvas) {
if (compoundDrawables == null) {
return;
}
Drawable topDrawable = compoundDrawables[1];
if (topDrawable == null) {
return;
}
int compoundDrawablePadding = getCompoundDrawablePadding();
TextPaint paint = getPaint();
paint.getFontMetrics(metrics);
float contentHeight = (metrics.bottom - metrics.top + compoundDrawablePadding + topDrawable.getIntrinsicHeight());
int measuredHeight = getMeasuredHeight();
canvas.translate(0, measuredHeight / 2 - contentHeight / 2);
}
}
Loading…
Cancel
Save