我想知道你是否可以帮助我。我有一个相对布局,顶部有一个imageview。在相对布局周围,我有一个边框(layout_bg.xml)。从我附上的图像中可以看出,边框不会在图像周围继续,这就是我想要的。我知道我需要一些额外的代码,但由于我对java很新,我无法弄清楚或在网上找到解决方案,我希望你能帮助我或指出我正确的方向。
我想做什么:
让图像适合我的画面,但边框和左上角和右上角呈圆角。
图片
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFFFFF"/>
<corners android:radius="8dp"/>
<stroke android:width="3dp" android:color="#50A4A4A4" />
<padding android:left="0dp" android:top="0dp" android:right="0dp" android:bottom="0dp" />
</shape>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:geekui="http://schemas.android.com/apk/res-auto"
android:background="@drawable/layout_bg"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="30dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="70dp">
<ImageView
android:id="@+id/imageView_player"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:layout_width="match_parent"
android:layout_height="225dp"
android:layout_alignParentTop="true" />
</RelativeLayout>
答案 0 :(得分:2)
查看下面的代码,使ImageView在左上角和右上角都有圆角。
public class TopRoundImageView extends ImageView {
public TopRoundImageView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public TopRoundImageView(Context context) {
super(context);
init();
}
private final RectF roundRect = new RectF();
private float rect_adius = 7;
private final Paint maskPaint = new Paint();
private final Paint zonePaint = new Paint();
private void init() {
maskPaint.setAntiAlias(true);
maskPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
zonePaint.setAntiAlias(true);
zonePaint.setColor(Color.WHITE);
float density = getResources().getDisplayMetrics().density;
rect_adius = rect_adius * density;
}
public void setRectAdius(float adius) {
rect_adius = adius;
invalidate();
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
int w = getWidth();
int h = getHeight();
roundRect.set(0, 0, w, h + rect_adius);
}
@Override
public void draw(Canvas canvas) {
canvas.saveLayer(roundRect, zonePaint, Canvas.ALL_SAVE_FLAG);
canvas.drawRoundRect(roundRect, rect_adius, rect_adius, zonePaint);
canvas.saveLayer(roundRect, maskPaint, Canvas.ALL_SAVE_FLAG);
super.draw(canvas);
canvas.restore();
}
答案 1 :(得分:1)
您可以使用此类RoundedBitmapDrawableFactory
RoundedBitmapDrawable
类
RoundedBitmapDrawable roundedDrawable = RoundedBitmapDrawableFactory.create(getResources(), BitmapFactory.decodeResource(getResources(), R.drawable.your_drawable_resource));
要设置角半径,请执行此操作
roundedDrawable.setCornerRadius(5.0f);
然后将其设置在您的ImageView
上
imageView.setDrawableResource(roundedDrawable)
点击HERE
了解有关RoundedBitmapDrawableFactory
课程的更多信息。
和HERE
对于RoundedBitmapDrawable
类
希望这有帮助。