我需要通过扩展ShapeDrawable以编程方式创建带圆角的边框.我需要一个带圆角的黑色边框,外面的像素是白色,内部像素是透明的.我目前的代码存在多个问题,其中的问题是它不会创建一个与边框厚度相同的平滑角,并且边框的外部像素是透明的而不是白色.
这是我目前得到的角落的图片 
这是我在构造函数中为'fill'传递Color.TRANSPARENT的代码:
public class CustomShape extends ShapeDrawable {
private final Paint fillpaint, strokepaint;
public CustomShape(int fill, int strokeWidth,int radius) {
super(new RoundRectShape(new float[] { radius, radius, radius, radius, radius, radius, radius, radius }, null, null));
fillpaint = new Paint(this.getPaint());
fillpaint.setColor(fill);
strokepaint = new Paint(fillpaint);
strokepaint.setStyle(Paint.Style.STROKE);
strokepaint.setStrokeWidth(strokeWidth);
strokepaint.setColor(Color.BLACK);
}
@Override
protected void onDraw(Shape shape, Canvas canvas, Paint paint) {
shape.draw(canvas, fillpaint);
shape.draw(canvas, strokepaint);
}
Run Code Online (Sandbox Code Playgroud)
}
我想为我的Android按钮制作一个drawable,定义为drawable.我发现我可以通过使用一个矩形来设置所有边框,但是当我想要三面时,我有点卡住了.我希望例如打开顶部或底部.
谁能告诉我怎么做?
我定义了以下drawable my_background_drawable.xml:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:gravity="center"
android:shape="rectangle">
<solid android:color="@color/color_stateful" />
</shape>
</item>
<item android:drawable="@drawable/selector_png_drawable" />
</layer-list>
Run Code Online (Sandbox Code Playgroud)
我还定义了以下颜色状态列表资源color_stateful.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" android:color="#FF00ff00"/>
<item android:color="#FFff0000"/>
</selector>
Run Code Online (Sandbox Code Playgroud)
当我将my_background_drawable某个视图设置为某个视图的背景时,我无法观察到color_stateful.xml为我的形状定义的颜色的任何变化,而视图状态实际上已更改(selector_png_drawable.xml是指示符).
但是,当我通过my_background_drawable.xml以下方式修改我时,一切都很好:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- This doesn't work
<item>
<shape android:gravity="center"
android:shape="rectangle">
<solid android:color="@color/color_stateful" />
</shape>
</item>
-->
<item>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape android:gravity="center"
android:shape="rectangle">
<solid android:color="#FF00ff00" />
</shape>
</item> …Run Code Online (Sandbox Code Playgroud) 我正在尝试为我的按钮创建一个具有不同状态的可绘制形状.所以我写了这个:
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" android:color="@android:color/black" >
<shape android:shape="rectangle" >
<solid android:color="@color/NEGATIVE_pressed" />
<stroke
android:width="1dp"
android:color="@color/ORANGE" />
<corners android:radius="4dp" />
</shape>
</item>
<item android:state_focused="true" android:color="@android:color/black" >
<shape android:shape="rectangle" >
<solid android:color="@color/NEGATIVE_focused" />
<stroke
android:width="1dp"
android:color="@color/ORANGE" />
<corners android:radius="4dp" />
</shape>
</item>
<item android:color="@android:color/black" >
<shape android:shape="rectangle" >
<solid android:color="@color/NEGATIVE" />
<stroke
android:width="1dp"
android:color="@color/NEGATIVE" />
<corners android:radius="4dp" />
</shape>
</item>
</selector>
Run Code Online (Sandbox Code Playgroud)
然后在我的按钮中我用它作为 android:background="@drawable/btn_negative_selector"
但是,我想绘制一个底部边框到那个形状,就像3 dp和不同颜色,但我无法弄清楚如何做到这一点.我试过搜索,但找不到适合选择器的东西.有什么建议吗?
我正在尝试为我的textview制作平行四边形背景,但它没有正确显示...它显示以下输出

<layer-list >
<item>
<rotate
android:fromDegrees="10"
android:toDegrees="10"
android:pivotX="-40%"
android:pivotY="87%" >
<shape
android:shape="rectangle" >
<stroke android:color="#000000" android:width="10dp"/>
</shape>
</rotate>
</item>
</layer-list>
Run Code Online (Sandbox Code Playgroud)
我需要像这样的输出........

尝试为Android Ring Shape设置动画,以产生与所示图像序列类似的效果.

我找到了Shape Drawable类型的戒指.
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="ring"
android:innerRadius="75dp"
android:thickness="25dp"
android:useLevel="false">
Run Code Online (Sandbox Code Playgroud)
随着ArcShape(startAngle,sweepAngle)方法.
ShapeDrawable shape = new ShapeDrawable(new ArcShape(0, 360));
shape.setIntrinsicHeight(100);
shape.setIntrinsicWidth(100);
shape.getPaint().setColor(Color.BLACK);
Run Code Online (Sandbox Code Playgroud)
然而,问题在于无法找到可绘制形状的扫掠角度属性或为ArcShape创建内半径的能力.
我试图制作一个流畅的动画.
我正在使用Action Bar Compat,因此带导航抽屉的操作栏向下兼容到API级别9,我想更改操作栏的背景.
我复制了Android开发者的代码:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme"
parent="@style/Theme.AppCompat.Light.DarkActionBar">
<item name="android:actionBarStyle">@style/MyActionBar</item>
<!-- Support library compatibility -->
<item name="actionBarStyle">@style/MyActionBar</item>
</style>
<!-- ActionBar styles -->
<style name="MyActionBar"
parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="android:background">@drawable/actionbar_background</item>
<!-- Support library compatibility -->
<item name="background">@drawable/actionbar_background</item>
</style>
</resources>
Run Code Online (Sandbox Code Playgroud)
这就是问题所在.
当我将图像绘制或颜色作为背景时,它工作正常.但是我想将背景定义为渐变形状,所以我actionbar_background看起来像:
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line">
<gradient
android:startColor="@color/ac_bg_start"
android:endColor="@color/ac_bg_end"
android:type="linear"/>
<size
android:width="1dp"
android:height="48dp"/>
</shape>
Run Code Online (Sandbox Code Playgroud)
我希望它以水平方式重复,但即使这样也会导致错误,实际上是非常有趣的错误.当我尝试运行应用程序时,测试设备甚至模拟器都会重新启动.DeadObjectException在重新启动之前我能够捕获.
背景画如何看起来像?
我的项目要求在运行时动态绘制一个圆.所以为了这个目的,我使用ShapeDrawable以编程方式创建圆,但不幸的是我在CircleShape的ShapeDrawable中找不到任何类或方法,而只是我找到了OvalShape().亲切地请帮我通过ShapeDrawable绘制一个圆圈,只需通过圆的直径或半径即可.提前致谢.任何类型的自定义对我来说都很有用,可以修复我的解决方案.
我用于ShapeDrawable的代码是
public static ShapeDrawable drawCircle (Context context, int width, int height, int color) {
//////Drawing oval & Circle programmatically /////////////
ShapeDrawable oval = new ShapeDrawable (new OvalShape ());
oval.setIntrinsicHeight (height);
oval.setIntrinsicWidth (width);
oval.getPaint ().setColor (color);
return oval;
}
Run Code Online (Sandbox Code Playgroud)
使用的代码 MainActivity.java
if(Build.VERSION.SDK_INT >= 16) {
txtCount.setBackground (Util.drawCircle (MainActivity.this, 50, 50, getResources ().getColor (R.color.yellow)));
txtHotelCount.setText ("20");
}else{
txtCount.setBackgroundDrawable (Util.drawCircle (MainActivity.this, 50, 50, getResources ().getColor (R.color.yellow)));
txtHotelCount.setText ("20");
}
Run Code Online (Sandbox Code Playgroud)
在我的项目中用于TextView的 xml txtCount是
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:background="@color/white">
<TextView …Run Code Online (Sandbox Code Playgroud) android xml-drawable android-canvas shapedrawable android-drawable
我怎么能放弃fromFegrees,toDegrees和android:color="#000000"编程?
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item >
<rotate
android:fromDegrees="45"
android:toDegrees="45"
android:pivotX="-40%"
android:pivotY="87%" >
<shape
android:shape="rectangle" >
<stroke android:color="@android:color/transparent" android:width="10dp"/>
<solid
android:color="#000000" />
</shape>
</rotate>
</item>
</layer-list>
Run Code Online (Sandbox Code Playgroud)
我在视图的背景中使用此xml.
我必须以编程方式创建三角形.所以需要以编程方式创建RotationDrawable.
我在LinearLayouts背景中使用ShapeDrawable.形状由cod制作,因为我需要动态地为它们分配颜色,具体取决于条件.这是我的自定义形状
public class CustomShapeDrawable extends ShapeDrawable {
private final Paint fillpaint, strokepaint, linePaint = new Paint();
private int strokeWidth = 3;
private final boolean disableBottomBorder;
public CustomShapeDrawable(Shape s, int fill, int stroke, int strokewidth, boolean disablebottomborder) {
super(s);
fillpaint = new Paint(this.getPaint());
fillpaint.setColor(fill);
strokepaint = new Paint(fillpaint);
strokepaint.setStyle(Paint.Style.STROKE);
strokepaint.setStrokeWidth(strokewidth);
strokepaint.setColor(stroke);
linePaint.setStyle(Paint.Style.STROKE);
linePaint.setStrokeWidth(strokewidth + 1);
linePaint.setColor(fill);
strokeWidth = strokewidth;
disableBottomBorder = disablebottomborder;
}
public CustomShapeDrawable(Shape s, int fill, int stroke, boolean disablebottomborder) {
super(s);
fillpaint = new Paint(this.getPaint());
fillpaint.setColor(fill);
strokepaint = …Run Code Online (Sandbox Code Playgroud) android ×10
shapedrawable ×10
xml-drawable ×4
background ×2
drawable ×2
java ×2
border ×1
colors ×1
styles ×1