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



这是我在画布上绘制图片的类,在这个类上,我试图将画布的背景设置为我设计的现有xml布局。我想做的事情就像这个虚构的函数:canvas.setBackground(R.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视图,并将高度和宽度设置为match_parent,作为具有透明背景的最后一个元素

使用这些构造函数更新您的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>

最新更新