java - How do i draw the same moving Bitmap multiple times? -
i want able create multiple bitmaps same image resource (firered.png). have creates everytime x position goes under 300, when run programm stops , gives me following:
i/art: starting blocking gc alloc i/art: waiting blocking gc alloc i/art: clamp target gc heap 387mb 384mb i/art: alloc concurrent mark sweep gc freed 0(0b) allocspace objects, 0(0b) los objects, 0% free, 383mb/384mb, paused 1.466ms total 1.212s i/art: forcing collection of softreferences 16b allocation i/art: starting blocking gc alloc i/art: waiting blocking gc alloc
dont know whats wrong in code: class create firered
package com.acimutstudios.deicide; import android.graphics.bitmap; import android.graphics.canvas; public class firered { private int id = 1; private bitmap firered; public firered(bitmap bmp){ firered = bmp; } public void drawfirered(canvas canvas, int x, int y){ canvas.drawbitmap(firered,x,y,null); } public void setid(int newid){ id = newid; } public int getid(){ return id; } }
and main program:
public class deicideview extends surfaceview implements surfaceholder.callback { private context mycontext; private surfaceholder mysurfaceholder; private bitmap backgroundimg; private int screenw = 1; private int screenh = 1; private boolean running = false; private boolean ontitle = true; private deicidethread thread; private int backgroundorigw; private int backgroundorigh; private float scalew; private float scaleh; private float drawscalew; private float drawscaleh; private bitmap pj; private int pj1x; private int pj1y; private bitmap floor; private int floor1x; private int floor1y; private bitmap fireblue; private int fireblue1x; private int fireblue1y; private bitmap firered; private list<firered> firered = new arraylist<firered>(); private int firered1x; private int firered1y; private boolean gameover = false; private int firetype = 0; //1 es rojo 2 es azul private int firespeed = 5; private boolean createfire = false; //para crear fuegos public deicideview(context context, attributeset attrs) { super(context, attrs); surfaceholder holder = getholder(); holder.addcallback(this); thread = new deicidethread(holder, context, new handler() { @override public void handlemessage(message m) { } }); setfocusable(true); } public deicidethread getthread() { return thread; } class deicidethread extends thread { public deicidethread(surfaceholder surfaceholder, context context, handler handler) { mysurfaceholder = surfaceholder; mycontext = context; backgroundimg = bitmapfactory.decoderesource(context.getresources(), r.drawable.title); backgroundorigh = backgroundimg.getheight(); backgroundorigw = backgroundimg.getwidth(); } @override public void run() { while (running) { canvas c = null; try { c = mysurfaceholder.lockcanvas(null); synchronized (mysurfaceholder) { if(!gameover && !ontitle){ createfire(); animatefire(); } draw(c); } } { if (c != null) { mysurfaceholder.unlockcanvasandpost(c); } } } } private void draw(canvas canvas) { try { canvas.drawbitmap(backgroundimg,0,0,null); if(!ontitle){ canvas.drawbitmap(pj,pj1x,pj1y,null); //canvas.drawbitmap(firered,firered1x,firered1y,null); for(firered red : firered){ red.drawfirered(canvas,firered1x,firered1y); } canvas.drawbitmap(fireblue,fireblue1x,fireblue1y,null); canvas.drawbitmap(floor,floor1x,floor1y,null); } } catch (exception e) { } } boolean dotouchevent(motionevent event) { synchronized (mysurfaceholder) { int eventaction = event.getaction(); int x = (int) event.getx(); int y = (int) event.gety(); switch (eventaction) { case motionevent.action_down: break; case motionevent.action_move: break; case motionevent.action_up: if (ontitle) { backgroundimg = bitmapfactory.decoderesource(mycontext.getresources(), r.drawable.background); backgroundimg = bitmap.createscaledbitmap(backgroundimg, screenw, screenh, true); pj = bitmapfactory.decoderesource(mycontext.getresources(),r.drawable.sprite_jumping0); floor = bitmapfactory.decoderesource(mycontext.getresources(),r.drawable.sprite_fondo20); firered = bitmapfactory.decoderesource(mycontext.getresources(),r.drawable.fuego1); fireblue = bitmapfactory.decoderesource(mycontext.getresources(), r.drawable.fuego2); scalew = (float)screenw/(float)backgroundorigw; scaleh = (float)screenh/(float)backgroundorigh; pj = bitmap.createscaledbitmap(pj,(int)(pj.getwidth()*scalew), (int)(pj.getheight()*scaleh),true); floor = bitmap.createscaledbitmap(floor,(int)(floor.getwidth()*scalew),(int)(floor.getheight()*scaleh),true); firered = bitmap.createscaledbitmap(firered,(int)(firered.getwidth()*scalew),(int)(firered.getheight()*scaleh),true); fireblue = bitmap.createscaledbitmap(fireblue,(int)(fireblue.getwidth()*scalew),(int)(firered.getheight()*scaleh),true); ontitle = false; pickfire(); } break; } } return true; } public void setsurfacesize(int width, int height) { synchronized (mysurfaceholder) { screenw = width; screenh = height; backgroundimg = bitmap.createscaledbitmap(backgroundimg, width, height, true); drawscalew = (float)screenw/800; drawscaleh = (float)screenh/600; pj1x = (int)(55*drawscalew); pj1y = (int)(225*drawscaleh); floor1x = 0; floor1y = (int)(370*drawscaleh); firered1x = (int)(400*drawscalew); //(screenw*drawscalew); firered1y = (int)(225*drawscaleh); fireblue1x = (int)(500*drawscalew); //(screenw*drawscalew); fireblue1y= (int)(225*drawscaleh); } } public void setrunning(boolean b) { running = b; } private void animatefire(){ if(firetype == 1){ firered1x -= firespeed; if(firered1x == (int)(300*drawscalew)){ createfire = true; }else{createfire = false;} } if(firetype == 2){ fireblue1x -= firespeed; } } private void pickfire(){ //firetype = new random().nextint(2)+1; firetype = 1; } private void createfire(){ do{ if(firetype == 1){ firered red = new firered(firered); firered.add(red); } }while(!gameover && createfire); } } @override public boolean ontouchevent(motionevent event) { return thread.dotouchevent(event); } @override public void surfacechanged(surfaceholder holder, int format, int width, int height) { thread.setsurfacesize(width, height); } @override public void surfacecreated(surfaceholder holder) { thread.setrunning(true); if (thread.getstate() == thread.state.new) { thread.start(); } } @override public void surfacedestroyed(surfaceholder holder) { thread.setrunning(false); } }
Comments
Post a Comment