问题标签 [android-custom-view]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
1892 浏览

android - Android Custom Compoent extending FrameLayout in Code

I would like to create an Android custom component for a game. The basic idea is that the custom component would be square in shape and contain other square ImageView components inside it. The ImageView components inside may be scaled and/or cropped as needed, but must remain square. The number of rows == columns, always. The difficulty level determines the exact value of rows and columns at runtime. As the game gets more difficult, the number of rows and columns will increase equally, but the scaled size of each ImageView will shrink accordingly. I would like the components to have some fixed small spacing between them, mostly so it looks nice. The display won't be scrollable, all items must fit on the display at the same time.

After a bunch of failed attempts, I'm getting frustrated and I'm not sure what is the best approach is to make this custom component.

My latest idea would be to do roughly the following:

I create my custom component class by "extends FrameLayout". In my constructor, I create an arbitrary small square ImageView object in code. I'd like others to reuse my custom component someday, so I do it in code rather than XML so that the custom component doesn't rely on any external XML drawable having to be defined. I set the scale type so that the ImageView retains it's "squareness" (I think I want fitStart?). I set the square ImageView's layout_width and layout_height to "fill_parent", so the ImageView fills in as big a space as is available in the containing FrameLayout. I add the now large and square ImageView to my layout using addView().

Next I create a RelativeLayout object and also programmatically add it to myself using addView(). Because my base class is a FrameLayout, I think this will make the size of the RelativeLayout the same as the square ImageView previously added. At least that's how I read the documentation.

Next I measure the ImageView to see how large it is. I don't think I can measure myself because my previous attempts failed at this. I kept getting 0 for my size. My research said this was because I can't measure myself during construction. This turned into a Catch-22. When I tried to scale my image to the available space, I couldn't because there was no space available yet. My hope is that because I changed to using the FrameLayout as my base class and I add the ImageView to it, I will be able to measure my newly created and correctly scaled large square ImageView.

If all goes as planned, I now know how big the space available is on the user's device for my RelativeLayout because it's the same as my ImageView. By knowing how much space is available and how many rows and columns need to be displayed, I can correctly scale each ImageView I add accordingly, assuming I remember to account for the spacing between the ImageViews I add. Now I no longer need the square ImageView and can make it go away if I want.

If reasonable, I would like the large ImageView to have rounded corners and each ImageView added also have rounded corners. With a small gap between ImageViews added, this would look cool for gameplay. This way I can leave it as a background.

This seems like a good solution, and better than my previous attempts that didn't work, but if there's a better way..., I'm listening.

Here's some psuedo-code that says mostly the same thing as above to the best of my understanding:

My most basic question is if this seems like a reasonable solution? If so, there's a bunch of things in the psuedocode I don't know how to do (yet).

  • How do I create a small square ImageView in code?
  • How do I set the scaleType in code?
  • Will I be able to measure my square ImageView and get the correct size?
  • Is there a better way to remove the square ImageView other than making it invisible?
  • Will making my square ImageView invisible make it unmeasurable?
  • I see something called a ShapeDrawable, would this be better than an ImageView for making a square?
  • How do I crop an ImageView to be square, distributing the loss equally to both sides?

I certainly don't expect anyone to know all these answers, but if you know any answers you can share, I would appreciate it.

Thanks.

0 投票
1 回答
3788 浏览

android - 在自定义视图中更新 TextView

我的活动中有一个半屏自定义视图和一个 TextView。

单击自定义视图后,如何在我的活动中更新 TextView?

目前我在我的自定义视图中有这段代码,但它在该部分onTouchEvent()中遇到了 NullPointerException 。setText()我不应该在我的自定义视图中更新 TextView 吗?

0 投票
1 回答
1926 浏览

android - SurfaceView 正在剪裁我的视图

我已经编写了自己的自定义 ViewGroup 以能够显示多个子视图并动态移动它们并旋转它们。我还将这个自定义 ViewGroup 与显示相机预览的背景 SurfaceView 结合起来,以便始终将相机预览作为背景。

如果我只使用没有 Camera SurfaceView 的 ViewGroup,一切正常,但是当我添加 Camera SurfaceView 时,我得到了图像中记录的这种奇怪的剪切行为。请注意,仅当我移动或旋转视图时才会发生剪裁,它们似乎在原始位置未剪裁。

在下图中,蓝线应该是子视图的封闭矩形,它传递给我在自定义 ViewGroup 的所有子视图上调用的布局方法:

公共无效布局(int l,int t,int r,int b)

http://developer.android.com/reference/android/view/View.html#layout(int , int, int, int)

不要担心红线。

我的假设是,当 ViewGroup 首次创建时,它只考虑原始子视图的位置,这是为它们保留的空间。一旦我旋转它们,它们就会被剪裁到原来的矩形。

270度旋转:

270度旋转无相机背景

与相机背景相同(相机没有出现在屏幕截图上,这就是为什么它也是黑色的):

带相机背景的 270 度旋转

320度旋转:

320度旋转无相机背景

与相机相同

带相机背景的 320 度旋转

0度旋转:

0度旋转无相机背景

与相机相同

0 度旋转与相机背景

这是代码片段,我删掉了很多东西,但这是非常基本的功能(mLayout 是我的自定义 ViewGroup):

0 投票
2 回答
519 浏览

android - 使用来自 xml 的自定义视图。查看类转换错误

运行我的代码时,当我为我的视图初始化 XML 布局时,我得到一个 java.lang.ClassCastException: android.view.View。

为什么?

更新

问题是因为我在视图元素中添加了一个按钮。更正了这个位置,它再次工作

0 投票
1 回答
13830 浏览

android - 将我的自定义视图添加到 XML 布局中会引发异常

我正在尝试创建我的第一个自定义 View 子类GraphView并将其添加到我的布局 XML 文件中。

我发现了这个 SO question并尝试将其应用到我的项目中,但似乎有些问题,因为 Eclipse IDE 告诉我:

并抛出异常:

这是我的布局:

我的代码:

0 投票
1 回答
2659 浏览

android - 具有膨胀 xml 和子节点的自定义 LinearLayout

我有一个自定义导航类,其中有公共视图,因此我想将它们放入一个布局中,该布局将由自定义类膨胀,并且在主布局上放置一些导航项。

目前,当我扩展线性布局时,xml 中的视图替换了膨胀视图中的所有视图(我将此代码放在构造函数上)

有没有一种方法可以使布局膨胀并获取 xml 实现的子级并将其放在一个视图上?喜欢

要膨胀的布局

用于主布局

我的问题是如何将菜单 1 和菜单 2 放入 navigationItemHolder?谢谢

0 投票
2 回答
535 浏览

android - 如何创建android自定义视图?

我的应用中有一张图片,可以根据用户的选择分成 6 x 6 或 5 x 5 或 4 x 4 grib。

有人可以告诉我如何创建一个可以容纳碎片的平铺视图,并且用户应该能够在视图周围移动碎片。

任何帮助将不胜感激。

谢谢。

0 投票
3 回答
5448 浏览

android - 样式化自定义视图

我的 Android 项目中有一些自定义视图,并且已将相关详细信息添加到 attrs.xml 文件中。现在我可以通过 XML 实现我的对象了。这工作正常。

我如何设置这些元素的样式?当我尝试在 styles.xml 中使用我的自定义属性时,出现错误“找不到与给定名称匹配的资源:”

为了在正常的 xml 开发中使用自定义视图,我使用 xmlns:app="http://schemas.android.com/apk/res/bla.bla.bla"。在样式中正确使用什么?

这就是我目前的风格

0 投票
1 回答
224 浏览

java - 安卓。搜索/帮助:带有一些位图的自定义视图 + 画布的非常基本的代码“框架”

我在这里浏览了几个主题,研究了月球发射器,但我仍然不知道如何为我的项目找到一个良好的开端。(项目是:将现有的 javascript/xml 游戏移植到 android/Java。)
到目前为止,我发现的所有示例要么太远了 - 对我来说 - 要么只是一些你应该自己集成和定义的代码行(构造函数ETC。)。

我的问题是:我有一个有效的“应用程序逻辑”,但只有“理论” Java 知识(学习!)。

我需要的是:几行代码(类?)定义一个自定义视图(“customGame”),其中三个位图(“tile1.png”..“tile3.png”)放置在定义了 X/Y 值的画布上.
这就是全部!
...并且:如果有人还提到一种简单的方法/方法来识别触摸/单击的位图,我会很高兴。

从第一步开始,我有信心/有希望找到前进的道路……我仍在努力学习 Java 的特性;因此,我(还)不能修复代码中的小错误,你们中的大多数人可能会嘲笑它们!
请:只发布不依赖于我识别明显遗漏的代码。

非常感谢   任何愿意花时间给予我这么大恩惠的人!!!

Clemens(来自德国的平面艺术家)

0 投票
11 回答
53169 浏览

android - Android onClick 方法不适用于自定义视图

我已经开始开发一个应用程序。我昨天构建了菜单,但是 onClick 方法不起作用!我创建了一个扩展 View 的类并将其命名为 MainMenuObject - 该类适用于主菜单中的任何对象(按钮、徽标等)。我为他们创建了一个特殊的类,因为我在菜单启动时制作动画。在我构建了 MainMenuObject 类之后,我构建了另一个类 (OpeningTimesView),它扩展了 View,其中包含主菜单的所有按钮,并将用作主要活动的布局。

一切都很好,动画效果很好,我想在我的按钮上放置监听器,所以我在 OpeningTimesView 类中添加了 onClickListener 的实现,并覆盖了 onClick 方法。然后我使用 setOnClickListener(this) 和 setClickable(true) 将侦听器添加到按钮中,但它不起作用!我什么都试过了!请帮我弄清楚我做错了什么。我在 onClick 方法中添加了一个 toast,它不依赖于任何“if”,但它也不会显示。

(顺便说一句,有没有办法将屏幕宽度和高度定义为所有类都可以访问的变量?它不能是静态的,因为您从显示对象获取高度和宽度,但必须有另一种方式)

这是代码:

提前感谢,埃拉德!