SurfaceView and xml

65 Views Asked by At

I have program where i use my SurfaceView class. But i want to add ad in my game.To do it i must add xml and use it (i never use xml file and know few about it). Help me pls whith it.

Main Activity on create:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ThisActivity=this; 
    new Game(this);
    setContentView(Game.game);
}

Game:

public class S_Game extends SurfaceView implements CallBack{
    public S_Game(Context context){
        super(context);
        getHolder().addCallback(this);
        game=this;
        sinh0=false;
        sinh1=false;
        sinh2=true;
        String abas=" "+"";
        spacekey=abas.hashCode();
        paint=new Paint();
        paint.setColor(0xffff0000);
        paint.setTextSize(20);
        pickx=new AtomicInteger[3];
        picky=new AtomicInteger[3];
        ifpick0=new AtomicBoolean[3];
        for(int i=0;i<3;i++){
            pickx[i]=new AtomicInteger();
            picky[i]=new AtomicInteger();
            ifpick0[i]=new AtomicBoolean();
        }
        pickt=new AtomicInteger();
        NewTimer();
    }

public void NewTimer(){
    upd=new S_Updater(this);
    timer=new Timer();
    timer.schedule(upd,250,40);
}

public void Init(){
    if(getHeight()>getWidth()){
        S_Graff.thisindgraff=0;
    }else{
        S_Graff.thisindgraff=1;
    }
    DisplayMetrics metrics = new DisplayMetrics();
    MainActivity.ThisActivity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
    xdpi=SK(metrics.xdpi);
    ydpi=SK(metrics.ydpi);
    xdpi=SK(metrics.densityDpi);
    float sm=xdpi/3;
    float sms=(55/sm);
    dpi=sms;
    wind=new S_Window(0,0,760,1430);
    Board.psize=KonK(18);
    Board.DY/=sms;
    if(dpi<=0.55){
        dpi=0.55f;
    }
    String s="|";
    Board.LINE=s.hashCode();
}
public void Update(Canvas cnv){
    if(getHeight()>getWidth()){
        maxx=smaxx;
        maxy=smaxy;
        kx=maxx/getWidth();
        ky=maxy/getHeight();
        kk=kx;
        S_Graff.thisindgraff=0;
        debug0=""+maxy+"/"+getHeight();
        debug1=""+maxx+"/"+getWidth();
    }else{
        maxx=smaxy;
        maxy=smaxx;
        kx=maxx/getWidth();
        ky=maxy/getHeight();
        kk=ky;
        S_Graff.thisindgraff=1;
    }
    if(binit){LInit();binit=false;}
    for(int i=0;i<3;i++){
        if(!ifpick0[i].get()){
            float px=pickx[i].get();
            px/=1000;
            float py=picky[i].get();
            py/=1000;
            ifpick0[i].set(true);
            S_Window.Pick(px,py,i);
    }
    }
    S_Graff.OnRotAll();
    cnv.drawColor(0xffffffff);
//I draw all S_Graffs in Update
    S_Graff.Update(cnv);
    Board.UpdateText(cnv);
}
@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {}

@Override
public void surfaceCreated(SurfaceHolder holder) {
    //new Timer().schedule(upd,250,40);
    /*Canvas c = getHolder().lockCanvas();
    Update(c);
    getHolder().unlockCanvasAndPost(c);*/
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {

}
}

S_Updater:

public class S_Updater extends TimerTask{
    public S_Game game;
    public SurfaceHolder surfaceHolder;
    static boolean isrun=true,ispause;

public S_Updater(S_Game g) {
    game=g;
    surfaceHolder=g.getHolder();
}

@Override
public void run() {
    Canvas canvas=null;
    try {
        synchronized (surfaceHolder) {
            if(surfaceHolder.getSurface().isValid()){
                canvas=surfaceHolder.lockCanvas(null);
                if(canvas!=null){
                    game.Update(canvas);
                    surfaceHolder.unlockCanvasAndPost(canvas);
                }
            }
        }
    } 
    finally {}  
}

}
0

There are 0 best solutions below