将布局与画布一起使用或使用画布上可绘制的背景指定背景颜色

发布于 2024-12-11 16:25:54 字数 1464 浏览 0 评论 0原文

我正在为编程作业创建一个小游戏。我试图弄清楚如何正确设置背景,但我似乎无法弄清楚。

我的 main.xml 中有两个线性布局;一个具有背景颜色,其中的另一个布局具有背景图像(透明 PNG)。当我启动应用程序时,它显示得很好:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#aacceeff"
    >

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/background"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    >

 </LinearLayout>

</LinearLayout>

但是,我现在必须添加动画。为此,我创建了一个 View 对象来加载图像并为其设置动画。但是,我现在必须将主活动的内容视图设置为这个新视图对象的实例。当我这样做时,我得到一个空指针异常,因为它找不到 id background 的线性布局:

BitmapDrawable tiledBackground = new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.drawable.background));
LinearLayout backgroundLayout = (LinearLayout) findViewById(R.id.background);
tiledBackground.setTileModeX(Shader.TileMode.REPEAT);
backgroundLayout.setBackgroundDrawable(tiledBackground); //backgroundLayout is null

我认为这是因为视图不再是 main.xml 中指定的视图。如果我删除此代码,代码运行正常,但我会得到黑色背景。我尝试将此特定片段移至自定义视图并将背景设置为图像,但随后我得到的图像具有黑色背景;我真正想要的是一个透明的图像,背景颜色可以透过。

我想我想弄清楚的是如何告诉自定义视图使用我在 main.xml 中定义的布局,或者找出一种指定背景颜色的方法以及自定义视图的可绘制背景。

I'm creating a small game for a programming assignment. I'm trying to figure out how to set the background properly, but I can't seem to figure out.

I have a two linear layouts in my main.xml; one has a background color, and another layout that's inside it has a background image (transparent PNG). It shows up fine when I start up the app:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#aacceeff"
    >

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/background"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    >

 </LinearLayout>

</LinearLayout>

However, I now have to add the animation. To do that I created a View object that loads up images and animates them. However, I now have to set the content view from the main activity to an instance of this new view object. When I do that, I get a null pointer exception because it cannot find the linear layout with id background:

BitmapDrawable tiledBackground = new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.drawable.background));
LinearLayout backgroundLayout = (LinearLayout) findViewById(R.id.background);
tiledBackground.setTileModeX(Shader.TileMode.REPEAT);
backgroundLayout.setBackgroundDrawable(tiledBackground); //backgroundLayout is null

I figure this is because the view is no longer the view that's specified in main.xml. If I remove this code, the code runs fine but I get a black background. I tried moving this specific snippet to the custom view and set the background to the image, but then I get the image with a black background; what I actually want is a transparent image with the background color showing through.

I guess what I'm trying to figure out is how to tell the custom view to use the layout I've defined in main.xml, or figure out a way to specify a background color and a background drawable for the custom view.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

破晓 2024-12-18 16:25:54

我可以通过使用滤色镜解决这个问题:

super(context);

BitmapDrawable tiledBackground = new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.drawable.background));
tiledBackground.setTileModeX(Shader.TileMode.REPEAT);

//I determined the PorterDuff.Mode value mainly through trial and error
tiledBackground.setColorFilter(0xaacceeff, PorterDuff.Mode.DARKEN);

this.setBackgroundDrawable(tiledBackground)

I was able to solve this by using a color filter:

super(context);

BitmapDrawable tiledBackground = new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.drawable.background));
tiledBackground.setTileModeX(Shader.TileMode.REPEAT);

//I determined the PorterDuff.Mode value mainly through trial and error
tiledBackground.setColorFilter(0xaacceeff, PorterDuff.Mode.DARKEN);

this.setBackgroundDrawable(tiledBackground)
能否归途做我良人 2024-12-18 16:25:54

当您使用 findViewById 方法时,您正在从 setContentView 方法中指定的layout.xml 中查找布局或视图。

所以如果你调用setContentView并设置另一个xml文件作为布局,那么你将无法从之前的布局中找到视图。

在理想情况下,setContentView 仅针对活动调用一次。不同的屏幕需要新的活动。但是您也可以使用 ViewFlipper 类,您可以将两个布局添加为其子布局。它一次只会显示 1 个孩子。要显示下一个孩子,您可以使用

viewflipper.setDisplayChild(1);

When you use findViewById method, you are looking for a layout or view from the layout.xml you specify in the setContentView method.

So if you call setContentView and set another xml file as the layout, then you will not be able to find a view from the previous layout.

In ideal cases setContentView is only called once for an activity. New activities are called for different screens. But you call also use the ViewFlipper class, you can add both your layouts as its childs. It will only show 1 child at a time. To show the next child you can use

viewflipper.setDisplayChild(1);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文