Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to draw a bitmap as a button?
    primarykey
    data
    text
    <p>I m trying to draw a button which consist of a bitmap,like</p> <pre><code>b[j] = (Button) sm.findViewById(R.drawable.picture); </code></pre> <p>but i m not able to display this picture using the <code>draw(Canvas canvas)</code> method.</p> <p>is there a way how i can display the button like a bitmap as in</p> <pre><code>canvas.drawBitmap(bitmap,..,..,null); </code></pre> <p>i actually dont want to display the button from an xml or inside the <code>onCreate()</code> method.</p> <p>is there a way i can display it from anywhere else????</p> <p>Thank you!!!!</p> <p>i will post my program here please help!!!</p> <p>have created four classes...</p> <p>enter code here</p> <pre><code>**class 1:-** package game.pack; import android.app.Activity; import android.os.Bundle; import android.view.Window; import android.view.WindowManager; public class Sushitap extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(new SushiMain(this)); } } **class 2:-** package game.pack; import android.content.Context; import android.graphics.*; import android.util.DisplayMetrics; import android.view.*; import android.widget.Button; public class SushiMain extends SurfaceView implements SurfaceHolder.Callback { Canvas canvas; Tower s; SushiThread st; int height; int width; public SushiMain(Context context) { super(context); getHolder().addCallback(this); DisplayMetrics met = context.getResources().getDisplayMetrics(); height=met.heightPixels; width= met.widthPixels; int x=width; s = new Tower(context,this,x,260); st = new SushiThread(getHolder(), this); setFocusable(true); } public void surfaceChanged(SurfaceHolder sh, int format, int height, int width) { getResources(); } public void surfaceCreated(SurfaceHolder sh) { st.setRunning(true); st.start(); } public void surfaceDestroyed(SurfaceHolder holder) { boolean retry = true; while (retry) { try { st.join(); retry = false; } catch (InterruptedException e) { } } } public void render(Canvas canvas) { s.onDraw(canvas); } } **class 3:-** package game.pack; import android.graphics.*; import android.view.*; public class SushiThread extends Thread{ private SurfaceHolder sh; private SushiMain sm; Tower t; private boolean running; public SushiThread(SurfaceHolder sh,SushiMain sm) { super(); this.sh=sh; this.sm=sm; } public void run() { Canvas canvas; while(running) { canvas=null; try{ canvas=this.sh.lockCanvas(); synchronized (sh) { this.sm.render(canvas); } } finally{ if(canvas!=null){ sh.unlockCanvasAndPost(canvas); } } } } public void setRunning(boolean running) { this.running=running; } **class 4** package game.pack; import java.util.Random; import android.content.Context; import android.graphics.*; import android.view.View; import android.widget.Button; import android.widget.ImageButton; public class Tower extends View { int x,y; ImageButton sushiArray; Canvas canvas; public Tower(Context c,SushiMain mainObj, int x, int y) { super(c); this.x = x; this.y = y; } protected void onDraw(Canvas canvas) { sushiArray.draw(canvas); } } please let me know how to implement my imageButton.,,thank you!!! </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload