android-drawable 相关问题

在Android中,Drawable是“可以绘制的东西”的一般抽象。

来自 JSON 的 Android 可绘制数组

在 JSON 文件中,我有数据和可绘制字符串: {"title": "title1","thumb": "R.drawable.image1"},{"title": "title2","thumb&...

回答 1 投票 0

我们可以在Android中创建自定义TextView吗

我刚刚开始学习Android开发,想知道有没有一种方法可以让TextViews更有创意。就像我们可以应用渐变滤镜或为其添加阴影一样。 我尝试使用一些背景...

回答 1 投票 0

当我点击 kotlin(android 开发)上的提交按钮并且我使用 sqlite 作为后端时,我的应用程序就崩溃了

所以我正在开发应用程序,我制作了一个简单的应用程序,您可以在其中输入电子邮件和密码,并验证用户是否存在,但只要我单击提交按钮...

回答 1 投票 0

如何将“shape”类重载到xml中?

我有这个 xml 可绘制的。但不幸的是,由于我的形状类“ExtOvalShape”,加载此 xml 时发生崩溃: 我有这个 xml 可绘制的。但不幸的是,由于我的形状类“ExtOvalShape”,加载此 xml 时发生崩溃: <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item> <com.xxx.ui.widget.drawable.shapes.ExtOvalShape android:state_selected="true"> <solid android:color="@color/theme_accent_1" /> <stroke android:width="1dp" android:color="@color/background_4" /> <size android:height="@dimen/part_number_value" android:width="@dimen/part_number_value" /> </com.xxx.ui.widget.drawable.shapes.ExtOvalShape> <com.xxx.ui.widget.drawable.shapes.ExtOvalShape android:state_selected="false"> <solid android:color="@android:color/transparent" /> <stroke android:width="1dp" android:color="@color/background_4" /> <size android:height="@dimen/part_number_value" android:width="@dimen/part_number_value" /> </com.xxx.ui.widget.drawable.shapes.ExtOvalShape> </item> </selector> 如何将形状重载到可绘制的 xml 中? 有关我的班级的信息(只是为了测试): public class ExtOvalShape extends OvalShape { @Override public void draw(Canvas canvas, Paint paint) { super.draw(canvas, paint); } } 据我所知,不可能使用自定义 XML 元素或以任何方式扩展 Android 可绘制文件中的现有元素。您必须坚持使用 <shape> 标签。 您可以做的是以编程方式应用您的自定义形状,例如: ExtOvalDrawable ovalDrawable = new ExtOvalDrawable(); view.setBackground(ovalDrawable);

回答 1 投票 0

带有自定义拇指的 Android Studio 范围滑块

我的 android 项目中有一个 RangeSlider,我想用我的一个可绘制对象来改变我的拇指。当我在 xml 代码中使用eekbar 尝试它时,它有效 -> android:thumb="@drawable/my_drawabl...

回答 1 投票 0

如何在 Compose 中使用动画矢量绘图

我创建了矢量可绘制动画,我想在 Compose 中使用。我在官方文档中发现我需要调用animatedVectorResource。但整个 AnimatedImageVector 已被删除...

回答 3 投票 0

如何使用XML绘制圆角矩形?

这里是绘制具有四个圆角的矩形的标记片段: 这是用于绘制具有四个圆角的矩形的标记片段: <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="#fff"></solid> <padding android:bottom="5dp" android:left="-1dp" android:right="-1dp" android:top="5dp"></padding> <corners android:radius="2dp"></corners> </shape> 但是如果我只想将一侧(两个角)的角变圆,我该怎么做? 谢谢。 <corners android:bottomLeftRadius="2dp" android:bottomRightRadius="2dp" android:topLeftRadius="2dp" android:topRightRadius="2dp" /> 创建可绘制资源:- <?xml version="1.0" encoding="utf-8"?> <!-- res/drawable/rounded_edittext.xml --> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" android:padding="10dp"> <solid android:color="#FFFFFF"/> <corners android:bottomRightRadius="15dp" android:bottomLeftRadius="15dp" android:topLeftRadius="15dp" android:topRightRadius="15dp"/> </shape> 设置在任何视图的背景上方。 按照您的要求这样做。 <corners android:bottomLeftRadius="2dp" android:bottomRightRadius="2dp" android:topLeftRadius="0dp" android:topRightRadius="0dp" /> 或 <corners android:bottomLeftRadius="0dp" android:bottomRightRadius="0dp" android:topLeftRadius="2dp" android:topRightRadius="2dp" /> 或任何其他显示您运行时间的效果。可以在 xml 文件中将其显示出来。 形状 xmlns:android="http://schemas.android.com/apk/res/android" 机器人:形状=“矩形”> <solid android:color="#FF0000" /> <corners android:radius="16dp" />

回答 4 投票 0

Android 渐变形状边框

我想为线性布局创建边框。所以我决定创建一个形状。我希望边框有渐变。下面就不做了。它填充矩形,然后创建描边。我...

回答 6 投票 0

以编程方式在 TextView 中设置左侧可绘制对象

我这里有一个xml中的textView。 我这里有一个xml中的textView。 <TextView android:id="@+id/bookTitle" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:drawableLeft="@drawable/checkmark" android:gravity="center_vertical" android:textStyle="bold" android:textSize="24dip" android:maxLines="1" android:ellipsize="end"/> 如你所见,我在 xml 中设置了 DrawableLeft。 我想更改代码中的可绘制对象。 有办法去做这件事吗?或者在代码中为文本视图设置drawableLeft? 您可以使用 setCompoundDrawablesWithIntrinsicBounds(int left, int top, int right, int Bottom) 在不需要图像的地方设置 0 左侧可绘制示例: TextView textView = (TextView) findViewById(R.id.myTxtView); textView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.icon, 0, 0, 0); 或者,您可以使用 setCompoundDrawablesRelativeWithIntrinsicBounds 来尊重 RTL/LTR 布局。 提示:每当您知道任何 XML 属性但不知道如何在运行时使用它时。只需转到开发人员文档中对该属性的描述即可。如果运行时支持,您将在那里找到相关方法。即 对于 DrawableLeft 使用 Kotlin: 您可以创建扩展函数或直接使用setCompoundDrawablesWithIntrinsicBounds。 fun TextView.leftDrawable(@DrawableRes id: Int = 0) { this.setCompoundDrawablesWithIntrinsicBounds(id, 0, 0, 0) } 如果需要调整drawable的大小,可以使用此扩展功能。 textView.leftDrawable(R.drawable.my_icon, R.dimen.icon_size) fun TextView.leftDrawable(@DrawableRes id: Int = 0, @DimenRes sizeRes: Int) { val drawable = ContextCompat.getDrawable(context, id) val size = resources.getDimensionPixelSize(sizeRes) drawable?.setBounds(0, 0, size, size) this.setCompoundDrawables(drawable, null, null, null) } 要变得真正奇特,请创建一个允许修改大小和/或颜色的包装器。 textView.leftDrawable(R.drawable.my_icon, colorRes = R.color.white) fun TextView.leftDrawable(@DrawableRes id: Int = 0, @DimenRes sizeRes: Int = 0, @ColorInt color: Int = 0, @ColorRes colorRes: Int = 0) { val drawable = drawable(id) if (sizeRes != 0) { val size = resources.getDimensionPixelSize(sizeRes) drawable?.setBounds(0, 0, size, size) } if (color != 0) { drawable?.setColorFilter(color, PorterDuff.Mode.SRC_ATOP) } else if (colorRes != 0) { val colorInt = ContextCompat.getColor(context, colorRes) drawable?.setColorFilter(colorInt, PorterDuff.Mode.SRC_ATOP) } this.setCompoundDrawables(drawable, null, null, null) } 从这里我看到方法setCompoundDrawablesWithIntrinsicBounds(int,int,int,int)可以用来做到这一点。 您可以使用以下任意一种方法在 TextView 上设置 Drawable: 1- setCompoundDrawablesWithIntrinsicBounds(int,int,int,int) 2- setCompoundDrawables(Left_Drawable、Top_Drawable、Right_Drawable、Bottom_Drawable) 要从您可以使用的资源中获取可绘制对象: getResources().getDrawable(R.drawable.your_drawable_id); Kotlin 扩展 + 可绘制周围的一些填充 fun TextView.addLeftDrawable(drawable: Int, padding: Int = 32) { val imgDrawable = ContextCompat.getDrawable(context, drawable) compoundDrawablePadding = padding setCompoundDrawablesWithIntrinsicBounds(imgDrawable, null, null, null) } 有两种方法可以做到这一点,您可以使用 XML 或 Java。 如果它是静态的并且不需要更改,那么您可以在 XML 中进行初始化。 android:drawableLeft="@drawable/cloud_up" android:drawablePadding="5sp" 现在如果您需要动态更改图标,那么您可以通过 根据事件调用图标 textViewContext.setText("File Uploaded"); textViewContext.setCompoundDrawablesWithIntrinsicBounds(R.drawable.uploaded, 0, 0, 0); 适用于我更改文本视图左/右可绘制颜色 for (drawable in binding.tvBloodPressure.compoundDrawablesRelative) { if (drawable != null) { drawable.colorFilter = PorterDuffColorFilter( ContextCompat.getColor(binding.tvBloodPressure.context, color), PorterDuff.Mode.SRC_IN ) } } 我就是这样用的。 txtUserName.setCompoundDrawablesWithIntrinsicBounds( requireActivity().getDrawable( R.drawable.ic_my_account ), null, null, null ) 上、下、左、右为图标资源。 TextView textView = (TextView) findViewById(R.id.your_id); textView.setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom); // for example personalTextView = (TextView) findViewById(R.id.personal_info); personalTextView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.personal, 0, 0, 0); 在 Android 中,您可以使用 setCompoundDrawablesRelativeWithIntrinsicBounds 方法以编程方式设置 TextView 的左侧可绘制对象(也称为复合可绘制对象)。这是 Java 中的示例: import android.graphics.drawable.Drawable; import android.os.Bundle; import android.widget.TextView; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Assume you have a TextView with the id "textView" TextView textView = findViewById(R.id.textView); // Set the left drawable programmatically Drawable drawable = getResources().getDrawable(R.drawable.your_drawable); // Replace with your drawable resource setLeftDrawable(textView, drawable); } private void setLeftDrawable(TextView textView, Drawable drawable) { // Set the left drawable with intrinsic bounds textView.setCompoundDrawablesRelativeWithIntrinsicBounds(drawable, null, null, null); } } 将 R.drawable.your_drawable 替换为您的可绘制对象的实际资源 ID。此示例假设您的布局 XML 文件中有一个 ID 为 textView 的 TextView。 请记住处理可绘制对象或 TextView 可能为 null 的情况,具体取决于您的应用程序逻辑。 static private Drawable **scaleDrawable**(Drawable drawable, int width, int height) { int wi = drawable.getIntrinsicWidth(); int hi = drawable.getIntrinsicHeight(); int dimDiff = Math.abs(wi - width) - Math.abs(hi - height); float scale = (dimDiff > 0) ? width / (float)wi : height / (float)hi; Rect bounds = new Rect(0, 0, (int)(scale * wi), (int)(scale * hi)); drawable.setBounds(bounds); return drawable; }

回答 11 投票 0

自 Android Studio 4.1 起,Android 后台 Drawable 无法在按钮中工作

我发现从 Android Studio 4.1 开始,我无法通过在其 android:background 上设置颜色来更改 Button 的背景颜色,只是没有效果。并且自定义 Drawable 也无法正常工作。 我的

回答 6 投票 0

Android (Kotlin):更改函数中 ImageButton 的背景

问题:如何将 ImageButton 声明为公共,就像在 Java 中一样: 公共类 MainActivity 扩展 AppCompatActivity { 图像按钮 b1; 问题: 此代码按预期工作: 覆盖 Cr 上的乐趣...

回答 1 投票 0

连续多次调用getDrawable,drawable是否被缓存和优化?

简单的问题,我想标题已经说明了一切!我想知道是否可以多次调用同一个 getDrawable,或者我应该调用它一次并存储在惰性 val 中并重用它?

回答 1 投票 0

如何在jetpack compose中将drawable添加到文本字段旁边?

我试图弄清楚如何将可绘制对象添加到文本字段(OutlinedTextField)内的文本,但没有成功。我已经在drawable文件夹中创建了一个drawable。我需要创建...

回答 2 投票 0

无法更改按钮的背景颜色和轮廓颜色

<Button android:id="@+id/signInBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="68dp" android:background="@drawable/gradient" android:drawableEnd="@drawable/rounded_arrow_forward_ios_24" android:fontFamily="@font/alata" android:paddingVertical="10dp" android:text="@string/sign_in" android:textFontWeight="600" android:textSize="16sp" app:iconPadding="140dp" app:iconSize="25dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/tagline" /> 我的 XML 布局文件中有两个按钮,一个用于登录,另一个用于注册。登录按钮具有渐变背景,但登录按钮的背景仍保持默认的蓝色。我正在尝试更改注册按钮的轮廓颜色,但尝试使用 background Tint 或 foreground Tint 更改整个背景颜色。我已经尝试过清除缓存并重新启动,但问题仍然存在。 如何正确更改注册按钮的轮廓颜色而不影响整个背景和登录按钮的背景? <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <gradient android:startColor="@color/orange" android:endColor="@color/lightBlue" android:angle="270"/> <corners android:radius="40dp"/> </shape> <Button android:id="@+id/signUpBtn" style="?attr/materialButtonOutlinedStyle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:drawableEnd="@drawable/rounded_arrow_forward_ios_24" android:fontFamily="@font/alata" android:paddingVertical="10dp" android:text="@string/sign_up" android:textColor="@color/white" android:textFontWeight="600" android:textSize="16sp" app:iconPadding="140dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/signInBtn" /> 要更改注册按钮的轮廓颜色而不影响整个背景,请使用“android:backgroundTint”属性。 将以下行添加到注册按钮 XML android:backgroundTint="@color/DesiredOutlineColor" 将您想要的颜色添加到上面的行。

回答 1 投票 0

是否可以将每个子视图的duplicateParentState设置为true?

背景 我有一个具有一些特殊状态(例如被选中/按下)的布局,并且我希望将其子项设置为基于此布局应用自己的可绘制对象。 我正在寻找替代品...

回答 1 投票 0

从状态列表drawable中获取特定的drawable

我有一个状态列表可绘制对象,我想从状态列表可绘制对象中获取特定的可绘制对象: 我有一个状态列表可绘制对象,我想从状态列表可绘制对象中获取特定的可绘制对象: <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" xmlns:kplus="http://schemas.android.com/apk/res-auto"> <item kplus:key_type_space_alt="true" android:state_pressed="true" android:drawable="@drawable/space_1_pressed" /> <item kplus:key_type_space_alt="true" android:drawable="@drawable/space_1_normal" /> <!-- TopNav keys. --> <item kplus:key_type_topnav="true" android:state_pressed="true" android:drawable="@drawable/tab_down" /> <item kplus:key_type_topnav="true" android:state_selected="true" android:drawable="@drawable/tab_down" /> <item kplus:key_type_topnav="true" android:drawable="@drawable/tab_normal" /> <!-- TopRow keys. --> <item kplus:key_type_toprow="true" android:state_pressed="true" android:drawable="@drawable/numeric_presseed" /> <item kplus:key_type_toprow="true" android:drawable="@drawable/numeric_normal" /> </selector> 我为每个键选择正确的可绘制状态,如下所示: if (keyIsNumbers) { if (KPlusInputMethodService.sNumbersState == 2) { drawableState = mDrawableStatesProvider.KEY_STATE_TOPNAV_CHECKED; } } 现在状态定义如下: KEY_STATE_TOPNAV_NORMAL = new int[] {keyTypeTopNavAttrId}; KEY_STATE_TOPNAV_PRESSED = new int[] {keyTypeTopNavAttrId, android.R.attr.state_pressed}; KEY_STATE_TOPNAV_CHECKED = new int[] {keyTypeTopNavAttrId, android.R.attr.state_selected}; 现在我的问题是如何为每个状态提取正确的可绘制对象?我需要获取可绘制对象的 9patch 填充,因为如果状态在 9patch 上有不同的填充,它将仅获得顶部可绘制对象的填充,并且我想为每个键手动设置填充(drawable.getPadding(rect)) . 过时的答案。 请检查 VinceStyling 的答案,以获得使用 SDK 29+ 的更好解决方案 没有没有公共API可以从状态获取可绘制对象。 StateListDrawable中有一些方法,但它们是@hide,带有注释“待定的API委员会”。 您可以通过反射来调用它们...但是您需要自行承担风险!!!。 (在未来的版本中可能会改变) 这些方法是: 获取StateDrawableIndex 获取状态可绘制 以下是如何进行(省略例外): int[] currentState = view.getDrawableState(); StateListDrawable stateListDrawable = (StateListDrawable)view.getBackground(); Method getStateDrawableIndex = StateListDrawable.class.getMethod("getStateDrawableIndex", int[].class); Method getStateDrawable = StateListDrawable.class.getMethod("getStateDrawable", int.class); int index = (int) getStateDrawableIndex.invoke(stateListDrawable,currentState); Drawable drawable = (Drawable) getStateDrawable.invoke(stateListDrawable,index); 现在这是无需反射即可获取特定可绘制对象的方法: StateListDrawable pressedDrawable = (StateListDrawable) this.mPressed; final int state = android.R.attr.state_pressed; final int index = pressedDrawable.findStateDrawableIndex(new int[]{state}); if (index >= 0) { Drawable drawable = pressedDrawable.getStateDrawable(index); if (drawable instanceof GradientDrawable) { ((GradientDrawable) drawable).setCornerRadius(mRoundConerRadius); } }

回答 2 投票 0

微光未显示在可塑形图像视图上

我在我的应用程序中使用了可变形图像视图。我想加载微光作为占位符,直到图像加载到可成形图像视图中。但微光未加载。它只显示空白图像...

回答 1 投票 0

这是 Android 文档中有关环形的错误吗?

我画了一个环形,这是: 我画了一个环形,这是: <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="ring" android:useLevel="false"> <solid android:color="@android:color/holo_blue_light" /> <size android:width="200dp" android:height="200dp" /> </shape> 我没有指定 innerRadiusRatio 或 thicknessRatio 属性,因此,根据文档(对于 innerRadiusRatio,对于 thicknessRatio),它们将分别为 9 和 3。 但是,当我以编程方式检索这些值时,它们似乎是相反的: val ring = ContextCompat.getDrawable(this, R.drawable.ring) as GradientDrawable Log.i(TAG, "thicknessRatio = ${ring.thicknessRatio}") Log.i(TAG, "innerRadiusRatio = ${ring.innerRadiusRatio}") 输出为: 厚度比 = 9.0 内半径比率 = 3.0 除了这里的错误之外我想不出任何东西。 也许我错过了什么? 由此产生的环显然其厚度似乎小于其内半径: 看起来像是。 由此产生的环显然其厚度似乎小于 它的内半径 那是因为这些值是宽度的约数: float thickness = st.mThickness != -1 ? st.mThickness : bounds.width() / st.mThicknessRatio; // inner radius float radius = st.mInnerRadius != -1 ? st.mInnerRadius : bounds.width() / st.mInnerRadiusRatio; 通过问题中的链接提供的文档也证实了这一点,除了默认值,这是不正确的: 内半径等于环的宽度除以 9 厚度等于环的宽度除以3

回答 1 投票 0

ShapeDrawable 椭圆形(描边样式)的边缘被剪切(Android Studio)

我是 Android Studio 的新手,目前我正在尝试以编程方式创建一个随机颜色、描边宽度和大小的椭圆形(不是实心,只是描边): 长 randomSeedm = 系统。

回答 2 投票 0

Android:如何将drawable转换为int?

我使用以下代码获取了一些已安装应用程序的图标: 可绘制图标 = getPackageManager().getApplicationIcon("com.example.example"); imageView.setImageDrawable(图标); 那我想...

回答 3 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.