首页 \ 问答 \ 是否有可能以编程方式创建drawable并将其分配给ImageView?(Is it possible to programmatically create a drawable and assign it to an ImageView?)

是否有可能以编程方式创建drawable并将其分配给ImageView?(Is it possible to programmatically create a drawable and assign it to an ImageView?)

所以我想以某种方式在代码中创建一个Path对象(无XML)并将其绘制到ImageView中。 问题是我找不出任何方式来以编程方式创建任何形状,并让它出现在我的ImageView中。 我可以轻松地将ImageView分配给XML资源,并且工作正常,如下所示:

imageView.setImageResource(R.drawable.resourcename);

这是XML文件:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <size
        android:height="50dp"
        android:width="50dp" />
    <solid
        android:color="#00f"
        />
</shape>

但是如果我做了一些像我认为应该工作的代码,它编译和运行没有错误,但没有任何反应。 我试图创建一个简单的RectShape并使用setImageDrawable将ImageView赋值给它。 我在这里完全咆哮错误的树吗? 我对Android编码非常陌生,所以我很可能会在这里离开。 我注意到,我从来没有手动绘制矩形,我必须在某处做什么? 我有点认为ImageView会照顾它,但也许不会。 让我知道你是否需要更多关于我的代码的信息。

RectShape rect = new RectShape();
rect.resize(50,50);
ShapeDrawable sdRect = new ShapeDrawable(rect);
sdRect.getPaint().setColor(Color.BLACK);
sdRect.getPaint().setStyle(Paint.Style.FILL);
imageView.setImageDrawable(sdRect);

So I'm trying to somehow create a Path object in code (no XML) and draw it into an ImageView. Problem is I can't figure out any way to programmatically create ANY shape and have it show up in my ImageView. I can easily assign the ImageView to an XML resource and that works fine like this:

imageView.setImageResource(R.drawable.resourcename);

Here's the XML file:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <size
        android:height="50dp"
        android:width="50dp" />
    <solid
        android:color="#00f"
        />
</shape>

But if I do something like this code below which I thought should work, it compiles and runs without errors, but nothing happens. I tried to create a simple RectShape and assign the ImageView to it with setImageDrawable. Am I totally barking up the wrong tree here? I am extremely new to Android coding fyi, so I very well may be way off here. I do notice that I'm never manually drawing the rectangle, do I have to do that somewhere? I sort of figured the ImageView would take care of it, but maybe not. Let me know if you need more information about my code.

RectShape rect = new RectShape();
rect.resize(50,50);
ShapeDrawable sdRect = new ShapeDrawable(rect);
sdRect.getPaint().setColor(Color.BLACK);
sdRect.getPaint().setStyle(Paint.Style.FILL);
imageView.setImageDrawable(sdRect);

原文:https://stackoverflow.com/questions/26453478
更新时间:2021-10-28 08:10

最满意答案

我将添加另一个答案,因为它涉及有关您的架构编译的不同主题。

我还注意到这两个简单类型实际上属于不同的命名空间。 所以它们实际上不应该首先在同一个包中生成。

我想你只需使用generatePackageorg.isan指定为目标包。 因此,所有命名空间最终都在一个包中,这非常糟糕。 如果每个命名空间有一个包,则JAXB最有效。 如果你不这样做会很奇怪。

所以我一般不鼓励使用generatePackage ,而是使用jaxb:package代替:

  <bindings schemaLocation="http://www.isan.org/schema/v1.11/common/country.xsd">
    <schemaBindings>
      <package name="org.isan.schema.v1_11.common.country"/>
    </schemaBindings>
  </bindings>

我还建议使用主要/次要架构版本的包名称。 您可能需要稍后并行支持多个模式版本。


I'll add another answer since it takes on a different topic concerning your schema compilation.

What I also noticed is that these two simple types actually belong to different namespaces. So they actually should not be generated in the same package in the first place.

I guess you just use generatePackage to specify org.isan as your target package. So all your namespaces end up in one package which is pretty bad. JAXB works best if you have one package per namespace. It will get weird if you don't.

So I generally discourage usage of generatePackage, use jaxb:package instead:

  <bindings schemaLocation="http://www.isan.org/schema/v1.11/common/country.xsd">
    <schemaBindings>
      <package name="org.isan.schema.v1_11.common.country"/>
    </schemaBindings>
  </bindings>

I'd also recommend using major/minor schema version the the package name. You may need to support several schema versions in parallel later on.

相关问答

更多
  • 我将添加另一个答案,因为它涉及有关您的架构编译的不同主题。 我还注意到这两个简单类型实际上属于不同的命名空间。 所以它们实际上不应该首先在同一个包中生成。 我想你只需使用generatePackage将org.isan指定为目标包。 因此,所有命名空间最终都在一个包中,这非常糟糕。 如果每个命名空间有一个包,则JAXB最有效。 如果你不这样做会很奇怪。 所以我一般不鼓励使用generatePackage ,而是使用jaxb:package代替:
    我在安装java时得到了它。 它现在可能在您的系统上。 查看java,javac等所在的目录。 I got it when I installed java. It is probably on your system right now. Look in the directory where java, javac, etc are located.
  • 我在JAXB maven build中修复了OSGi故障。 所以现在应该没问题。 此外,为了快速构建,您可以尝试使用: $ mvn clean install -Ddev 这将在不创建docs / osgi-tests的情况下运行快速构建。 您还可以在$JAXB_HOME/bundles/ri/target/找到遗留的jaxb-ri.zip (带bin / libs / examples /)。 或者,如果您不希望每次在构建之后将其解压缩,请直接转到: $JAXB_HOME/bundles/ri/targ ...
  • 使用jaxb:package自定义: ...
  • 错误消息已本地化,您可以更改语言的错误消息目录以满足您的需要。 或者,您可以修改一些报告错误的通行证,例如使用您的插件(甚至某些MELT扩展名)。 但是我很惊讶你要问,GCC源代码包含一个错误消息目录,在源代码的gcc / po /子目录中。 请注意,消息目录使用的是gettext(3)机制,因此消息ID实际上是英语(C语言环境)消息本身。 Error messages are localized, you could change the error message catalog for your la ...
  • 看来,jxb:factoryMethod只能用于xjc抱怨的元素。 在我的示例中,我必须通过添加与StepAction相同级别的其他元素来导致“编译器无法兑现此FactoryMethod自定义”: ...
  • 是的 ,XJC 可以使用插件进行扩展 : 插件可以做什么? JAXB RI插件参与模式的代码生成。 它可以定义用户可以用来控制它的自定义,它可以访问JAXB RI生成的代码,并且可以生成其他类/方法/字段/注释/注释。 插件也可以选择完全接管XJC的代码生成过程。 只有一个插件可以在任何给定时间执行此操作,但这允许插件从架构生成完全不同的代码。 另见这个答案: CodeOutline / JClass / CClass在CodeModel中的作用是什么? 但不 ,我不认为这是解决你所遇问题的正确方法。 尝试 ...
  • 我没有任何经验,但有Apache JaxMe和 EclipseLink MOXy 。 更新: 根据下面的Blaise Doughan,MOXy使用参考实现。 I've not had experience with either of these, but there is Apache JaxMe and EclipseLink MOXy . Update: According to Blaise Doughan below, MOXy uses the reference implementation. ...
  • 这是因为你有一个名为Managers的元素在你的序列中出现两次。 为了能够往返文档,JAXB需要将所有数据存储在有序List 。 你的用例 以下是您的用例的简化版本: ...
  • 你应该为你的complexType使用一个Hierarchy。 i:type="advice_instruction_adjust"意味着有一个名为advice_instruction_adjust 。

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)