这是我将图片绘制到这个画布上的类,在这个类中,我正在尝试将画布的背景设置为我设计的现有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>
答案 0 :(得分:0)
快速又脏:在xml中插入MyBringBack视图,并将height_parent设置为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>