Skip to content

Commit

Permalink
[fix bug] 修复尺寸计算微小偏差
Browse files Browse the repository at this point in the history
  • Loading branch information
Lauzy committed Jul 25, 2018
1 parent 7d3f972 commit fd80eba
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 43 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
app:anim_duration="300"
app:bg_color="#E0E0E0"
app:btn_color="#000000"
app:gap_width="30"/>
app:gap_width="3dp"/>

```

Expand Down Expand Up @@ -73,7 +73,7 @@
|-------------------|---------------|---------------|
|app:bg_color |setBgColor(int color)|按钮的背景颜色,默认为白色
|app:btn_color |setBtnColor(int color)|暂停播放按钮颜色,默认为黑色
|app:gap_width |setGapWidth(int width)|暂停时左右两个矩形的距离,默认为暂停时矩形的宽度
|app:gap_width |setGapWidth(float width)|暂停时左右两个矩形的距离,默认为暂停时矩形的宽度
|app:space_padding |setSpacePadding(float space)|按钮边缘与播放暂停按钮的距离
|app:anim_duration |setAnimDuration(int duration)|动画时长,默认为200ms
|app:anim_direction |setDirection(Direction direction)|动画旋转方向,默认为顺时针。属性值:PlayPauseView.Direction.POSITIVE(顺时针),PlayPauseView.Direction.NEGATIVE(逆时针)
Expand Down
16 changes: 7 additions & 9 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@

<Button
android:id="@+id/btn_paly_pause"
android:text="Click"
android:textAllCaps="false"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
android:layout_height="wrap_content"
android:text="Click"
android:textAllCaps="false"/>

<com.freedom.lauzy.playpauseviewlib.PlayPauseView
android:id="@+id/play_pause_view1"
Expand All @@ -24,8 +24,7 @@
app:anim_direction="positive"
app:anim_duration="300"
app:bg_color="#E0E0E0"
app:btn_color="#000000"
app:gap_width="30"/>
app:btn_color="#000000"/>

<com.freedom.lauzy.playpauseviewlib.PlayPauseView
android:id="@+id/play_pause_view2"
Expand All @@ -34,10 +33,9 @@
android:layout_gravity="center"
android:layout_marginTop="40dp"
android:background="?attr/selectableItemBackgroundBorderless"
app:bg_color="#E0E0E0"
app:anim_duration="300"
app:anim_direction="negative"
app:btn_color="#000000"
app:gap_width="30"/>
app:anim_duration="300"
app:bg_color="#E0E0E0"
app:btn_color="#000000"/>

</LinearLayout>
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Sat Aug 12 17:06:51 GMT+08:00 2017
#Wed Jul 25 17:20:40 CST 2018
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public class PlayPauseView extends View {
private boolean isPlaying;
private float mRectWidth; //圆内矩形宽度
private float mRectHeight; //圆内矩形高度
private int mRectLT; //矩形左侧上侧坐标
private int mRadius; //圆的半径
private float mRectLT; //矩形左侧上侧坐标
private float mRadius; //圆的半径
private int mBgColor = Color.WHITE;
private int mBtnColor = Color.BLACK;
private int mDirection = Direction.POSITIVE.value;
Expand Down Expand Up @@ -63,34 +63,34 @@ private void init(Context context, AttributeSet attrs) {
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.PlayPauseView);
mBgColor = ta.getColor(R.styleable.PlayPauseView_bg_color, Color.WHITE);
mBtnColor = ta.getColor(R.styleable.PlayPauseView_btn_color, Color.BLACK);
mGapWidth = ta.getFloat(R.styleable.PlayPauseView_gap_width, 0);
mGapWidth = ta.getDimensionPixelSize(R.styleable.PlayPauseView_gap_width, dp2px(context, 0));
mPadding = ta.getDimensionPixelSize(R.styleable.PlayPauseView_space_padding, dp2px(context, 0));
mDirection = ta.getInt(R.styleable.PlayPauseView_anim_direction, Direction.POSITIVE.value);
mPadding = ta.getFloat(R.styleable.PlayPauseView_space_padding, 0);
mAnimDuration = ta.getInt(R.styleable.PlayPauseView_anim_duration, 200);
ta.recycle();

setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}

@SuppressWarnings("unused")
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
mWidth = MeasureSpec.getSize(widthMeasureSpec);
mHeight = MeasureSpec.getSize(heightMeasureSpec);
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
switch (widthMode) {
case MeasureSpec.EXACTLY:
mWidth = mHeight = Math.min(mWidth, mHeight);
setMeasuredDimension(mWidth, mHeight);
break;
case MeasureSpec.AT_MOST:
float density = getResources().getDisplayMetrics().density;
mWidth = mHeight = (int) (50 * density); //默认50dp
setMeasuredDimension(mWidth, mHeight);
break;
case MeasureSpec.UNSPECIFIED:
break;
if (widthMode == MeasureSpec.EXACTLY) {
mWidth = Math.min(mWidth, mHeight);
} else {
mWidth = dp2px(getContext(), 50);
}
if (heightMode == MeasureSpec.EXACTLY) {
mHeight = Math.min(mWidth, mHeight);
} else {
mHeight = dp2px(getContext(), 50);
}
mWidth = mHeight = Math.min(mWidth, mHeight);
setMeasuredDimension(mWidth, mHeight);
}

@Override
Expand All @@ -104,25 +104,25 @@ private void initValue() {
// int rectLT = (int) (mWidth / 2 - radius / Math.sqrt(2));
// int rectRB = (int) (mWidth / 2 + radius / Math.sqrt(2));
mRadius = mWidth / 2;
/* if (getPadding() > mRadius / Math.sqrt(2) || mPadding < 0) {
*//*throw new IllegalArgumentException("The value of your padding is too large. " +
/* if (getPadding() > mRadius / Math.sqrt(2) || mPadding < 0) {
*//*throw new IllegalArgumentException("The value of your padding is too large. " +
"The value must not be greater than " + (int) (mRadius / Math.sqrt(2)));*//*
}*/
mPadding = getSpacePadding() == 0 ? mRadius / 3f : getSpacePadding();
if (getSpacePadding() > mRadius / Math.sqrt(2) || mPadding < 0) {
mPadding = mRadius / 3f; //默认值
}
float space = (float) (mRadius / Math.sqrt(2) - mPadding); //矩形宽高的一半
mRectLT = (int) (mRadius - space);
int rectRB = (int) (mRadius + space);
mRect.top = mRectLT;
mRect.bottom = rectRB;
mRect.left = mRectLT;
mRect.right = rectRB;
mRectLT = mRadius - space;
float rectRB = mRadius + space;
mRect.top = (int) mRectLT;
mRect.bottom = (int) rectRB;
mRect.left = (int) mRectLT;
mRect.right = (int) rectRB;
// mRectWidth = mRect.width();
// mRectHeight = mRect.height();
mRectWidth = 2 * space + 1; //改为float类型,否则动画有抖动。并增加一像素防止三角形之间有缝隙
mRectHeight = 2 * space + 1;
mRectWidth = 2 * space;
mRectHeight = 2 * space;
mGapWidth = getGapWidth() != 0 ? getGapWidth() : mRectWidth / 3;
mProgress = isPlaying ? 0 : 1;
mAnimDuration = getAnimDuration() < 0 ? 200 : getAnimDuration();
Expand Down Expand Up @@ -250,6 +250,13 @@ public interface PlayPauseListener {
void pause();
}

public int dp2px(Context context, float dpVal) {
float density = context.getResources().getDisplayMetrics().density;
return (int) (density * dpVal + 0.5f);
// return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
// dpVal, context.getResources().getDisplayMetrics());
}

/* ------------下方是参数------------- */

public boolean isPlaying() {
Expand All @@ -260,7 +267,7 @@ public void setPlaying(boolean playing) {
isPlaying = playing;
}

public void setGapWidth(int gapWidth) {
public void setGapWidth(float gapWidth) {
mGapWidth = gapWidth;
}

Expand Down
4 changes: 2 additions & 2 deletions playpauseviewlib/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<declare-styleable name="PlayPauseView">
<attr name="bg_color" format="color"/>
<attr name="btn_color" format="color"/>
<attr name="gap_width" format="float"/>
<attr name="space_padding" format="float"/>
<attr name="gap_width" format="dimension|reference"/>
<attr name="space_padding" format="dimension|reference"/>
<attr name="anim_duration" format="integer"/>
<attr name="anim_direction">
<enum name="positive" value="1"/>
Expand Down

0 comments on commit fd80eba

Please sign in to comment.