【问题标题】:How to make a rounded view in android [closed]如何在android中制作圆形视图[关闭]
【发布时间】:2017-11-20 05:45:47
【问题描述】:

我想做一个圆角的视图如下

我希望以编程方式更改半径、颜色来创建该视图。不是从 xml 配置的

【问题讨论】:

  • 你想做吗?酷...
  • 是的。也许我糟糕的英语并没有暴露我的想法。我不想构建一个 java 类来绘制像上面这样的视图,并且它在内部是透明的。我不知道我的问题有什么问题?
  • I expect to create that view 我们也希望来创建它。或者,至少,尝试一下。
  • 事实上,我尝试了一些这样的方法:stackoverflow.com/questions/26074784/… 但是结果似乎与我想要的不一样。我不知道要继续前进的关键字。所以我决定在这里问。这可能听起来很愚蠢,但我正在挣扎......
  • 没有证据表明你的努力。

标签: android rounded-corners


【解决方案1】:

新建类

ShapeMaker.class

public class ShapeMaker {

public ShapeMaker() {

}

public GradientDrawable circle (int backgroundColor, int borderColor, int storke, int radius) {
    GradientDrawable shape = new GradientDrawable();
    shape.setShape(GradientDrawable.RECTANGLE);
    shape.setColor(backgroundColor);
    shape.setStroke(storke, borderColor);
    shape.setCornerRadius(radius);

    return shape;
}
}

叫它

 myLayout.setBackground(new ShapeMaker().circle(Color.BLUE, Color.BLACK, 1, 10));

编辑

布局示例:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_green_light"
tools:context="net.netbox4u.gradientcolor.MainActivity">

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginTop="8dp"
    android:elevation="8dp"
    android:text="Imagine"
    android:textSize="30sp"
    android:textStyle="bold"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<LinearLayout
    android:id="@+id/shape_background"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginBottom="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginTop="8dp"
    android:orientation="vertical"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="1.0"></LinearLayout>
</android.support.constraint.ConstraintLayout>

MainActivity 示例:

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        LinearLayout shapeBackgroundLayout = (LinearLayout)findViewById(R.id.shape_background);

        shapeBackgroundLayout.setBackground(new ShapeMaker().circle(Color.WHITE, Color.TRANSPARENT, 0, 50));
    }
}

【讨论】:

  • 内部不透明
  • 您可以自定义! myLayout.setBackground(new ShapeMaker().circle(Color.TRANSPARENT, Color.BLACK, 1, 10));
  • 不,不,不,看 OP 图片,他想要可定制的绿色区域(不同颜色),里面有透明的“孔”——类似于带有圆形内角的框架
  • ShapeMaker().circle(background_color, storke_color, storke_width,corner_radius))
  • 谢谢米洛斯·卢利克。我正在尝试你的代码。我看到 storke_width 值不会扩展颜色而是收缩。我想做一个像pskink说的那样的框架。您能否再回顾一下您的示例?
猜你喜欢
  • 2013-05-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-06
  • 2017-01-19
  • 2016-08-02
相关资源
最近更新 更多