2017-06-17 109 views
-6

一个圆形视图我想打一个圆形的观点如下如何让机器人

enter image description here

我希望创建与编程方式更改半径,颜色这一观点。不能通过XML配置

+1

你想让它?酷... –

+0

是的。也许我可怜的英语不会暴露我的想法。我想创建一个java类来绘制类似上面的视图,并且它在里面是透明的。我不知道我的问题出了什么问题? –

+1

'我期望创造这个观点'我们也期望**你**创造它。或者至少要尝试。 –

回答

2

结交新类

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)); 
    } 
} 
+0

它在里面不透明 – pskink

+0

你可以把它缩小! myLayout.setBackground(new ShapeMaker()。circle(Color.TRANSPARENT,Color.BLACK,1,10)); –

+0

不,不,不,看OP的形象,他希望绿地面积定制的(不同颜色)与透明的“洞”里 - 像与圆内cormers框架 – pskink