android - Adding drawable image to background of canvas -


i know possible paint background of canvas using

mpaint = new paint(); mpaint.setcolor(color.red); 

im wondering how set permanent background it. ive tried using xml file nothing happens. ideas?

this source code of project, ive been following tutorial how because im unfamiliar 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 these values make 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 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 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 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 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 area contains our flag         rect bullseye = new rect(mflagx, mflagy, mflagx+mbitmap.getwidth(), mflagy+mbitmap.getheight());          //check see on board user pressed         if (bullseye.contains((int) x, (int)y)) {             //found             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 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 activity 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 , 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> 

yes can set permanent background using xml layout..i done creating 2 class.

this code in mainactivity

protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);      final brushview view=new brushview(this);     setcontentview(r.layout.mylayout);//removed 1 if paint doesnt work     view.setbackgroundresource(r.drawable.background);//to set background     setcontentview(view);// display background 

and 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 gives idea


Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -