我正在尝试绘制一个由位图组成的按钮,例如
b[j] = (Button) sm.findViewById(R.drawable.picture);
但我无法使用draw(Canvas canvas)
方法显示此图片。
有一种方法可以像
一样显示像位图一样的按钮canvas.drawBitmap(bitmap,..,..,null);
我实际上不想在xml或onCreate()
方法中显示按钮。
有没有办法可以从其他任何地方显示它????
谢谢!!!!
我会在这里发布我的节目请帮助!!!
创建了四个类......
在这里输入代码
**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!!!
答案 0 :(得分:1)
使用ImageButton在按钮上显示图像而不是文本。
答案 1 :(得分:1)
(1)您可以使用ImageButton来完成您的运动,如下所示:
<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" />
此处“ic_your_image”是您想要显示为按钮的图像。
(2)或者,您只需使用图像视图并通过在其上设置onClickListener使其可点击。
(3)你也可以通过编程方式完成:
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);
要在imageview中设置位图(我不确定,你必须测试它):
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);