Reputation: 55
I m trying to draw a button which consist of a bitmap,like
b[j] = (Button) sm.findViewById(R.drawable.picture);
but i m not able to display this picture using the draw(Canvas canvas)
method.
is there a way how i can display the button like a bitmap as in
canvas.drawBitmap(bitmap,..,..,null);
i actually dont want to display the button from an xml or inside the onCreate()
method.
is there a way i can display it from anywhere else????
Thank you!!!!
i will post my program here please help!!!
have created four classes...
enter code here
**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!!!
Upvotes: 2
Views: 1374
Reputation: 2624
(1)You can use the ImageButton to accomplish you moto , like this:
<ImageButton android:id="@+id/theImageButton"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:layout_gravity="center"
android:background="@drawable/ic_your_image" />
here "ic_your_image" is the image which you want to show as a button.
(2) Or, you can simply use an image view and make it clickable by setting onClickListener on it.
(3) You can do it programmatically as well :
View linearLayout = findViewById(R.id.your_main_xml);
ImageButton newButton=new ImageButton(this);
newButton.setBackgroundDrawable(getApplication().getResources().getDrawable(R.drawable.your_image));
newButton.setId(5);
noLisnText.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
noLisnText.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL);
((LinearLayout) linearLayout).addView(newButton);
To set the bitmap in the imageview(I am not sure about it, you have to test it):
View linearLayout = findViewById(R.id.your_main_xml);
ImageView newImage=new ImageView(this);
newImage.setId(5);
newImage.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
((LinearLayout) linearLayout).addView(newImage);
newImage.setImageBitmap(bitmap);
Upvotes: 1