返回

Android 屏幕圆角实现:打造您的时尚手机

Android







## Android 屏幕圆角实现:圆润视觉新体验

现如今,许多全面屏手机的屏幕四角采用圆角设计,这种圆润的视觉效果为用户带来别样的体验。让我们先来欣赏一下「锤子」手机的圆角屏幕,直观感受一下圆角的魅力。

当然,这种圆角效果是通过硬件实现的,猜测可能是使用方形显示屏,然后在上面做了一个圆角遮罩。那么对于我们这些没有硬件支持的手机,是否就不能拥有圆角屏幕效果了呢?答案是肯定的!

## 实现方法

要实现 Android 中的圆角屏幕效果,我们可以使用圆角布局或自定义视图。

### 圆角布局

可以使用 Android 提供的 CardView 或 RoundRectCornerLayout 等圆角布局来实现。

android.support.v7.widget.CardView android:layout_width="match_parent" android:layout_height="wrap_content" app:cardCornerRadius="10dp"

<!-- 内容 -->

</android.support.v7.widget.CardView>

com.github.florent37.shapeofview.shapes.RoundRectView android:layout_width="match_parent" android:layout_height="wrap_content" app:shape_round_rect_radius="10dp"

<!-- 内容 -->

</com.github.florent37.shapeofview.shapes.RoundRectView>


### 自定义视图

public class RoundCornerImageView extends ImageView {

private float radius;

public RoundCornerImageView(Context context) {
    super(context);
}

public RoundCornerImageView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init(context, attrs);
}

public RoundCornerImageView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init(context, attrs);
}

private void init(Context context, AttributeSet attrs) {
    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RoundCornerImageView);
    radius = a.getDimension(R.styleable.RoundCornerImageView_radius, 0);
    a.recycle();
}

@Override
protected void onDraw(Canvas canvas) {
    Path path = new Path();
    path.addRoundRect(0, 0, getWidth(), getHeight(), radius, radius, Path.Direction.CW);
    canvas.clipPath(path);
    super.onDraw(canvas);
}

}


com.example.RoundCornerImageView android:layout_width="wrap_content" android:layout_height="wrap_content" app:radius="10dp"

<!-- 内容 -->

</com.example.RoundCornerImageView>


## 结语

以上就是实现 Android 屏幕圆角效果的几种方法,您可以根据需要选择适合自己的方式。希望这些圆角效果能为您的应用带来更时尚、更美观的外观。