Android 我想在现有的xml背景上使用画布绘制,我该怎么做?

Android 我想在现有的xml背景上使用画布绘制,我该怎么做?,android,Android,这是我在画布上绘制图片的类,在这个类上,我试图将画布的背景设置为我设计的现有xml布局。 我想做的是这样一个虚构的函数:canvas.setBackgroundR.layout.game;关于onDraw方法。 java类: package com.example.snakesnladders; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactor

这是我在画布上绘制图片的类,在这个类上,我试图将画布的背景设置为我设计的现有xml布局。 我想做的是这样一个虚构的函数:canvas.setBackgroundR.layout.game;关于onDraw方法。 java类:

package com.example.snakesnladders;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.view.View;

public class MyBringBack extends View {
    Bitmap playerW;

    public MyBringBack(Context context) {
        super(context);

        playerW = BitmapFactory
                .decodeResource(getResources(), R.drawable.white);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.onDraw(canvas);
        canvas.drawBitmap(playerW, 0, 0, null);
    }

}
XML文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/easymap"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/whitePlayer"
        android:layout_width="32dp"
        android:layout_height="32dp"
        android:background="@drawable/white"
        android:visibility="gone" />

    <TextView
        android:id="@+id/blackPlayer"
        android:layout_width="32dp"
        android:layout_height="32dp"
        android:visibility="gone"
        android:background="@drawable/black" />

    <Button
        android:id="@+id/btRoll"
        android:layout_width="160dp"
        android:layout_height="50dp"
        android:layout_alignParentLeft="true"
        android:layout_alignTop="@+id/cubePic"
        android:background="@drawable/buttonshape"
        android:text="Roll"
        android:textColor="#FFFFFF" />

    <TextView
        android:id="@+id/cubePic"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="42dp"
        android:layout_marginRight="22dp"
        android:background="@drawable/cube" />

    <TextView
        android:id="@+id/tvTurn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/cubePic"
        android:layout_alignRight="@+id/btRoll"
        android:text="Your turn!"
        android:textColor="@color/green"
        android:textSize="32dp"
        android:textStyle="italic" />

</RelativeLayout>

快速且脏:在xml中插入MyBringBack视图,并设置高度和宽度以匹配作为透明背景的最后一个元素的父元素

使用以下构造器更新MyBringBack:

public MyBringBack (Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    initView();
}

public MyBringBack (Context context, AttributeSet attrs) {
    super(context, attrs);
    initView();
}

public MyBringBack (Context context) {
    super(context);
    initView();
}

private void initView() {
    //DO WHAT YOU WANT ON INIT
    playerW = BitmapFactory
            .decodeResource(getResources(), R.drawable.white);

}
还有像这样的XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="@drawable/easymap"
  android:orientation="vertical" >

<TextView
    android:id="@+id/whitePlayer"
    android:layout_width="32dp"
    android:layout_height="32dp"
    android:background="@drawable/white"
    android:visibility="gone" />

<TextView
    android:id="@+id/blackPlayer"
    android:layout_width="32dp"
    android:layout_height="32dp"
    android:visibility="gone"
    android:background="@drawable/black" />

<Button
    android:id="@+id/btRoll"
    android:layout_width="160dp"
    android:layout_height="50dp"
    android:layout_alignParentLeft="true"
    android:layout_alignTop="@+id/cubePic"
    android:background="@drawable/buttonshape"
    android:text="Roll"
    android:textColor="#FFFFFF" />

<TextView
    android:id="@+id/cubePic"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_marginBottom="42dp"
    android:layout_marginRight="22dp"
    android:background="@drawable/cube" />

<TextView
    android:id="@+id/tvTurn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/cubePic"
    android:layout_alignRight="@+id/btRoll"
    android:text="Your turn!"
    android:textColor="@color/green"
    android:textSize="32dp"
    android:textStyle="italic" />

  <your.package.MyBringBack
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/my_bring_back"
    android:background="@android:color/transparent" />


  </RelativeLayout>

快速而肮脏:在xml中插入MyBringBack视图,并设置高度和宽度以匹配作为最后一个元素的父元素,并具有透明的背景。这对我来说没有意义,我应该在xml文件中的确切位置插入MyBringBack视图,你能帮我写吗?你明白了吗?我试着用你写的东西,但没用,所以我重新创建了一张图片,以适应整个屏幕,并用它作为我的背景,虽然现在我的问题是设置按钮以对点击做出反应,因为背景是一张图片,我不能像以前在XML文件中那样设置按钮。感谢您的努力,不幸的是,它不起作用。错误是“自定义视图MyBringBack未使用2参数或3参数视图构造函数;XML属性将不起作用”,因此您必须使用其他构造函数