How to test the touch screen like this - android

I want to create an app to test the touch screen as shown in the above image.
My code for creating the layout as shown in the image is as follows :
private void fillScreen() {
try {
for (int i = 1; i < 14; i++) {
layoutMain = new LinearLayout(getApplicationContext());
for (int j = 1; j < 14; j++) {
b = new Button(getApplicationContext());
b.setTag("btn" + i + j);
b.setBackgroundColor(getResources().getColor(R.color.orange));
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
b.setBackgroundDrawable(getResources().getDrawable(R.drawable.my_button_bg));
} else {
b.setBackground(getResources().getDrawable(R.drawable.my_button_bg));
}
b.setOnTouchListener(getOnClickDoSomething(b));
b.setLayoutParams(param);
layoutMain.addView(b);
if (i == j || j == (15 - i - 1) || j == 13 || j == 1 || i == 1 || i == 13) {
b.setVisibility(View.VISIBLE);
}
else {
b.setVisibility(View.INVISIBLE);
}
}
layoutMain.setLayoutParams(layoutParams);
layoutButtons.addView(layoutMain);
}
} catch (Exception e) {
Log.e("promptMessage,RotationActivity Exception", e.toString());
}
}
View.OnTouchListener getOnClickDoSomething(final Button btn) {
return new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (!buttons.contains(btn.getTag())) {
buttons.add(btn.getTag());
btnCounter++;
}
btn.setBackgroundColor(getResources().getColor(R.color.videocon_header_green));
if ((btnCounter) == 67) {
flag++;
if (flag == 2)
promptMessage("Touch Screen test pass?");
}
return false;
}
};
}
I can change the colors of the button on touch but i want to "change the colors of the button when i swipe my fingers on the screen to left right top bottom".

Related

How can I make each individual frog stop using onTouch?

I am making a game where there there are frogs hopping around the screen. Once a frog is touched, I change the game image to what I have set as "deadFrog" and its movement stops. I have them all created under and array-list, and I am unsure as to how to only make changes to the individual frog. Right now, if one frog is tapped, all of the frogs stop moving and change to deadFrog. Hopefully you can help me fix the tactical nuke of a tap ;) *If you need any more information, just comment and I'll be sure to provide it!
Edit Is there not a way to access a single element in the blocks arraylist? I've tried doing blocks(1) for example but that's invalid.
Here is where the frogs are declared:
public void init() {
blocks = new ArrayList<Block>();
for (int i = 0; i < 5; i++) {
Block b = new Block(i * 200, MainActivity.GAME_HEIGHT - 95,
BLOCK_WIDTH, BLOCK_HEIGHT);
blocks.add(b);
tapped = false;
}
}
They are rendered with this:
private void renderFrogs(Painter g) {
if (!tapped) {
for (int i = 0; i < blocks.size(); i++) {
Block b = blocks.get(i);
if (b.isVisible()) {
Assets.runAnim.render(g, (int) b.getX(), (int) b.getY());
}
}
}
if (tapped) {
for (int i = 0; i < blocks.size(); i++) {
Block b = blocks.get(i);
if (b.isVisible()) {
g.drawImage(Assets.deadfrog, (int) b.getX(), (int) b.getY());
}
}
}
}
And this is the onTouchListener:
public boolean onTouch(MotionEvent e, int scaledX, int scaledY) {
if (e.getAction() == MotionEvent.ACTION_DOWN) {
recentTouchY = scaledY;
} else if (e.getAction() == MotionEvent.ACTION_UP) {
for (int i = 0; i < blocks.size(); i++) {
Block b = blocks.get(i);
if ((scaledY >= b.getY() - BLOCK_HEIGHT || scaledY <= b.getY()) && (scaledX >= b.getX() || scaledX <= b.getX() + BLOCK_WIDTH)) {
tapped = true;
}
}
}
return true;
}
Of course all frogs turn dead, you are supposed to keep "tapped" variable for each frog, your tapped variable is for all frogs at once.
Declare a class
public class Frog extends View{
public Drawable liveFrog;
public Drawable deadFrog;
public boolean isDead;
public Point location;
public int width;
public int height;
public Frog(Context context, int x, int y,int width,int height){
super(context);
this.isDead = false;
this.location = new Point(x,y);
this.width = width;
this.height = height;
}
public void onDraw(Canvas c){
super.onDraw(c);
if(!isDead){
//draw live frog at x,y
}else {
//draw dead frog at x,y
}
}
}
then you array is supposed to contain frogs
public void init(Context context) {
blocks = new ArrayList<Frog>();
for (int i = 0; i < 5; i++) {
Frog b = new Frog(context,i * 200, MainActivity.GAME_HEIGHT - 95,
BLOCK_WIDTH, BLOCK_HEIGHT);
blocks.add(b);
}
}
private void renderFrogs() {
for(Frog f : blocks){
//cause redraw
f.invalidate();
}
}
here comes the fun part, when you tap the frog
public boolean onTouch(MotionEvent e, int scaledX, int scaledY) {
if (e.getAction() == MotionEvent.ACTION_DOWN) {
recentTouchY = scaledY;
} else if (e.getAction() == MotionEvent.ACTION_UP) {
for (int i = 0; i < blocks.size(); i++) {
Frog frog = blocks.get(i);
if ((scaledY >= frog.getY() - BLOCK_HEIGHT || scaledY <= frog.getY()) && (scaledX >= frog.getX() || scaledX <= frog.getX() + BLOCK_WIDTH)) {
frog.isDead = true;
//cause one frog redraw
frog.invalidate();
//if the event was handled, stop here (unless you can have multiple frogs one on top of the other ?
return true;
}
}
}
//if the event was not handled, let it bubble up
return false;
}
From what I can see, your "tapped" boolean is not a property of EACH frog. It is declared once, and when triggered, per your for loop, will make every frog dead (obviously, since that's what you're experiencing).
Once "tapped" is true, your for loop is going through every block and assigning a dead frog to it.
I think you need to create a Frog class, and store instances of those in your ArrayList. A variable of the new frog class will be "touched", and when that is triggered you will do something to that specific instance only.

redraw bitmap on the android canvas

In my app, I want to switch a bitmap image from one to another when I touch on that image. I keep getting NULLPOINTEREXCEPTION whenever I touch the image.
Is there any method that allows to switch from a bitmap to other bitmap?
#Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.BLACK);
this.cVas = canvas;
for(int i = 0; i < BSIZE; i++) {
for(int j = 0; j < BSIZE; j++) {
gameBoard[i][j] = new Cell(i*wCell, j*wCell+hCell);
if(gameBoard[i][j].getBitmap() == null) {
gameBoard[i][j].setBitmap(b3);
}
/*if(gameBoard[i][j].getTouch()) {
gameBoard[i][j].setBitmap(b4);
}else{
gameBoard[i][j].setBitmap(b3);
}*/
gameBoard[i][j].drawCell(canvas);
}
}
}
#Override
public boolean onTouchEvent(MotionEvent event) {
if(event.getAction() != MotionEvent.ACTION_DOWN) {
return super.onTouchEvent(event);
}
// Get the Cell's position.
selX = (int)((event.getX()) / wCell);
selY = (int)((event.getY() - hCell) / wCell);
if( selX > BSIZE || selY > BSIZE || selY < 0) {
return false;
}
else {
Log.d(TAG, "selX: " + selX + " selY: " + selY);
cellSelection(selX, selY);
}
return true;
}
// Redraw the image (switch to other bitmap).
private void cellSelection( int x, int y) {
if(gameBoard[x][y].getTouch()) {
gameBoard[x][y].setTouch(false);
gameBoard[x][y].setBitmap(b3);
} else {
gameBoard[x][y].setTouch(true);
gameBoard[x][y].setBitmap(b4);
}
gameBoard[x][y].drawCell(cVas);
invalidate();
}

View not displayed in my application

I Am developing android abacus application, in that i need to develope the user interface like the following and i am using the following logic. It compiles and executes without exceptions but the view is not visible.
So, please guide me how develope this.
**MainActivity**
public class Hello extends Activity implements SensorListener
{
private MyView myView;
private SensorManager sensorManager;
public void onAccuracyChanged(int paramInt1, int paramInt2)
{
}
public void onCreate(Bundle paramBundle)
{
super.onCreate(paramBundle);
Log.e("hello", "hello");
this.myView = new MyView(this);
setContentView(this.myView);
Log.e("hello", "hello after constructor");
this.sensorManager = ((SensorManager)getSystemService("sensor"));
this.myView.setTheme(2);
}
protected void onResume()
{
super.onResume();
this.sensorManager.registerListener(this, 3, 0);
}
public void onSensorChanged(int paramInt, float[] paramArrayOfFloat)
{
switch (paramInt)
{
default:
case 1:
case 2:
}
this.myView.resetTama();
do
{
do
return;
while (Math.abs(paramArrayOfFloat[2]) <= 50.0F);
}
while (Math.abs(paramArrayOfFloat[0]) <= 15.0F);
}
public void onWindowFocusChanged(boolean paramBoolean)
{
super.onWindowFocusChanged(paramBoolean);
if (paramBoolean)
this.myView.init();
}
}
**MyView class**
public class MyView extends RelativeLayout
{
private final int FP = -1;
private final int WC = -2;
public boolean disableApplication = false;
private int imgType = 1;
public boolean isBlack = false;
boolean isInitialized;
private Bitmap myBitmap;
private Paint myPaint = new Paint();
private int[] numbers;
private Tama[] oyatamaArray;
private Tama[][] tamaArray;
public MyView(Context paramContext)
{
super(paramContext);
Log.e("MyView", "MyView");
setFocusable(true);
setBackgroundResource(R.drawable.haikei);
this.myBitmap = BitmapFactory.decodeResource(getContext().getResources(), R.drawable.tama);
this.numbers = new int[6];
//for displaying numbers
for (int i = 0; i<numbers.length; i++)
{
if (i >= this.numbers.length)
{
loadChangeThemeButton(paramContext);
new TextView(paramContext).setBackgroundResource(R.drawable.bar);
return;
}
this.numbers[i] = 0;
}
}
private void changeBackgroundImage()
{
int i = 1 + this.imgType;
this.imgType = i;
if (i > 3)
this.imgType = 1;
setTheme(this.imgType);
}
private void loadChangeThemeButton(Context paramContext)
{
ImageButton localImageButton = new ImageButton(paramContext);
localImageButton.setBackgroundResource(R.drawable.themebutton);
RelativeLayout.LayoutParams localLayoutParams = new RelativeLayout.LayoutParams(-2, -2);
localLayoutParams.addRule(9);
localLayoutParams.addRule(12);
localLayoutParams.setMargins(0, 0, 30, 30);
addView(localImageButton, localLayoutParams);
localImageButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View paramView)
{
MyView.this.changeBackgroundImage();
}
});
}
private Bitmap returnNumberImg(Resources paramResources, int paramInt)
{
switch (paramInt)
{
default:
return BitmapFactory.decodeResource(paramResources, R.drawable.n0);
case 1:
return BitmapFactory.decodeResource(paramResources, R.drawable.n1);
case 2:
return BitmapFactory.decodeResource(paramResources, R.drawable.n2);
case 3:
return BitmapFactory.decodeResource(paramResources, R.drawable.n3);
case 4:
return BitmapFactory.decodeResource(paramResources, R.drawable.n4);
case 5:
return BitmapFactory.decodeResource(paramResources, R.drawable.n5);
case 6:
return BitmapFactory.decodeResource(paramResources, R.drawable.n6);
case 7:
return BitmapFactory.decodeResource(paramResources, R.drawable.n7);
case 8:
return BitmapFactory.decodeResource(paramResources, R.drawable.n8);
case 9:
}
return BitmapFactory.decodeResource(paramResources, R.drawable.n9);
}
private void settingTamasTouchX(int paramInt1, int paramInt2)
{
int i = 0;
if (i >= this.oyatamaArray.length);
int k;
for (int j = 0; ; j++)
{
if (j >= this.tamaArray.length)
{
return;
}
k = 0;
if (k < this.tamaArray[j].length)
{
if (this.tamaArray[j][k].checkArea(paramInt1, paramInt2));
for (this.tamaArray[j][k].touchX = paramInt1; ; this.tamaArray[j][k].touchX = 0)
{
k++;
break;
}
// break label90;
}
}
}
public void init()
{
int i = getHeight() / 7;
int[] arrayOfInt = new int[6];
int j = 5;
//if (j < 0){
this.oyatamaArray = new Tama[6];
//}
int m;
int i1 = 0;
for (int k = 0; ; k++)
{
if (k >= this.oyatamaArray.length)
{
this.tamaArray = ((Tama[][])Array.newInstance(Tama.class, new int[] { 6, 4 }));
m = 0;
if (m < this.tamaArray.length)
{
for (int n = 0; ; n++)
{
if (n >= this.tamaArray[m].length)
{
m++;
break;
}
this.tamaArray[m][n] = new Tama(40 + n * 37, arrayOfInt[m] - 32, n + m * 10);
}
// break label154;
}
i1 = 0;
if (i1 < this.tamaArray.length)
{
for (int i2 = 0; ; i2++)
{
if (i2 >= this.tamaArray[i1].length)
{
i1++;
break;
}
if (i2 != this.tamaArray[i1].length - 1)
this.tamaArray[i1][i2].ueTama = this.tamaArray[i1][(i2 + 1)];
if (i2 == 0)
continue;
this.tamaArray[i1][i2].shitaTama = this.tamaArray[i1][(i2 - 1)];
}
// break label222;
}
this.isInitialized = true;
return;
}
this.oyatamaArray[k] = new Tama(279, arrayOfInt[k] - 32, k);
this.oyatamaArray[k].isOya = true;
}
}
#Override
protected void onDraw(Canvas paramCanvas)
{
Log.e("this is",".........onDraw");
if (!this.isInitialized)
init();
int i = 0;
Resources localResources;
int m;
while (true)
{
int j;
if (i >= this.oyatamaArray.length)
{
j = 0;
if (j >= this.tamaArray.length)
{
localResources = getContext().getResources();
m = 0;
if (m < 7)
break;
return;
}
}
else
{
paramCanvas.drawBitmap(this.myBitmap, this.oyatamaArray[i].getX(), this.oyatamaArray[i].getY(), this.myPaint);
i++;
continue;
}
for (int k = 0; ; k++)
{
if (k >= this.tamaArray[j].length)
{
j++;
break;
}
paramCanvas.drawBitmap(this.myBitmap, this.tamaArray[j][k].getX(), this.tamaArray[j][k].getY(), this.myPaint);
}
}
if (this.numbers.length <= m)
{
for (Bitmap localBitmap = BitmapFactory.decodeResource(localResources, R.drawable.space); ; localBitmap = returnNumberImg(localResources, this.numbers[m]))
{
paramCanvas.drawBitmap(localBitmap, 8.0F, getHeight() / 2 + m * 18, this.myPaint);
m++;
break;
}
}
}
public boolean onTouchEvent(MotionEvent paramMotionEvent)
{
int i = (int)paramMotionEvent.getX();
int j = (int)paramMotionEvent.getY();
if ((paramMotionEvent.getAction() == 0) && (i >= 0) && (i <= 40) && (410 <= j) && (j <= 430))
changeBackgroundImage();
if (paramMotionEvent.getAction() == 0)
settingTamasTouchX(i, j);
if (1 == paramMotionEvent.getAction())
settingTamasTouchX(0, 0);
int k;
int m = 0;
if (2 == paramMotionEvent.getAction())
{
k = 0;
if (k >= this.oyatamaArray.length)
{
m = 0;
if (m < this.tamaArray.length)
{
for (int n = 0; ; n++)
{
if (n >= this.tamaArray[m].length)
{
m++;
break;
}
this.tamaArray[m][n].checkAndSetArea(i, j);
if (!this.tamaArray[m][n].isUp)
continue;
int[] arrayOfInt = this.numbers;
arrayOfInt[m] = (1 + arrayOfInt[m]);
}
// break label164;
}
invalidate();
}
}
else
{
return true;
}
this.oyatamaArray[k].checkAndSetArea(i, j);
if (this.oyatamaArray[k].isUp)
this.numbers[k] = 5;
while (true)
{
k++;
break;
}
// label164:
return disableApplication;
}
public void resetTama()
{
int j;
for (int i = 0; ; i++)
{
if (i >= this.oyatamaArray.length)
{
j = 0;
if (j < this.tamaArray.length)
break;
invalidate();
return;
}
this.oyatamaArray[i].moveX(this.oyatamaArray[i].startX);
this.oyatamaArray[i].isUp = false;
this.numbers[i] = 0;
}
for (int k = 0; ; k++)
{
if (k >= this.tamaArray[j].length)
{
j++;
break;
}
this.tamaArray[j][k].moveX(this.tamaArray[j][k].startX);
this.tamaArray[j][k].isUp = false;
}
}
public void sensorChange(float[] paramArrayOfFloat)
{
int i = 0;
if (paramArrayOfFloat[2] < -50.0F)
i = 0 - 3;
int k;
while (true)
{
int j = 0;
if (j >= this.oyatamaArray.length)
{
k = 0;
if (k < this.tamaArray.length)
break;
if (i != 0)
invalidate();
return;
}
else
{
this.oyatamaArray[j].moveX(i + this.oyatamaArray[j].getX());
if (this.oyatamaArray[j].isUp)
this.numbers[j] = 5;
while (true)
{
j++;
break;
}
}
}
for (int m = 0; ; m++)
{
if (m >= this.tamaArray[k].length)
{
k++;
break;
}
this.tamaArray[k][m].moveX(i + this.tamaArray[k][m].getX());
if (!this.tamaArray[k][m].isUp)
continue;
int[] arrayOfInt = this.numbers;
arrayOfInt[k] = (1 + arrayOfInt[k]);
}
}
public void setTheme(int paramInt)
{
Resources localResources = getContext().getResources();
if (paramInt == 1)
{
this.myBitmap = BitmapFactory.decodeResource(localResources, R.drawable.tama);
setBackgroundResource(R.drawable.haikei);
}
while (true)
{
invalidate();
// return;
if (paramInt == 2)
{
this.myBitmap = BitmapFactory.decodeResource(localResources, R.drawable.tama2);
setBackgroundResource(R.drawable.haikei1);
continue;
}
this.myBitmap = BitmapFactory.decodeResource(localResources, R.drawable.tama3);
setBackgroundResource(R.drawable.haikei1);
}
}
}
Try this (instead of the setContentView )
this.myView = new MyView(this);
this.myView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
this.addContentView(myView, new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));

TextView onTouchEvent within customview

I have a custom view, and within the custom view I create 4 text views. I want each of these text views to respond to on touch events. However, the textviews themselves do not respond to an on touch event. I can however, make an ontouch listener for the entire custom view. However, I do not want the whole view to have an ontouch event because I want the textviews that I have created to be dragged and dropped. I tried going the route of registering the x and y coordinates of the ontouch event and assuming that if the ontouch event is within the bounds of a textview, to change the coordinates of the textview, but that was overly complicated because if one textview got dragged to the coordinates of another one, then the ontouch event would "pick up" the other textview, so then I'd be moving two text views, which is not what I want. So, to sum it up, I want to know if it's possible to set in ontouch listener for a textview inside a customview, and if possible, why it is not working:
mScale.mPositive.get(0).setOnTouchListener(new OnTouchListener()
{
#Override
public boolean onTouch(View v, MotionEvent event)
{
mScale.mCurrentXPos[0] = event.getX();
mScale.mCurrentYPos[0] = event.getY();
mScale.mDrag = true;
return true;
}
});
This same code works for the custom view, but not for the specific textviews inside that customview.
Here is the custom view code:
public class Scale extends View
{
public Scale(Context context, AttributeSet attrs)
{
super(context, attrs);
mContext = this.getContext();
h = new Handler();
mCalendarDbHelper=new CalendarDbAdapter(mContext);
mCalendarDbHelper.open();
Cursor thoughts = mCalendarDbHelper.fetchThoughts();
//create a string array of negative thoughts from the db
while (thoughts.moveToNext())
{
if (thoughts.getString(thoughts.getColumnIndexOrThrow(CalendarDbAdapter.COLUMN_NAME_THOUGHT)).length() > 0 && thoughts.getString(thoughts.getColumnIndexOrThrow(CalendarDbAdapter.COLUMN_NAME_THOUGHT)).charAt(0) == '-')
{
negative_thoughts.add(thoughts.getString(thoughts.getColumnIndexOrThrow(CalendarDbAdapter.COLUMN_NAME_THOUGHT)));
}
}
thoughts.close();
array_size = negative_thoughts.size();
mBag =BitmapFactory.decodeResource(getResources(), R.drawable.bag);
mGreenBag = BitmapFactory.decodeResource(getResources(), R.drawable.green_bag);
for (int i = 0; i < 72; i ++)
{
try
{
mScale[i] = BitmapFactory.decodeStream(context.getAssets().open("scale_"+i+".gif"));
}
catch (IOException e)
{
}
}
}
private Runnable r= new Runnable()
{
#Override
public void run() {
invalidate();
}
};
protected void onDraw (Canvas canvas)
{
if (first == true)
{
width = this.getWidth();
height = this.getHeight();
mScale[i] = Bitmap.createScaledBitmap(mScale[i], (int) (width * 1.5), height, true);
mBag = Bitmap.createScaledBitmap(mBag, width/2, height/2, true);
negative = new TextView(mContext);
word = negative_thoughts.get((int) (Math.random() * array_size));
negative.setText(word);
negative.layout(0, 0, width/3, height/4);
negative.setGravity(Gravity.CENTER);
negative.setTextSize(15);
negative.setTextColor(Color.BLACK);
negative.setTypeface(Typeface.DEFAULT_BOLD);
negative.setShadowLayer(5, 2, 2, Color.WHITE);
negative.setDrawingCacheEnabled(true);
negative.setBackgroundResource(R.drawable.graycloud);
positive_paint.setColor(Color.parseColor("#FF4444"));
positive_paint.setShadowLayer(5, 2, 2, Color.YELLOW);
positive_paint.setTypeface(Typeface.DEFAULT_BOLD);
positive_paint.setTextSize(25);
mCurrentXPos[0] = (width/2);
mCurrentYPos[0] = height/4;
mCurrentXPos[1] = (width/2) + (width/8);
mCurrentYPos[1] = height/6;
mCurrentXPos[2] = width/2;
mCurrentYPos[2] = height/12;
mCurrentXPos[3] = (width/2) + (width/8);
mCurrentYPos[3] = height/18;
mMoveXPos[0] = ((width/2) - width)/FRAME_RATE;
mMoveYPos[0] = ((height/4) - (height + (height/4)))/FRAME_RATE;
mMoveXPos[1] = (((width/2) + (width/8)) - width)/ FRAME_RATE;
mMoveYPos[1] = ((height/6) - (height + (height/4)))/FRAME_RATE;
mMoveXPos[2] = ((width/2) - width)/ FRAME_RATE;
mMoveYPos[2] = ((height/12) - (height + (height/4)))/FRAME_RATE;
mMoveXPos[3] = (((width/2) + (width/8)) - width)/ FRAME_RATE;
mMoveYPos[3] = ((height/18) - (height + (height/4)))/FRAME_RATE;
mMoveByXPos[0] = -(width/2)/ FRAME_RATE;
mMoveByYPos[0] = -(height/4)/FRAME_RATE;
mMoveByXPos[1] = ((width - (width/3)) - (width/2 + (width/8)))/ FRAME_RATE;
mMoveByYPos[1] = -(height/6)/FRAME_RATE;
mMoveByXPos[2] = - (width/2)/ FRAME_RATE;
mMoveByYPos[2] = ((height) - (height/12))/FRAME_RATE;
mMoveByXPos[3] = ((width - (width/3)) - (width/2 + (width/8)))/ FRAME_RATE;
mMoveByYPos[3] = ((height) - (height/18))/FRAME_RATE;
currentX = width;
currentY = height + height/4;
first = false;
}
if (game_over == false)
{
canvas.drawBitmap(mScale[i], 0 - (width/4), 0, null);
canvas.drawBitmap(negative.getDrawingCache(),(int) (width/12), (int) (height - (height)/2.5) - (j), null);
}
else
{
canvas.drawBitmap(mBag, width/4, height/4, null);
}
if (mMoveScale == true)
{
i++;
j+=3;
ScaleIt(canvas, i);
if (i == 21 || i == 37 || i == 53 || i == 71)
{
mMoveScale = false;
}
}
if (tracker > 0)
{
if (tracker == 1)
{
if (currentX > width/2 && currentY > height/4 && sStop == false)
{
currentX += mMoveXPos[0];
currentY += mMoveYPos[0];
canvas.drawBitmap(mPositive.get(tracker -1 ).getDrawingCache(), currentX, currentY, null);
}
else
{
if (sStop == false)
{
mMoveScale = true;
sStop = true;
currentX = width;
currentY = height + height/4;
draw_em++;
}
}
}
if (tracker == 2)
{
if (currentX > width/2 + (width/8) && currentY > (height/6) && sStop == false)
{
currentX += mMoveXPos[1];
currentY += mMoveYPos[1];
canvas.drawBitmap(mPositive.get(tracker -1 ).getDrawingCache(), currentX, currentY, null);
}
else
{
if (sStop == false)
{
mMoveScale = true;
sStop = true;
currentX = width;
currentY = height + height/4;
draw_em++;
}
}
}
if (tracker == 3)
{
if (currentX > width/2 && currentY > height/12 && sStop == false)
{
currentX += mMoveXPos[2];
currentY += mMoveYPos[2];
canvas.drawBitmap(mPositive.get(tracker -1 ).getDrawingCache(), currentX, currentY, null);
}
else
{
if (sStop == false)
{
mMoveScale = true;
sStop = true;
currentX = width;
currentY = height + height/4;
draw_em++;
}
}
}
if (tracker == 4)
{
if (currentX > width/2 + (width/8) && currentY > (height/18) && sStop == false)
{
currentX += mMoveXPos[3];
currentY += mMoveYPos[3];
canvas.drawBitmap(mPositive.get(tracker -1 ).getDrawingCache(), currentX, currentY, null);
}
else
{
if (sStop == false)
{
mMoveScale = true;
sStop = true;
game_over = true;
currentX = width;
currentY = height + height/4;
draw_em++;
}
}
}
if (draw_em > 0 && game_over == false)
{
for (int i = 0; i < draw_em; i ++)
{
if (i == 0)
{
canvas.drawBitmap(mPositive.get(i).getDrawingCache(), width/2, height/4 + j, null);
}
if (i == 1)
{
canvas.drawBitmap(mPositive.get(i).getDrawingCache(), width/2 + (width/8), height/6 + j, null);
}
if (i == 2)
{
canvas.drawBitmap(mPositive.get(i).getDrawingCache(), width/2, height/12 + j, null);
}
if (i == 3)
{
canvas.drawBitmap(mPositive.get(i).getDrawingCache(), width/2 + (width/8), height/18 + j, null);
}
}
}
else if (game_over == true)
{
for (int i = 0; i < draw_em; i++)
{
if (i == 0 && mCurrentXPos[0] > 0 && mCurrentYPos[0] > 0 && mDrag == false)
{
mCurrentXPos[0] += mMoveByXPos[0];
mCurrentYPos[0] += mMoveByYPos[0];
canvas.drawBitmap(mPositive.get(i).getDrawingCache(), mCurrentXPos[0], mCurrentYPos[0], null);
}
else if (i == 0 && mCurrentXPos[0] <= 0 || mCurrentYPos[0] <= 0 && mDrag == false)
{
canvas.drawBitmap(mPositive.get(0).getDrawingCache(), 0, 0, null);
}
else if (i == 0 && mDrag == true)
{
canvas.drawBitmap(mPositive.get(0).getDrawingCache(), mCurrentXPos[0], mCurrentYPos[0], null);
}
if (i == 1 && mCurrentXPos[1] < (width - (mPositive.get(i).getWidth()/2)) && mCurrentYPos[1] > mPositive.get(i).getHeight()/2)
{
mCurrentXPos[1] += mMoveByXPos[1];
mCurrentYPos[1] += mMoveByYPos[1];
canvas.drawBitmap(mPositive.get(i).getDrawingCache(), mCurrentXPos[1], mCurrentYPos[1], null);
}
else if (i == 1 && mCurrentXPos[1] >= (width - (mPositive.get(i).getWidth()/2)) || mCurrentYPos[1] <= mPositive.get(i).getHeight()/2)
{
canvas.drawBitmap(mPositive.get(1).getDrawingCache(), width - (width/3), 0, null);
}
if (i == 2 && mCurrentXPos[2] > (mPositive.get(i).getWidth()/2) && mCurrentYPos[2] < (height - mPositive.get(i).getHeight()/2))
{
mCurrentXPos[2] += mMoveByXPos[2];
mCurrentYPos[2] += mMoveByYPos[2];
canvas.drawBitmap(mPositive.get(i).getDrawingCache(), mCurrentXPos[2], mCurrentYPos[2], null);
}
else if (i == 2 && mCurrentXPos[2] <= (mPositive.get(i).getWidth()/2) || mCurrentYPos[2] >= (height - mPositive.get(i).getHeight()/2))
{
canvas.drawBitmap(mPositive.get(2).getDrawingCache(), 0, height - (height/4), null);
}
if (i == 3 && mCurrentXPos[3] < (width - (mPositive.get(i).getWidth()/2)) && mCurrentYPos[3] < (height - mPositive.get(i).getHeight()/2))
{
mCurrentXPos[3] += mMoveByXPos[3];
mCurrentYPos[3] += mMoveByYPos[3];
canvas.drawBitmap(mPositive.get(i).getDrawingCache(), mCurrentXPos[3], mCurrentYPos[3], null);
}
else if (i == 3 && mCurrentXPos[3] >= (width - (mPositive.get(i).getWidth()/2)) || mCurrentYPos[3] >= (height - mPositive.get(i).getHeight()/2))
{
canvas.drawBitmap(mPositive.get(3).getDrawingCache(), width - (width/3), height - (height/4), null);
}
}
}
}
h.postDelayed(r, FRAME_RATE);
}
protected void moveIt(Canvas canvas, int moveX,int moveY, int i)
{
if (i == 0)
{
canvas.drawBitmap(mPositive.get(i).getDrawingCache(), moveX, moveY, null);
}
if (i == 1)
{
canvas.drawBitmap(mPositive.get(i).getDrawingCache(), moveX, moveY, null);
}
if (i == 2)
{
canvas.drawBitmap(mPositive.get(i).getDrawingCache(), moveX, moveY, null);
}
if (i == 3)
{
canvas.drawBitmap(mPositive.get(i).getDrawingCache(), moveX, moveY, null);
}
}
protected void moveEm(Canvas canvas, int[]mMovePosX, int[] mMovePosY)
{
for (int i = 0; i < 4; i++)
{
}
}
protected void ScaleIt(Canvas canvas, int i)
{
mScale[i] = Bitmap.createScaledBitmap(mScale[i], (int) (width * 1.5), height, true);
mScale[i-1].recycle();
}
}
And here is the activity:
public class ScaleView extends Activity
{
Context mContext;
Scale mScale;
EditText positive_thought;
Button fire;
TextView pos;
private static Set<String> mPositiveWords;
private static Set<String> mNegativeWords;
int count;
private Pattern four_letter_words = Pattern.compile("not|cant|cnt|can't");
String inputLine;
private String[] inputTokens;
Button question;
Button skip;
public static boolean populatePositiveWords(Context context)
{
mNegativeWords = new HashSet<String>();
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(context.getAssets().open("negative_words.txt")));
String line = reader.readLine();
while (line != null)
{
mNegativeWords.add(line.toLowerCase(Locale.US));
line = reader.readLine();
}
reader.close();
}
catch (IOException exception)
{
return false;
}
return true;
//TODO list of negative words
}
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.getActionBar().hide();
mContext = this;
populatePositiveWords(mContext);
setContentView(R.layout.activity_scale);
mScale = (Scale) findViewById(R.id.anim_view);
mScale.setClickable(true);
positive_thought = (EditText) findViewById(R.id.thoughts);
fire = (Button) findViewById(R.id.scale_it);
skip = (Button) findViewById(R.id.skip);
question = (Button) findViewById(R.id.question);
InputFilter[] FilterArray = new InputFilter[1];
FilterArray[0] = new InputFilter.LengthFilter(60);
positive_thought.setFilters(FilterArray);
fire.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View view)
{
//if the button is clicked invalidate the ondraw method and pass in the text of the positive word
inputLine = positive_thought.getText().toString();
inputTokens = inputLine.split(" ");
if (inputLine.isEmpty())
{
Toast.makeText(mContext, "You have to write something!", Toast.LENGTH_SHORT).show();
return;
}
if (inputTokens.length < 3)
{
Toast.makeText(mContext, "At least three words are required.", Toast.LENGTH_SHORT).show();
return;
}
if (four_letter_words.matcher(inputLine).find() == true)
{
Toast.makeText(mContext, "Make an affirmative statement!", Toast.LENGTH_SHORT).show();
return;
}
boolean matchesToken = false;
for (int i = 0; i < inputTokens.length; i++)
{
String token = inputTokens[i];
if (mNegativeWords.contains(token.toLowerCase(Locale.US)))
{
matchesToken = true;
break;
}
}
if (matchesToken == true)
{
Toast.makeText(mContext, "Use positive words!", Toast.LENGTH_SHORT).show();
return;
}
else
{
InputMethodManager imm = (InputMethodManager)mContext.getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(positive_thought.getWindowToken(), 0);
pos = new TextView (mContext);
pos.layout(0, 0, mScale.width/3, mScale.height/4);
pos.setGravity(Gravity.CENTER);
pos.setTextSize(15);
pos.setTextColor(Color.RED);
pos.setTypeface(Typeface.DEFAULT_BOLD);
pos.setShadowLayer(5, 2, 2, Color.YELLOW);
pos.setText(positive_thought.getText().toString());
pos.setDrawingCacheEnabled(true);
pos.setBackgroundResource(R.drawable.whitecloud);
pos.setClickable(true);
mScale.mPositive.add(pos);
mScale.scale_it = true;
count++;
mScale.sStop = false;
mScale.tracker = count;
if (count == 4)
{
((RelativeLayout)question.getParent()).removeView(question);
((RelativeLayout)skip.getParent()).removeView(skip);
mScale.mPositive.get(0).setOnTouchListener(new OnTouchListener()
{
#Override
public boolean onTouch(View v, MotionEvent event)
{
mScale.mCurrentXPos[0] = event.getX();
mScale.mCurrentYPos[0] = event.getY();
mScale.mDrag = true;
return true;
}
});
}
}
positive_thought.setText(null);
}
});
}
}
The reason your TextView cannot receive touch events is that the TextView is drawn on the canvas just as a bitmap, not as a View. An excerpt from your code shown below illustrates this.
protected void onDraw(Canvas canvs)
{
....
negative = new TextView(mContext);
...
canvas.drawBitmap(negative.getDrawingCache(), ...)
To deliver touch events to your TextView, your Scale class should extend not View but ViewGroup and the TextView needs to be added as a subview to Scale class by using ViewGroup.addView() or addViewInLayout(). It is not a simple task to implement a ViewGroup subclass. You may have to implement onInterceptTouchEvent(MotionEvent) depending on your needs.
Android's source code will be of help.
Is your CustomView also sets some TouchListner??
If yes then this might causing issue..Remove TouchListner from CustomView and see if it works..

Character never stops from walking

I am creating a game which uses andendgine and here is my code:
Player stanley = new Player();
...
scene.registerUpdateHandler(new IUpdateHandler() {
public void onUpdate(float pSecondsElapsed) {
stanX = stanley.getX();
destX = x.getX();
if(destX < stanX){
if(hasMovedRight == 1){
stanley.stop();
hasMovedRight = 0;
}
else{
stanley.moveLeft();
hasMovedRight = 0
hasMovedLeft = 1;
}
}
if(destX > stanX){
if(hasMovedLeft == 1){
stanley.stop();
hasMovedLeft == 0;
}
else{
stanley.moveRight();
hasMovedLeft = 0;
hasMovedRight = 1;
}
}
}
}
what i want is to stop Player from walking whenever his position X is equal to the touched area X. The problem is it never stop from walking. Thanks!
Your if statements are missing an element where destX == stanX. and you should really use else if. See modified code below.
if(destX + 8 < stanX){
if(hasMovedRight == 1){
stanley.stop();
hasMovedRight = 0;
}
else{
stanley.moveLeft();
hasMovedRight = 0
hasMovedLeft = 1;
}
}
else if(destX - 8 > stanX){
if(hasMovedLeft == 1){
stanley.stop();
hasMovedLeft == 0;
}
else{
stanley.moveRight();
hasMovedLeft = 0;
hasMovedRight = 1;
}
}
else //makes stanley stop. (calls stop method), if at touched x.
{
stanley.stop();
hasMovedRight = 0;
hasMovedLeft = 0;
}
try this one
setOnSceneTouchListener(new IOnSceneTouchListener() {
#Override
public boolean onSceneTouchEvent(Scene scene, TouchEvent event) {
int touchX = (int) (event.getX() - (sCHARStanley.getWidth() / 2));
//so that your sprite will go to the touched part of the screen
}

Categories

Resources