android-drawable 相关问题

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

将 Android 资源可绘制资源转换为位图时出现问题

我有一个 Android 资源可绘制对象,我想在对绘制对象的 BitmapShader 调用中使用它。 我在尝试将此 Android 可绘制对象转换为位图时不断遇到问题。 我努力了: 可绘制

回答 1 投票 0

致命异常:android.content.res.Resources$NotFoundException:Drawable org.radarcns.detail:drawable/status_searching,资源 ID #0x7f080130

我收到异常: 致命异常:android.content.res.Resources$NotFoundException:Drawable org.radarcns.detail:drawable/status_searching,资源 ID #0x7f080130 安卓引起的。

回答 1 投票 0

为 ListView 最后一个元素添加下边距

我需要添加具有复杂项目背景的ListView:顶部和底部的偶数/奇数和圆角不同。它看起来像这样: 我已经通过 level-l 实现了所有这些东西...

回答 7 投票 0

在 android Switch 上设置TrackDrawable 导致错误的图像

我的 Android 应用程序与服务器有永久的 WebSocket 连接。还有一个开关可以关闭该连接。所以现在开关有两种颜色。如果开关打开,则 WS 已连接...

回答 2 投票 0

如何在android中图像检查动画?

我想检查从Android Gallery收到的照片是动画还是只是照片。 我通过搜索找到了AnimationDrawable,发现它检查了,但是总是返回N。 前我...

回答 1 投票 0

如何更改 TextInputLayout android 中 setEndIconDrawable 的可见性(可见、不可见或消失)?

我正在尝试使 endIconDrawable 不可见或不可见,它是上图中仅用于 FirstName 字段的编辑图标,并且希望保留其余字段的可见编辑图标,但你...

回答 6 投票 0

如何在自定义视图中为 Drawable 制作动画?

我目前有一个带有可绘制资源的自定义视图,我想将其从一个位置移动到另一个位置。我知道有两种方法可以实现 PropertyAnimation,您可以使用

回答 3 投票 0

来自 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

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