我试图以编程方式更改selectable_kachel_shape的颜色.这是xml文件:
kachel_ticked_style.xml
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape
android:id="@+id/selectable_kachel_shape"
android:shape="rectangle" >
<stroke
android:width="5dp"
android:color="@color/headrbar_color" />
</shape>
</item>
<item>
<rotate
android:fromDegrees="45"
android:pivotX="120%"
android:pivotY="100%"
android:toDegrees="45" >
<shape android:shape="line" >
<stroke
android:width="40dp"
android:color="@color/headrbar_color" />
</shape>
</rotate>
</item>
<item
android:right="5dp"
android:top="5dp">
<bitmap
android:gravity="top|right"
android:src="@drawable/selectable_tiles_check" />
</item>
</layer-list>
Run Code Online (Sandbox Code Playgroud)
我在片段内调用以下代码
LayerDrawable layers = (LayerDrawable) this.getActivity().getResources().getDrawable(R.drawable.kachel_ticked_style);
GradientDrawable shape = (GradientDrawable) (layers.findDrawableByLayerId(R.id.selectable_kachel_shape));
shape.setColor(this.getActivity().getResources().getColor(android.R.color.background_dark);
Run Code Online (Sandbox Code Playgroud)
1.为什么我在shape.setColor中设置NullPointerException ?
2.如何以编程方式更改图层内部的形状内的颜色?
有没有办法LayerDrawable在API <23上添加图层?我是否必须LayerDrawable使用构造函数创建一个新的只添加新图层?
所以我有两个不同的Drawables需要合并并Drawable在运行时获得一个.我希望第一个Drawable位于顶部,另一个位于底部.我遇到了LayerDrawable它,看起来它正是我所需要的,但我在努力安排它时遇到了麻烦Drawables.
所以我有一个ImageButton48x48 dp,这是最终Drawable的结果.第一个Drawable是加号按钮(20x20 dp),第二个是dp加号按钮下方的小点(4x4 ).
加号按钮Drawable使用字体字形加载.我正在Drawable使用这个xml片段创建点按钮:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid
android:color="@color/white_40"/>
<size
android:width="4dp"
android:height="4dp"/>
</shape>
Run Code Online (Sandbox Code Playgroud)
我的第一种方法是只既能补充Drawables到LayerDrawable,但是当我这样做,宽度/高度在XML中指定的点被忽略的属性,并将其延伸到覆盖的加号图标.
LayerDrawable finalDrawable = new LayerDrawable(new Drawable[] {plusIcon, dotIcon});
Run Code Online (Sandbox Code Playgroud)
我尝试的第二种方法是setLayerInset尝试定位两者Drawables.
LayerDrawable finalDrawable = new LayerDrawable(new Drawable[] {plusIcon, dotIcon});
finalDrawable.setLayerInset(0, 0, 0, 0, 0);
finalDrawable.setLayerInset(1, dp(22), dp(44), dp(22), 0);
Run Code Online (Sandbox Code Playgroud)
上面的代码片段最终将点放在正确的位置,但它也开始影响加号按钮的位置和大小,最终看起来像这样: …
android drawable android-layout android-drawable layerdrawable
由于我的应用程序的颜色主题是动态的,我只能使用颜色和shaperawable创建背景绘图,我想构建一个可用颜色和形状绘制的edittext背景,如下所示.但我想以编程方式执行此操作
如何以编程方式构建同样的drawable?
<item>
<shape>
<solid android:color="@android:color/yellow" />
</shape>
</item>
<!-- main color -->
<item
android:bottom="1dp"
android:left="1dp"
android:right="1dp">
<shape>
<solid android:color="@android:color/white" />
</shape>
</item>
<!-- draw another block to cut-off the left and right bars -->
<item android:bottom="10dp">
<shape>
<solid android:color="@android:color/white" />
</shape>
</item>
Run Code Online (Sandbox Code Playgroud)
这就是我试过的......
GradientDrawable border = new GradientDrawable();
border.setShape(GradientDrawable.RECTANGLE);
border.setColor(Color.WHITE);
GradientDrawable background = new GradientDrawable();
background.setShape(GradientDrawable.RECTANGLE);
background.setColor(Color.YELLOW);
GradientDrawable clip = new GradientDrawable();
clip.setShape(GradientDrawable.RECTANGLE);
border.setColor(Color.WHITE);
Drawable[] layers = {background, border, clip};
LayerDrawable layerDrawable = new LayerDrawable(layers);
layerDrawable.setLayerInset(0, 0, 0, 0, …Run Code Online (Sandbox Code Playgroud) 我想以编程方式将颜色设置为进度条primaryProgress,secondaryProgress,因为颜色将根据屏幕的背景颜色进行更改.
LayerDrawable progressDrawable = (LayerDrawable) ProgressBar1.getProgressDrawable();
Drawable backgroundColor = progressDrawable.getDrawable(0);
Drawable secondaryColor = progressDrawable.getDrawable(1);
Drawable primaryColor = progressDrawable.getDrawable(2);
final float[] roundedCorners = new float[] { 5, 5, 5, 5, 5, 5, 5, 5 };
primaryColor = new ShapeDrawable(new RoundRectShape(roundedCorners, null, null));
secondaryColor = new ShapeDrawable(new RoundRectShape(roundedCorners, null, null));
primaryColor.setColor((Color.rgb(color_normal[0], color_normal[1], color_normal[2])), null);
secondaryColor.setColor((Color.rgb(color_normal[0], color_normal[1], color_normal[2])), null);
progressDrawable.setDrawableByLayerId(progressDrawable.getId(2), new ClipDrawable(primaryColor, Gravity.LEFT, ClipDrawable.HORIZONTAL));
...
Run Code Online (Sandbox Code Playgroud)
**此处的颜色代码仅供测试.之后,颜色代码将被引用到其他部分以进行相应的更新
secondaryColor.setColorFilter((Color.rgb(255, 0, 0)), PorterDuff.Mode.SRC_OVER);
primaryColor.setColorFilter((Color.rgb(0, 255, 213)), PorterDuff.Mode.SRC_OVER);
progressDrawable.setDrawableByLayerId(progressDrawable.getId(2), new ClipDrawable(primaryColor, Gravity.LEFT, ClipDrawable.HORIZONTAL));
progressDrawable.setDrawableByLayerId(progressDrawable.getId(1), new …Run Code Online (Sandbox Code Playgroud) 我正在创建一个LayerDrawable创建底部笔划,但我不知道如何给出图层的下边距(Drawablw).
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:bottom="2dp">
..
</item>
</layer-list>
Run Code Online (Sandbox Code Playgroud)
我想以编程方式设置上面的底部边距.
到目前为止,我已经这样做了:
Drawable[] list = new Drawable[2];
GradientDrawable strokeDrawable = new GradientDrawable(
GradientDrawable.Orientation.TOP_BOTTOM, new int[] {
strokeColor[0], strokeColor[0] });
GradientDrawable backgroundDrawable = new GradientDrawable(
GradientDrawable.Orientation.TOP_BOTTOM, bgColor);
// Now how to set bottom margin to make border.
list[0] = strokeDrawable;
list[1] = backgroundDrawable;
LayerDrawable layerDrawable = new LayerDrawable(list);
Run Code Online (Sandbox Code Playgroud)
有人知道吗?
错误:无效的可绘制对象已添加到LayerDrawable中!Drawable已经属于另一个所有者,但是没有公开恒定状态。
我今天突然注意到了这个错误,我不确定是否是因为我刚刚将测试设备更新为Android 8.0。该错误消息清楚地表明,在浮动操作按钮上设置波纹效果有问题,并且在按下按钮时确实没有波纹效果。但是,我不确定是什么导致了此问题。实际上,相同的错误连续两次抛出。任何帮助将非常感激!该应用程序的其余部分仍然可以正常运行,但该错误确实困扰着我。
ps minSdkVersion为22,targetSdkVersion和compiledSdkVersion为27
在MyActivity中,第117行是数据绑定和设置内容视图。
ActivityMyBinding binding = DataBindingUtil.setContentView(
this, R.layout.activity_my);
Run Code Online (Sandbox Code Playgroud)
这是完整的堆栈跟踪:
W/LayerDrawable: Invalid drawable added to LayerDrawable! Drawable already belongs to another owner but does not expose a constant state.
java.lang.RuntimeException
at android.graphics.drawable.LayerDrawable$ChildDrawable.<init>(LayerDrawable.java:1855)
at android.graphics.drawable.LayerDrawable$LayerState.<init>(LayerDrawable.java:1975)
at android.graphics.drawable.LayerDrawable.createConstantState(LayerDrawable.java:168)
at android.graphics.drawable.LayerDrawable.mutate(LayerDrawable.java:1779)
at android.graphics.drawable.LayerDrawable.mutate(LayerDrawable.java:1785)
at android.graphics.drawable.RippleDrawable.mutate(RippleDrawable.java:997)
at android.view.View.applyBackgroundTint(View.java:21809)
at android.view.View.setBackgroundDrawable(View.java:21680)
at android.support.design.widget.FloatingActionButton.access$001(FloatingActionButton.java:68)
at android.support.design.widget.FloatingActionButton$ShadowDelegateImpl.setBackgroundDrawable(FloatingActionButton.java:824)
at android.support.design.widget.FloatingActionButtonLollipop.setBackgroundDrawable(FloatingActionButtonLollipop.java:73)
at android.support.design.widget.FloatingActionButton.<init>(FloatingActionButton.java:179)
at android.support.design.widget.FloatingActionButton.<init>(FloatingActionButton.java:151)
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:334)
at android.view.LayoutInflater.createView(LayoutInflater.java:647)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:790)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374) …Run Code Online (Sandbox Code Playgroud) 我有一个自定义按钮,并通过编程方式更改其按下和默认颜色.
public class CustomApplicationButton extends Button {
public CustomApplicationButton(Context context) {
this(context, 0, 0, 0);
}
public CustomApplicationButton(Context context, int topDrawableResId, int outlineDefaultColorId, int outlinePressedColorId) {
super(context);
// set width and height
LinearLayout.LayoutParams params = new LayoutParams(
context.getResources().getDimensionPixelSize(R.dimen.sr_application_button_width),
context.getResources().getDimensionPixelSize(R.dimen.sr_application_button_height));
setLayoutParams(params);
// set drawable top icon
if (topDrawableResId != 0) {
setCompoundDrawablesWithIntrinsicBounds(0, topDrawableResId, 0, 0);
}
// set background and outline color
int strokeWidth = context.getResources().getDimensionPixelSize(R.dimen.sr_launcher_button_stroke_size);
// unpressed state drawable
LayerDrawable defaultLayers = (LayerDrawable) context.getResources().getDrawable(
R.drawable.btn_launcher_shape_default);
GradientDrawable defaultShapeOutline = (GradientDrawable) defaultLayers.findDrawableByLayerId(R.id.outline_default); …Run Code Online (Sandbox Code Playgroud) 根据某些条件,我必须对角剪切列表单元格。为此,我使用以下代码制作了对角线可绘制图像:
对角线.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:top="0dp"
android:bottom="0dp"
>
<rotate
android:fromDegrees="315"
android:toDegrees="315"
android:pivotX="0%"
android:pivotY="0%" >
<shape
android:shape="line"
>
<stroke
android:width="10dp"
android:color="@color/grey" />
</shape>
</rotate>
</item>
</layer-list>
Run Code Online (Sandbox Code Playgroud)
在列表单元格的 xml 中,它用作:
<ImageView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="@drawable/diagonal_line"
android:layerType="software"
android:scaleType="fitXY"
/>
Run Code Online (Sandbox Code Playgroud)
该对角线出现在单元格 xml 的图形视图中,但在列表膨胀后不会出现。目前,它的可见性与任何条件无关,即可见性始终为真。
知道问题出在哪里吗?
我想要一个填充圆圈,具有一定颜色和宽度的笔划,以及内部的图像.
这可以很容易地用XML完成(这只是一个示例):
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<size
android:width="120dp" android:height="120dp"/>
<solid android:color="#ffff0000"/>
<stroke
android:width="3dp" android:color="#ff00ff00"/>
</shape>
</item>
<item
android:width="60dp" android:height="60dp" android:drawable="@android:drawable/btn_star"
android:gravity="center"/>
</layer-list>
Run Code Online (Sandbox Code Playgroud)
我需要具有以上某些属性以编程方式进行更改,因此我无法使用XML文件,但这个想法仍然是相同的.
事实是,我无法找到一种简单的方法来在OvalShape drawable上绘制,就像在XML中一样.我找不到这样做的功能.
StackOverflow上有解决方案,但我找不到一个运行良好的解决方案.我发现只有一个在这里,但它的划线正在削减.
但是,我通过一种方式来解决这个问题,通过使用一个仅用于描述本身的XML:
stroke_drawable.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
<stroke
android:width="4dp" android:color="@android:color/white"/>
</shape>
Run Code Online (Sandbox Code Playgroud)
码:
final int strokeDrawableResId = R.drawable.stroke_drawable;
Drawable innerDrawable = ResourcesCompat.getDrawable(getResources(), ..., null);
final Drawable strokeDrawable = ResourcesCompat.getDrawable(getResources(), strokeDrawableResId, null);
ShapeDrawable biggerCircle = new ShapeDrawable(new OvalShape());
int size = ...;
biggerCircle.setIntrinsicHeight(size);
biggerCircle.setIntrinsicWidth(size);
biggerCircle.getPaint().setColor(0xffff0000);
biggerCircle.setBounds(new Rect(0, 0, size, size));
LayerDrawable layerDrawable …Run Code Online (Sandbox Code Playgroud) android android-drawable layerdrawable android-shapedrawable
我扩展了LayerDrawable以在按下ImageButtons时自动更改Alpha值:
protected class CustomDrawable extends LayerDrawable {
protected int _pressedAlpha = 60;
public CustomDrawable(Drawable d) {
Log.d(TAG, "constructor");
super(new Drawable[] { d });
}
/**
* Wenn sich der Status auf pressed ändert, soll der Alphawert sich auf _pressedAlpha ändern. Wenn sich der
* Status von pressed wieder auf normal ändert soll der ursprüngliche Alphawert gesetzt werden
*/
@Override
protected boolean onStateChange(int[] states) {
Log.d(TAG, "status changed: " + states);
boolean pressed = false;
for (int state : states) { …Run Code Online (Sandbox Code Playgroud) 这是三角形的xml:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/shape_id">
<rotate
android:fromDegrees="45"
android:toDegrees="45"
android:pivotX="-40%"
android:pivotY="87%" >
<shape android:shape="rectangle" >
<stroke android:width="10dp"/>
</shape>
</rotate>
</item>
</layer-list>
Run Code Online (Sandbox Code Playgroud)
这是textview的背景
<TextView
android:id="@+id/headlineSelect_TXT2"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_weight="1"
android:background="@drawable/category_triangle_shape1"
android:visibility="invisible" />
Run Code Online (Sandbox Code Playgroud)
我想以编程方式改变形状的颜色.我试过这个,但我得到空指针异常
LayerDrawable bgDrawable = (LayerDrawable) getActivity()
.getResources()
.getDrawable(R.drawable.category_triangle_shape1);
final GradientDrawable shape = (GradientDrawable) bgDrawable
.findDrawableByLayerId(R.id.shape_id);
shape.setStroke(10,Color.GREEN);
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?感谢帮助.
我有一个RatingBar:
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleX="0.75"
android:isIndicator="false"
android:scaleY="0.75"
android:id="@+id/ratingBar"
android:stepSize="0.5"
android:numStars="5" />
Run Code Online (Sandbox Code Playgroud)
而我正在使用滤色器使评级栏中的星星呈粉红色,如下所示:
ratingBar = (RatingBar) findViewById(R.id.ratingBar);
Drawable progressDrawable = ratingBar.getProgressDrawable();
if (progressDrawable instanceof LayerDrawable) {
LayerDrawable stars = (LayerDrawable) progressDrawable;
stars.getDrawable(2).setColorFilter(getResources().getColor(R.color.ColorSecondary), PorterDuff.Mode.SRC_ATOP);
stars.getDrawable(1).setColorFilter(getResources().getColor(R.color.ColorSecondary), PorterDuff.Mode.SRC_ATOP);
stars.getDrawable(0).setColorFilter(getResources().getColor(R.color.ColorSecondary), PorterDuff.Mode.SRC_ATOP);
}
Run Code Online (Sandbox Code Playgroud)
这在除Nexus 5(Android版本6.0)之外的所有手机中都可以正常工作,其中5颗星都是粉红色,但默认情况下填充.即使我点击星星,它们也不会改变颜色,所有5个都会被填满.
但是当我这样做时ratingBar.getRating(),它会返回我的用户触摸评级栏的位置的评级,这意味着它正在工作,只是滤色器出现故障.
如果我删除滤色器,RatingBar使用默认颜色可以正常工作.
似乎无法在任何地方找到解决方案.提前致谢.
layerdrawable ×13
android ×12
drawable ×3
layer-list ×3
xml ×2
alpha ×1
colorfilter ×1
colors ×1
diagonal ×1
imagebutton ×1
shape ×1
stroke ×1