如何解决如何更改或填充图层列表中形状的颜色
我尝试使用以下解决方案,但没有奏效
https://stackoverflow.com/questions/32163918/programmatically-change-color-of-shape-in-layer-list
这是我的代码...
Rolling.java
package com.example.app1;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.LayerDrawable;
import android.os.Bundle;
public class Rolling extends AppCompatActivity {
private LayerDrawable layerDrawable;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rolling);
LayerDrawable shape = (LayerDrawable) ContextCompat.getDrawable(Rolling.this,R.drawable.dice1);
/* GradientDrawable outer = (GradientDrawable) layerDrawable.findDrawableByLayerId(R.drawable.dice1);
outer.setColor(Color.BLACK);*/
shape.setTint(Color.BLUE);
}
}
activity_rolling.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
tools:context=".Rolling">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="44dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/dice1" />
</androidx.constraintlayout.widget.ConstraintLayout>
dice1.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingRight="50dp" android:paddingLeft="50dp">
<!-- Larger blue circle -->
<item
android:id="@+id/dice1"
android:bottom="214dp"
android:top="214dp"
android:left="100dp"
android:right="100dp"
android:gravity="center"
android:tileMode="repeat"
>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/listview_background_shape">
<stroke android:width="2dp" android:color="#ff207d94" />
<padding android:left="2dp"
android:top="2dp"
android:right="2dp"
android:bottom="2dp" />
<size android:width="100dp" android:height="100dp"/>
<corners android:radius="5dp" />
<solid android:color="#ffffffff" />
</shape>
</item>
</layer-list>
所以我想以编程方式更改方形的颜色那么我该如何实现呢?
解决方法
在 Rolling extends AppCompatActivity
中,您正在获得名为 LayerDrawable
的新 shape
,但这是此图像的“新实例”,您必须为您的 ImageView
设置它然后
ImageView iv = findViewById(R.id.imageView);
iv.setImageDrawable(shape);
资源是“不可触碰的”——如果您以编程方式获得一些可绘制对象并进行一些更改(例如设置滤色器或色调),这并不意味着资源/可绘制对象已更改。任何其他 getDrawable
或 app:srcCompat
将在没有您更改的情况下获取原始图像
我没有足够的声誉来发表评论,所以我会将此作为答案发布。 您可以尝试其中之一。
sf::Texture hero_texture;
hero_texture.loadFromFile("image/main.png");
sf::Sprite hero_sprite;
hero_sprite.setTexture(hero_texture);
或
shape.setColorFilter(Color.BLUE,PorterDuff.Mode.SRC_IN);
不要忘记使可绘制对象无效/刷新。
shape.setColorFilter(Color.BLUE,PorterDuff.Mode.DST_IN);
,
下面的代码,用于更改可绘制对象的纯色和描边颜色。
ImageView imageView = findViewById(R.id.imageView);
GradientDrawable imageViewDrawable = (GradientDrawable) imageView.getBackground();
imageViewDrawable.setStroke(<border_width_int>,<border_color>)
imageViewDrawable.setColor(<btn_color>)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。