Skip to content

Commit

Permalink
修复issue #112
Browse files Browse the repository at this point in the history
  • Loading branch information
lihangleo2 committed Sep 13, 2021
1 parent 4707009 commit 0356840
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 109 deletions.
1 change: 1 addition & 0 deletions app/src/main/java/com/leo/ShapeActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class ShapeActivity extends AppCompatActivity {
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this, R.layout.activity_shape);
binding.ShadowLayoutImage.setSelected(true);
binding.ShadowLayoutImage.setOnClickListener(v -> {
binding.ShadowLayoutImage.setSelected(!binding.ShadowLayoutImage.isSelected());
});
Expand Down
205 changes: 114 additions & 91 deletions shadowLibrary/src/main/java/com/lihang/GlideRoundUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.util.Log;
import android.view.View;

import com.bumptech.glide.Glide;
Expand All @@ -18,38 +19,41 @@
* on 2020/8/3.
*/
class GlideRoundUtils {
public static void setRoundCorner(final View view, final Drawable resourceId, final float cornerDipValue) {
public static void setRoundCorner(final View view, final Drawable resourceId, final float cornerDipValue, final String currentTag) {

if (cornerDipValue == 0) {
if (view.getMeasuredWidth() == 0 && view.getMeasuredHeight() == 0) {
view.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
view.removeOnLayoutChangeListener(this);
Glide.with(view)
.asDrawable()
.load(resourceId)
.transform(new CenterCrop())
.override(view.getMeasuredWidth(), view.getMeasuredHeight())
.into(new CustomTarget<Drawable>() {
@Override
public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
view.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
view.removeOnLayoutChangeListener(this);
Glide.with(view)
.asDrawable()
.load(resourceId)
.transform(new CenterCrop())
.override(view.getMeasuredWidth(), view.getMeasuredHeight())
.into(new CustomTarget<Drawable>() {
@Override
public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {

String lastTag = (String) view.getTag(R.id.action_container);
if (lastTag.equals(currentTag)) {
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.JELLY_BEAN) {
view.setBackgroundDrawable(resource);
} else {
view.setBackground(resource);
}

}
}

@Override
public void onLoadCleared(@Nullable Drawable placeholder) {
@Override
public void onLoadCleared(@Nullable Drawable placeholder) {
}
});
}
});

}
});
}
});
} else {

if (view.getMeasuredWidth() != 0 || view.getMeasuredHeight() != 0) {
Glide.with(view)
.asDrawable()
.load(resourceId)
Expand All @@ -68,41 +72,44 @@ public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? s

@Override
public void onLoadCleared(@Nullable Drawable placeholder) {

}
});
}

} else {

if (view.getMeasuredWidth() == 0 && view.getMeasuredHeight() == 0) {
view.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
view.removeOnLayoutChangeListener(this);
Glide.with(view)
.load(resourceId)
.transform(new CenterCrop(), new RoundedCorners((int) cornerDipValue))
.override(view.getMeasuredWidth(), view.getMeasuredHeight())
.into(new CustomTarget<Drawable>() {
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
view.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
view.removeOnLayoutChangeListener(this);
Glide.with(view)
.load(resourceId)
.transform(new CenterCrop(), new RoundedCorners((int) cornerDipValue))
.override(view.getMeasuredWidth(), view.getMeasuredHeight())
.into(new CustomTarget<Drawable>() {
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
String lastTag = (String) view.getTag(R.id.action_container);
if (lastTag.equals(currentTag)) {
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.JELLY_BEAN) {
view.setBackgroundDrawable(resource);
} else {
view.setBackground(resource);
}
}
}

@Override
public void onLoadCleared(@Nullable Drawable placeholder) {
@Override
public void onLoadCleared(@Nullable Drawable placeholder) {

}
});
}
});
} else {
}
});
}
});


if (view.getMeasuredWidth() != 0 || view.getMeasuredHeight() != 0) {
Glide.with(view)
.load(resourceId)
.transform(new CenterCrop(), new RoundedCorners((int) cornerDipValue))
Expand Down Expand Up @@ -130,35 +137,40 @@ public void onLoadCleared(@Nullable Drawable placeholder) {
}


public static void setCorners(final View view, final Drawable resourceId, final float leftTop_corner, final float leftBottom_corner, final float rightTop_corner, final float rightBottom_corner) {
public static void setCorners(final View view, final Drawable resourceId, final float leftTop_corner, final float leftBottom_corner, final float rightTop_corner, final float rightBottom_corner, final String currentTag) {
if (leftTop_corner == 0 && leftBottom_corner == 0 && rightTop_corner == 0 && rightBottom_corner == 0) {
if (view.getMeasuredWidth() == 0 && view.getMeasuredHeight() == 0) {
view.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
view.removeOnLayoutChangeListener(this);
Glide.with(view)
.load(resourceId)
.override(view.getMeasuredWidth(), view.getMeasuredHeight())
.into(new CustomTarget<Drawable>() {
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {

view.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
view.removeOnLayoutChangeListener(this);
Glide.with(view)
.load(resourceId)
.override(view.getMeasuredWidth(), view.getMeasuredHeight())
.into(new CustomTarget<Drawable>() {
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
String lastTag = (String) view.getTag(R.id.action_container);
if (lastTag.equals(currentTag)) {
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.JELLY_BEAN) {
view.setBackgroundDrawable(resource);
} else {
view.setBackground(resource);
}
}
}

@Override
public void onLoadCleared(@Nullable Drawable placeholder) {
@Override
public void onLoadCleared(@Nullable Drawable placeholder) {

}
});
}
});
} else {
}
});
}
});


if (view.getMeasuredWidth() != 0 || view.getMeasuredHeight() != 0) {
Glide.with(view)
.load(resourceId)
.override(view.getMeasuredWidth(), view.getMeasuredHeight())
Expand All @@ -178,40 +190,50 @@ public void onLoadCleared(@Nullable Drawable placeholder) {

}
});

}

} else {
if (view.getMeasuredWidth() == 0 && view.getMeasuredHeight() == 0) {
view.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
view.removeOnLayoutChangeListener(this);
GlideRoundTransform transform = new GlideRoundTransform(view.getContext(), leftTop_corner, leftBottom_corner, rightTop_corner, rightBottom_corner);
Glide.with(view)
.load(resourceId)
.transform(transform)
.override(view.getMeasuredWidth(), view.getMeasuredHeight())
.into(new CustomTarget<Drawable>() {
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {

/**
* 注意:
* 有特殊角,长宽不为0的状况(实际上也存在长宽不为0但还未渲染到画面上)
* */
final GlideRoundTransform transform = new GlideRoundTransform(view.getContext(), leftTop_corner, leftBottom_corner, rightTop_corner, rightBottom_corner);
view.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
view.removeOnLayoutChangeListener(this);
// GlideRoundTransform transform = new GlideRoundTransform(view.getContext(), leftTop_corner, leftBottom_corner, rightTop_corner, rightBottom_corner);
Glide.with(view)
.load(resourceId)
.transform(transform)
.override(view.getMeasuredWidth(), view.getMeasuredHeight())
.into(new CustomTarget<Drawable>() {
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
String lastTag = (String) view.getTag(R.id.action_container);
if (lastTag.equals(currentTag)) {
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.JELLY_BEAN) {
view.setBackgroundDrawable(resource);
} else {
view.setBackground(resource);
}
}

@Override
public void onLoadCleared(@Nullable Drawable placeholder) {
}

@Override
public void onLoadCleared(@Nullable Drawable placeholder) {

}
});
}
});

}
});
}
});
} else {

GlideRoundTransform transform = new GlideRoundTransform(view.getContext(), leftTop_corner, leftBottom_corner, rightTop_corner, rightBottom_corner);
if (view.getMeasuredWidth() != 0 || view.getMeasuredHeight() != 0) {
Glide.with(view)
.load(resourceId)
.transform(transform)
Expand All @@ -220,10 +242,13 @@ public void onLoadCleared(@Nullable Drawable placeholder) {
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.JELLY_BEAN) {
view.setBackgroundDrawable(resource);
} else {
view.setBackground(resource);
String lastTag = (String) view.getTag(R.id.action_container);
if (lastTag.equals(currentTag)) {
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.JELLY_BEAN) {
view.setBackgroundDrawable(resource);
} else {
view.setBackground(resource);
}
}
}

Expand All @@ -232,9 +257,7 @@ public void onLoadCleared(@Nullable Drawable placeholder) {

}
});

}

}

}
Expand Down
Loading

0 comments on commit 0356840

Please sign in to comment.