Clipping Views(裁剪视图)
首先我们layout,设置两个正方形的TextView
<TextView
android:id="@+id/tv_1"
android:layout_width="100dp"
android:layout_height="100dp"
android:elevation="1dip"
android:layout_gravity="center"
/>
<TextView
android:id="@+id/tv_2"
android:layout_width="100dp"
android:layout_height="100dp"
android:elevation="1dip"
android:layout_gravity="center"
/>
然后我们将它们两分别裁剪成圆角正方形和圆形:
v1 = findViewById(R.id.tv_1);
v2 = findViewById(R.id.tv_2);
ViewOutlineProvider viewOutlineProvider = new ViewOutlineProvider() {
@Override
public void getOutline(View view, Outline outline) {
outline.setRoundRect(0, 0, view.getWidth(), view.getHeight(), 10);
}
};
ViewOutlineProvider viewOutlineProvider1 = new ViewOutlineProvider() {
@Override
public void getOutline(View view, Outline outline) {
outline.setRoundRect(0, 0, view.getWidth(), view.getHeight(), view.getHeight() / 2);
}
};
v1.setOutlineProvider(viewOutlineProvider);
v2.setOutlineProvider(viewOutlineProvider1);
效果如下:
Clipping Views(裁剪视图)
可以使用View.setClipToOutline方法去剪切一个视图的outline区域。
只有rectangle,circle, 和round rectangle outlines支持裁剪(Outline.canClip方法用来判断是否可以裁剪)
floatingShape.setClipToOutline(true);
下面请看一个使用裁剪的例子:
int margin = Math.min(clippedView.getWidth(), clippedView.getHeight()) / 10;
Outline mClip = new Outline();
mClip.setRoundRect(margin, margin, clippedView.getWidth() - margin,
clippedView.getHeight() - margin, margin / 2);
/* Sets the Outline of the View. */
clippedView.setOutline(mClip);
/* Enables clipping on the View. */
clippedView.setClipToOutline(true);
首先创建一个轮廓,给轮廓设置区域大小,添加轮廓到视图上,确认裁剪,效果图如下:
开源项目:AndroidSample:Topeka中的使用:
@SuppressLint("NewApi")
public void setAvatar(@DrawableRes int resId) {
if (ApiLevelHelper.isAtLeast(Build.VERSION_CODES.LOLLIPOP)) { // 高于5.0版本
//为了裁剪一个可绘制的视图形状,需要先设置一个outline然后调用View.setClipToOutline方法
setClipToOutline(true);
setImageResource(resId);
} else {
setAvatarPreLollipop(resId); // 低于5.0版本
}
}
// 低于5.0版本实现方法
private void setAvatarPreLollipop(@DrawableRes int redId) {
Drawable drawable = ResourcesCompat.getDrawable(getResources(), redId,
getContext().getTheme());
BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
RoundedBitmapDrawable roundedBitmapDrawable =
RoundedBitmapDrawableFactory.create(getResources(),bitmapDrawable.getBitmap());
roundedBitmapDrawable.setCircular(true);
setImageDrawable(roundedBitmapDrawable);
}
.....
@Override protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
if (ApiLevelHelper.isLowerThan(Build.VERSION_CODES.LOLLIPOP)) {
return;
}
if (w > 0 && h > 0) {
setOutlineProvider(new RoundOutlineProvider(Math.min(w, h))); //添加轮廓到视图上,确认裁剪
}
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP) public class RoundOutlineProvider extends ViewOutlineProvider{
private final int mSize;
public RoundOutlineProvider(int size) {
if (0 > size) {
throw new IllegalArgumentException("size needs to be > 0. Actually was " + size);
}
mSize = size;
}
@Override public void getOutline(View view, Outline outline) {
outline.setOval(0,0,mSize,mSize);
}
}