【问题标题】:Imageview to fit layout with border and rounded cornersImageview 以适合带有边框和圆角的布局
【发布时间】:2014-12-15 15:15:52
【问题描述】:

我想知道你能不能帮我做点什么。我有一个相对布局,顶部有一个 imageview。在相对布局周围我有一个边框(layout_bg.xml)。正如您从我附加的图像中看到的那样,边框不会在图像周围继续,这就是我想要它做的。我知道我在这里需要一些额外的代码,但由于我对 java 很陌生,所以我无法弄清楚或在线找到解决方案,我希望你能帮助我或为我指明正确的方向。

我想做什么:

使图像适合我的框架,但带有边框和圆形的左上角和右上角。

图片

layout_bg.xml

<?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>

item.xml

<?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>

【问题讨论】:

标签: android android-layout


【解决方案1】:

查看下面的代码,它使 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();
    }

【讨论】:

  • 谢谢!这是完美的。
【解决方案2】:

您可以像这样使用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

希望这会有所帮助。

【讨论】:

  • 首先尝试了其他代码,因为它有效,我还没有尝试过这个,但我非常感谢您的帮助和快速响应。谢谢!
猜你喜欢
  • 1970-01-01
  • 2013-10-14
  • 1970-01-01
  • 1970-01-01
  • 2012-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多