Question

I know its possible to paint the background of canvas using

mPaint = new Paint();
mPaint.setColor(Color.RED);

Im just wondering how to i set a permanent background for it. Ive tried using the xml file but nothing happens. Any ideas?

This is the source code of the project, ive been following a tutorial how to do it because im fairly unfamiliar with bitmaps.

Canvas Class

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;

public class GameBoard extends View{

    private int mFlagX = -1;
    private int mFlagY = -1;
    private Bitmap mBitmap = null;
    private Bitmap nBitmap = null;
    private Paint mPaint = null;
    private boolean isFlagHidden = false;
    private int mBoundX = -1;
    private int mBoundY = -1;

    //play with these values to make the app more or less challenging
    public final int CLOSER = 50;
    public final int CLOSE = 100;

    public GameBoard(Context context, AttributeSet aSet) { 
        super(context, aSet); 
        //load our bitmap
        mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.star);
        //create a paint brush
        mPaint = new Paint();
        mPaint.setColor(Color.RED);
    }

    @Override
    public void onDraw(Canvas canvas) {
        //initialize
        if ((mFlagX < 1) || (mFlagY < 1)) {
            mFlagX = (int) (getWidth() / 2) - mBitmap.getWidth() / 2;
            mFlagY = (int) (getHeight() / 2) - mBitmap.getHeight() / 2;
            mBoundX = (int)getWidth() - mBitmap.getWidth();
            mBoundY = (int)getHeight() - mBitmap.getHeight();
        }
        //draw background
    canvas.drawRect(0, 0, getWidth(), getHeight(), mPaint);
        //draw the flag
        if (!isFlagHidden) {
            canvas.drawBitmap(mBitmap, mFlagX, mFlagY, null);
        }
    }

    public void hideTheFlag(){
        //randomize flag location 
        mFlagX = (int) Math.ceil(Math.random() * mBoundX);
        mFlagY = (int) Math.ceil(Math.random() * mBoundY);
        isFlagHidden = true;
        //force redraw
        invalidate();   
    }

    public void giveUp(){
        isFlagHidden = false;
        //force redraw
        invalidate();
    }   

    public Indicators takeAGuess(float x, float y) {

        //this is our "warm" area
        Rect prettyClose = new Rect(mFlagX - CLOSE, mFlagY - CLOSE, mFlagX+mBitmap.getWidth() + CLOSE, mFlagY+mBitmap.getHeight() + CLOSE);
        //normalize
        if (prettyClose.left < 0) prettyClose.left = 0;
        if (prettyClose.top < 0) prettyClose.top = 0;
        if (prettyClose.right > mBoundX) prettyClose.right = mBoundX;
        if (prettyClose.bottom > mBoundY) prettyClose.bottom = mBoundY;

        //this is our "hot" area
        Rect reallyClose = new Rect(mFlagX - CLOSER, mFlagY - CLOSER, mFlagX+mBitmap.getWidth() + CLOSER, mFlagY+mBitmap.getHeight() + CLOSER);
        //normalize
        if (reallyClose.left < 0) reallyClose.left = 0;
        if (reallyClose.top < 0) reallyClose.top = 0;
        if (reallyClose.right > mBoundX) reallyClose.right = mBoundX;
        if (reallyClose.bottom > mBoundY) reallyClose.bottom = mBoundY;

        //this is the area that contains our flag
        Rect bullsEye = new Rect(mFlagX, mFlagY, mFlagX+mBitmap.getWidth(), mFlagY+mBitmap.getHeight());

        //check to see where on the board the user pressed
        if (bullsEye.contains((int) x, (int)y)) {
            //found it
            isFlagHidden = false;
            invalidate();
            return Indicators.BULLSEYE;
        } else if (reallyClose.contains((int) x, (int)y)) {
            //hot
            return Indicators.HOT;
        } else if (prettyClose.contains((int)x, (int)y)) {
            //warm
            return Indicators.WARM;
        } else {
            //not even close
            return Indicators.COLD;
        }
    }
}

Game Class

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.TextView;

public class FindTheStar extends Activity implements OnTouchListener, OnClickListener{

    private GameBoard mGameBoard = null;
    private boolean isFlagHidden = false;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main_star);
        mGameBoard = (GameBoard) findViewById(R.id.Hide_canvas);
        mGameBoard.setOnTouchListener(this);
        Button b = (Button) findViewById(R.id.the_button);
        b.setOnClickListener(this);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (v.getId() == R.id.Hide_canvas) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                if (isFlagHidden) {
                    TextView tv = (TextView)findViewById (R.id.the_label);
                    switch (mGameBoard.takeAGuess(event.getX(), event.getY())) {
                    case BULLSEYE:
                        Button b = (Button) findViewById(R.id.the_button);
                        isFlagHidden = false;
                        b.setText("Go Hide!");
                        tv.setText("You found me!");
                        tv.setTextColor(Color.GREEN);
                        break;
                    case HOT:
                        tv.setText("You're hot!");
                        tv.setTextColor(Color.RED);
                        break;
                    case WARM:
                        tv.setText("Getting warm...");
                        tv.setTextColor(Color.YELLOW);
                        break;
                    case COLD:
                        tv.setText("You're cold.");
                        tv.setTextColor(Color.BLUE);
                        break;
                    }
                }
            }
            return true;
        }
        return false;
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.the_button) {
            TextView tv = (TextView)findViewById (R.id.the_label);
            tv.setText("");
            Button b = (Button) findViewById(R.id.the_button);
            isFlagHidden = !isFlagHidden;
            if (isFlagHidden) {
                b.setText("Can't find me?");
                mGameBoard.hideTheFlag();
            } else {
                b.setText("Go Hide!");
                mGameBoard.giveUp();
            }
        }
    }
}

XML File

<?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">
    <TextView  
        android:id="@+id/the_label"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center" 
        android:textSize="20sp"
        android:layout_marginBottom="10dip"
        android:text="Lets Play Hide and Seek!"/>
    <Button
        android:id="@+id/the_button"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center" 
        android:layout_marginBottom="10dip"
        android:text="Go Hide!"/>

    <app.autismapp.GameBoard

        android:layout_width="fill_parent"  
        android:layout_height="fill_parent"
        android:id="@+id/Hide_canvas"/>



</LinearLayout>
Was it helpful?

Solution

yes you can set your permanent background using xml layout..i done this by creating two class.

this is my code in MainACtivity

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
     final BrushView view=new BrushView(this);
    setContentView(R.layout.mylayout);//removed this one if the paint doesnt work
    view.setBackgroundResource(R.drawable.background);//to set background
    setContentView(view);// to display the background

and my second class

public class PaintView extends View {

private Paint paint = new Paint();

public LayoutParams params;



public PaintView(Context context) {
    super(context);
    paint.setAntiAlias(true);
    paint.setColor(Color.BLUE);

i hope it gives you an idea

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top