I click dice button in arenalayout.xml (this xml is displayed by 'ArenaLayout' class). But canvas doesn't draw char 2.I want to... if I click dice button then I draw char 2.
Note : After I click dice button, value of 'haveFirstDiced' variable in Arena class change to TRUE. There's condition inside of onDraw in 'Arena' class. . . if 'haveFirstDiced' variable have TRUE value then drawing char 2.
public class ArenaLayout extends Activity {
private Arena arena;
ImageButton diceButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.arena_layout);
diceButton = (ImageButton) findViewById(R.id.dice);
dice();
}
private void dice() {
diceButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
arena.dicing();
}
});
}
}
This is 'Arena' class :
public class Arena extends View{
private Paint p;
private Handler threadHandler = new Handler();
private Bitmap char1;
private float x = 20, y = 300;
Canvas c;
boolean haveFirstDiced = false;
public Arena(Context context, AttributeSet aSet) {
super(context, aSet);
p = new Paint();
}
public void dicing() {
new Thread(new XXX()).start();
}
#Override
synchronized public void onDraw(Canvas canvas) {
char2 = BitmapFactory.decodeResource(getResources(), R.drawable.char_2);
if(haveFirstDiced == true) {
canvas.drawBitmap(char2,x,y,null);
}
}
class XXX implements Runnable {
#Override
public void run() {
threadHandler.post(new Runnable() {
#Override
public void run() {
haveFirstDiced = true;
}
});
}
}
}
This is the most convoluted solution I've ever seen. There's no need for a thread, a runnable, or for onDraw to be synchronized. Here's what it should be:
private void dice() {
diceButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
arena.dicing();
}
});
}
public class Arena extends View{
private Paint p;
private Handler threadHandler = new Handler();
private Bitmap char2;
private float x = 20, y = 300;
Canvas c;
boolean haveFirstDiced = false;
public Arena(Context context, AttributeSet aSet) {
super(context, aSet);
p = new Paint();
char2 = BitmapFactory.decodeResource(getResources(), R.drawable.char_2);
}
public void dicing() {
haveFirstDiced = true;
invalidate();
}
#Override
public void onDraw(Canvas canvas) {
if(haveFirstDiced == true) {
canvas.drawBitmap(char2,x,y,null);
}
}
}
Related
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 {}
}
}
I know this may seem like a noob question but I've tried everything. I want to animate a number incrementing. So I set up a view. I made a thread inside the view that loops a certain amount of times. In the loop I call Thread.sleep and then I increment a number. However I'm not sure how to call the invalidate() method to update the view each time it goes through the loop. I've tried a callback but that isn't working. Your help would be appreciated. What I'm trying to increment is the 'centerText' which is drawn using textPaint
public class GameView extends View {
private int backgroundColor;
private int circleColor;
private int radiusFactor;
private Paint circlePaint;
private Paint textPaint;
private Paint text2Paint;
private Paint text3Paint;
private String topMessage;
private String bottomMessage;
private String topMessage2;
private String bottomMessage2;
private String centerText;
private boolean isRunning = false;
private int FPS = 60;
public interface animateThread{
public void updateUI(String text);
}
private animateThread threadCheck = new animateThread(){
#Override
public void updateUI(String text) {
centerText="text";
invalidate();
}
};
public GameView(Context context) {
super(context);
backgroundColor = Color.parseColor("#FF4000");
circleColor = Color.parseColor("#B40404");
radiusFactor = 4;
centerText="?";
circlePaint = new Paint();
circlePaint.setAntiAlias(true);
circlePaint.setColor(circleColor);
textPaint = new Paint();
textPaint.setAntiAlias(true);
textPaint.setColor(Color.WHITE);
textPaint.setTextAlign(Paint.Align.CENTER);
text2Paint = new Paint();
text2Paint.setAntiAlias(true);
text2Paint.setColor(Color.BLACK);
text2Paint.setTextAlign(Paint.Align.CENTER);
text3Paint = new Paint();
text3Paint.setAntiAlias(true);
text3Paint.setColor(Color.BLACK);
text3Paint.setTextAlign(Paint.Align.CENTER);
topMessage = "One in a";
topMessage2="Hundred?";
bottomMessage="Click the Circle";
bottomMessage2="To find out";
}
#Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_MOVE:
break;
}
if(!isRunning) startAnimation(threadCheck);
return true;
}
#Override
protected void onDraw(Canvas canvas) {
setBackgroundColor(backgroundColor);
textPaint.setTextSize(getWidth()/4);
text2Paint.setTextSize(getWidth()/6);
text3Paint.setTextSize(getWidth()/8);
canvas.drawCircle(getWidth()/2, getHeight()/2,getWidth()/radiusFactor,
circlePaint);
canvas.drawText(centerText, getWidth()/2, getHeight()/2-
((textPaint.descent()+textPaint.ascent())/2), textPaint);
canvas.drawText(topMessage, getWidth()/2, ((getHeight()/2-
getWidth()/radiusFactor)/2+((text2Paint.descent()+text2Paint.ascent())/2)),
text2Paint);
canvas.drawText(topMessage2, getWidth()/2, (getHeight()/2-
getWidth()/radiusFactor)/2-((text2Paint.descent()+text2Paint.ascent())),
text2Paint);
canvas.drawText(bottomMessage, getWidth()/2, getHeight()*4/5+
((text3Paint.descent()+text3Paint.ascent())/2), text3Paint);
canvas.drawText(bottomMessage2, getWidth()/2, getHeight()*4/5-
((text3Paint.descent()+text3Paint.ascent())), text3Paint);
}
public void startAnimation(final animateThread mCallback) {
Thread animate = new Thread(new Runnable(){
#Override
public void run() {
isRunning=true;
int count=0;
for (int i=0;i<FPS*5;i++) {
count++;
if(count>100) count = 1;
mCallback.updateUI(count+"");
try {
Thread.sleep(1000/FPS);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
isRunning=false;
}
});
animate.start();
}
}
Anything invoked within
new Thread(new Runnable(){
...
}
Will implicitly run on a non-UI thread. If you want to touch the UI, the code for that task must be forced to run on the UI thread like this:
context.runOnUiThread(new Runnable() {
#Override
public void run() {
updateUI(count+"");
}
});
I am a newbie in android and started with livewallpaper development using Wallpaper Engine. My problem is the run() method inside Runnable is not called dont know why.... Am I missing something?
Here is my code:
public class Hearts extends WallpaperService {
public static boolean LANDSCAPE;
public static Paint backgroundPaint;
public static Point backgroundSize;
public static Point globalCanvasSize;
public static boolean landscapeMode = false;
public static int maxScreenSize;
Bitmap background;
private final Handler mHandler = new Handler();
public void onCreate() {
super.onCreate();
}
public WallpaperService.Engine onCreateEngine() {
return new HeartsEngine();
}
public void onDestroy() {
super.onDestroy();
}
class HeartsEngine extends Engine {
private final int amountOfFlakes = 40;
private final Paint backgroundPaint = new Paint();
private Canvas c;
public Flake[] flakeArray = new Flake[40];
final SurfaceHolder holder = getSurfaceHolder();
private final Runnable mDrawCube = new Runnable() {
#Override
public void run() {
Log.d("tag", "Inside Run Called");
drawFrame();
}
};
float mXOffset;
float mYOffset;
private float oldScreen;
Random random = new Random();
Resources res = Hearts.this.getResources();
HeartsEngine() {
super();
Log.d("tag", "Constructyor");
}
void drawFrame() {
Log.d("tag", "drawFrame");
this.oldScreen -= this.mXOffset;
Flake.scrollSpeed = 48.0F * this.oldScreen;
this.oldScreen = this.mXOffset;
this.c = null;
this.c = this.holder.lockCanvas();
try {
if (this.c != null)
this.c.drawBitmap(Hearts.this.background, 0.0F, 0.0F, null);
for (int i = 0;; i++) {
if (i >= 40) {
this.holder.unlockCanvasAndPost(this.c);
Hearts.this.mHandler.postDelayed(this.mDrawCube, 16L);
}
this.flakeArray[i].updateFlake(this.random, this.res,
this.c);
}
} catch (IllegalArgumentException localIllegalArgumentException) {
// break label94;
localIllegalArgumentException.printStackTrace();
return;
}
}
You call drawFrame() only inside the mDrawCube Runnable. So there is no a first time call for the drawFrame() method
I try to display one from list of bitmaps during onDraw.
When i'm passing list to the canvas all are display and stay in their places.
When I pass one random bitmaps it's redrawing canvas all the time.
All works when i'm using public void drawEnemy(Canvas canvas) but not exactly like I want when using public void drawEn(Canvas canvas).
I want to display one random bitmap, then after a few seconds, delete it and display other bitmap. I think the problem is how I implemented onDrow() method. It's redrawing canvas all the time.
Activity:
public class NewGameActivity extends Activity{
NewGame newgame;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
// Landscape mode
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
// no title
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
// content Newgame.java
newgame = new NewGame(this);
setContentView(newgame);
}
Thread:
public class MainThread extends Thread{
private SurfaceHolder surfaceHolder;
private NewGame screen;
public MainThread(SurfaceHolder surfaceHolder, NewGame ekran) {
super();
this.surfaceHolder = surfaceHolder;
this.screen= screen;
}
private boolean running;
public void setRunning(boolean running) {
this.running = running;
}
#Override
public void run() {
Canvas canvas;
while (running) {
canvas = null;
try {
canvas = this.surfaceHolder.lockCanvas();
synchronized (surfaceHolder) {
this.screen.onDraw(canvas);
}
} finally {
if (canvas != null) {
surfaceHolder.unlockCanvasAndPost(canvas);
}
}
}
}
}
SurfaceView:
public class NewGame extends SurfaceView implements SurfaceHolder.Callback{
private MainThread thread;
private EnemyManager manager;
public NewGame(Context context) {
super(context);
getHolder().addCallback(this);
thread = new MainThread(getHolder(), this);
manager = new EnemyManager();
// TODO Auto-generated constructor stub
//adding enemy
Enemy e1 = new Enemy(BitmapFactory.decodeResource(getResources(), R.drawable.card), 1);
Enemy e2 = new Enemy(BitmapFactory.decodeResource(getResources(), R.drawable.horse), 2);
EnemyLocation l1 = new EnemyLocation(60, 180);
EnemyLocation l2 = new EnemyLocation(60, 50);
manager.AddEnemy(e1, l1);
manager.AddEnemy(e2, l2);
setFocusable(true);
}
#Override
protected void onDraw(Canvas canvas) {
canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.saloon), 0, 0, null);
manager.drawEn(canvas);
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
thread.setRunning(true);
thread.start();
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
thread.setRunning(false);
thread.stop();
}
#Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
manager.handleActionDown((int)event.getX(), (int)event.getY());
}
return true;
}
}
EnemyManager:
public class EnemyManager {
private ArrayList<Enemy> enemyList;
private ArrayList<Enemy> suspects;
private Enemy cow;
private String message;
private int suspectID;
private Random rnd;
public String getMessage() {
return message;
}
public EnemyManager(){
enemyList = new ArrayList<Enemy>();
suspects = new ArrayList<Enemy>();
}
public void AddEnemy(Enemy enemy, EnemyLocation loc){
// set x,y enemy localization
enemy.setX(loc.getX());
enemy.setY(loc.getY());
enemyList.add(enemy);
}
public void clearEnemy() {
enemyList.clear();
}
// message if enemy touched
public void handleActionDown(int x, int y) {
for (Enemy enemy: enemyList) {
if (enemy.wasTouched(x, y)) {
message = enemy.getId();
return;
}
}
}
public void PrepareEnemy(){
suspectID = enemyList.get(rnd.nextInt(enemyList.size()+1)).getId();
suspects = new ArrayList<Enemy>();
suspects.add(getSuspectByID(suspectID));
}
private Enemy SingleEnemy(){
Double i = 1 + Math.random() * ((enemyList.size()-1)+1);
cow = getSuspectByID(i.intValue());
return cow;
}
private Enemy getSuspectByID(int suspectID) {
for (Enemy s: enemyList) {
if (s.getId() == suspectID) {
return s;
}
}
return null;
}
public void drawEn(Canvas canvas){
try {
Enemy k = SingleEnemy();
canvas.drawBitmap(cow.picture, cow.x, cow.y, null);
} catch (Exception e) {
// TODO: handle exception
}
}
// draw enemy
public void drawEnemy(Canvas canvas) {
try {
for (Enemy enemy: enemyList) {
canvas.drawBitmap(enemy.picture, enemy.x, enemy.y, null);
}
} catch (Exception e) {
// TODO: handle exception
}
}
}
das
As for as understand you are trying to do something like this (if it's not, please correct me):
This is rendering the canvas with all components:
Draw background
Draw enemy
To "refresh" the canvas you simply do something like this:
Draw background
Update
To pause the rendering you could do something like this:
int lastUpdateTime;
int delayTime = 2000; 2 seconds
if(System.currenttimeMillis() > lastUpdateTime + delayTime) {
// Finished waiting
}
You should only define lastUpdateTime when you want to wait and not in every iteration.
NB: Don't call Thread.sleep() in a rendering thread!
This might seem like a silly qustion but how do you change the picture that draws on the screen.I have already been able to program a app were it draws a little icon where you touch the screen.So natually after I completed that I want to make it better by adding a option menu and the ability to change what icon you were being drown but when I ran the code the icon picture stayed the same.When I looked at it I found that when you click on any of the menu item it does do it's job and change the image id but when you go back to the main screen and try to create a new image it revertes back to the old image.I have no idea why it doesn't change because when I look at it everything make sense for it to change icon properly.If any one has any idea on what i am doing wrong or any suggestion on how to do this it would be greatly appreciate
Main
public class main extends Activity {
/** Called when the activity is first created. */
MenuItem item2;
int item3=R.drawable.ic_launcher;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FrameLayout sv = new FrameLayout(this);
LinearLayout ll = new LinearLayout(this);
Panel test = new Panel(this);
//ImageButton button = new ImageButton(this);
ll.setOrientation(LinearLayout.VERTICAL);
sv.addView(test);
//ll.addView(button);
sv.addView(ll);
setContentView(sv);
}
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
Log.v("test", "item3 before is: "+item3);
item3=R.drawable.box;
Log.v("test", "item3 after is: "+item3);
return super.onOptionsItemSelected(item);
}
}
Panel
public class Panel extends SurfaceView implements SurfaceHolder.Callback {
private Bitmap image;
private ViewThread mThread;
private int x;
private int y;
private ArrayList<Element> mElements = new ArrayList<Element>();
public Panel(Context context) {
super(context );
image = BitmapFactory.decodeResource(getResources(),yantz.imageapp4.R.drawable.test);
getHolder().addCallback(this);
mThread = new ViewThread(this);
}
public void doDraw(Canvas canvas) {
canvas.drawColor(Color.CYAN);
canvas.drawBitmap(image, x, y, null);
synchronized (mElements){
for(Element element : mElements){
element.doDraw(canvas);
}
}
}
#Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
Log.v("test", "you have touched the sreen: ");
synchronized (mElements){
mElements.add(new Element(getResources(),(int) event.getX(),(int) event.getY()));
}
return super.onTouchEvent(event);
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
// TODO Auto-generated method stub
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
if (!mThread.isAlive()) {
mThread = new ViewThread(this);
mThread.setRunning(true);
mThread.start();
}
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
if (mThread.isAlive()) {
mThread.setRunning(false);
}
}
}
Elements
public class Element extends main{
private int mX;
private int mY;
int location ;
private Bitmap mBitmap;
public Element(Resources res, int x, int y) {
Log.v("element", "item3 before location is: "+item3);
location =item3;
mBitmap = BitmapFactory.decodeResource(res, location);
mX = x - mBitmap.getWidth() / 2;
mY = y - mBitmap.getHeight() / 2;
Log.v("element", "item3 before location is: "+item3);
}
public void doDraw(Canvas canvas) {
canvas.drawBitmap(mBitmap, mX, mY, null);
}
public void setlocation(int location2){
location=location2;
}
}
ViewThread
public class ViewThread extends Thread {
private Panel mPanel;
private SurfaceHolder mHolder;
private boolean mRun = false;
public ViewThread(Panel panel) {
mPanel = panel;
mHolder = mPanel.getHolder();
}
public void setRunning(boolean run) {
mRun = run;
}
#Override
public void run() {
Canvas canvas = null;
while (mRun) {
canvas = mHolder.lockCanvas();
if (canvas != null) {
mPanel.doDraw(canvas);
mHolder.unlockCanvasAndPost(canvas);
}
}
}
}
you can use
#Override
protected void onResume() {
super.onResume();
id="what ever you want";
//and set it to imagevIew;
}
if i have understood the uestion correctly,this happens because your activity pauses when it is not focused and resumes with default values.