我一直试图通过On Touch从表面视图调用活动。然而,随着我构建的代码(我已经尝试了所有可能的试验和错误,我是一个新手程序员),我一直点击表面视图,没有任何反应。
我非常需要一些帮助,这是一个学校项目:3
谢谢!
package com.projtimesequencesrc;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
public class SurfaceViewer extends View implements OnTouchListener {
private Context svc;
float x, y;
Bitmap bgd, main;
Bitmap dialog;
Rect dialogsrc, dialogdst;
//dog
Bitmap dog;
Rect dogsrc, dogdst;
Paint p;
//Molly
Bitmap molly;
Rect mollysrc, mollydst;
//Molly2
Bitmap youngstar;
Rect youngstarsrc, youngstardst;
//Bear
Bitmap bear;
Rect bearsrc, beardst;
Thread tbear, tdbear;
Runnable rbear, dbear;
//man1
Bitmap man1;
Rect mansrc, mandst;
// Main Character
Rect bgdst, charsrc, chardst;
Thread tstory, tdstory;
Runnable rstory, dstory;
protected void onDraw (Canvas c) {
super.onDraw(c);
bgdst.right=getWidth();
bgdst.bottom=getHeight();
c.drawBitmap(bgd, null, bgdst, p);
c.drawBitmap(main, charsrc,chardst, p);
c.drawBitmap(dog, dogsrc, dogdst, p);
c.drawBitmap(bear, bearsrc, beardst, p);
c.drawBitmap(man1, mansrc, mandst, p);
c.drawBitmap(dialog, dialogsrc, dialogdst, p);
c.drawBitmap(molly, mollysrc,mollydst, p);
c.drawBitmap(youngstar, youngstarsrc, youngstardst, p);
invalidate();
}
void sqyoungstar2() {
int count = 0;
while (true) {
youngstarsrc.left += 64;
youngstarsrc.right += 64;
if (youngstarsrc.left > (64*6)) {
youngstarsrc.left = 0;
youngstarsrc.right = 64;
}
SystemClock.sleep(200);
count++;
if (count > 38) {
break;
}
}
}
void sqyoungstar1() {
youngstarsrc.left = 0;
youngstarsrc.right = 64;
youngstarsrc.top = 64*2;
youngstarsrc.bottom = 64*3;
youngstardst.left = 280;
youngstardst.right = 330;
youngstardst.top = 200;
youngstardst.bottom = 250;
sqyoungstar2();
}
void seq4() {
// Main Character
charsrc.left = 0;
charsrc.right = 64;
charsrc.top = (64*9);
charsrc.bottom = (64*10);
mansrc.left = 0;
mansrc.right = 64;
mansrc.top = (64*9);
mansrc.bottom = (64*10);
int count = 0;
while (true) {
charsrc.left += 64;
charsrc.right += 64;
if (charsrc.left > (64*8)) {
charsrc.left = 0;
charsrc.right = 64;
}
chardst.offset(-5, 0);
SystemClock.sleep(100);
count++;
if (count > 38) {
break;
}
}
SystemClock.sleep(1000);
}
void seq3() {
// Main Character
charsrc.left = 0;
charsrc.top = (64*11);
charsrc.right= 64;
charsrc.bottom = (64*12);
SystemClock.sleep(3000);
seq4();
}
void seq2() {
// Main Character
int count = 0;
while (true) {
charsrc.left += 64;
charsrc.right += 64;
if (charsrc.left > (64 * 6)) {
charsrc.left = 0;
charsrc.right = 64;
}
chardst.offset(5, 0);
SystemClock.sleep(100);
count++;
if (count > 38) {
break;
}
}
seq3();
}
void sequence1() {
// main character
dialogsrc.left = 0;
dialogsrc.right = 536;
dialogsrc.top = 0;
dialogsrc.bottom = 120;
dialogdst.left = 100;
dialogdst.right = 736;
dialogdst.top = 50;
dialogdst.bottom = 170;
charsrc.left = 0;
charsrc.top = (64*11);
charsrc.right= 64;
charsrc.bottom = (64*12);
chardst.left = 192;
chardst.top = 255;
chardst.right= 256;
chardst.bottom = 315;
mansrc.left = 0;
mansrc.right = 64;
mansrc.top = (64*3);
mansrc.bottom = (64*4);
mandst.left = 150;
mandst.right = 214;
mandst.top = 255;
mandst.bottom = 315;
SystemClock.sleep(2000);
seq2();
}
void sqdog4() {
dogsrc.left = 0;
dogsrc.right = 80;
dogsrc.top = 0;
dogsrc.bottom = 80;
SystemClock.sleep(2000);
}
void sqdog3() {
dogsrc.left = 80;
dogsrc.right = 80*2;
dogsrc.top = 80;
dogsrc.bottom = 80*2;
SystemClock.sleep(2000);
sqdog4();
}
void sqdog2() {
int count = 0;
while (true) {
dogsrc.left +=80;
dogsrc.right +=80;
if (dogsrc.left > (80*2)) {
dogsrc.left = 0;
dogsrc.right = 80;
}
SystemClock.sleep(100);
count++;
if (count > 28) {
break;
}
}
SystemClock.sleep(5000);
// insert sequence
sqdog3();
}
void sqdog1() {
dogsrc.left = 0;
dogsrc.right = 80;
dogsrc.top = 0;
dogsrc.bottom = 80;
dogdst.left = 470;
dogdst.right = 535;
dogdst.top = 280;
dogdst.bottom = 315;
sqdog2();
}
void sqbear2() {
int count = 0;
while (true) {
bearsrc.left+= 56;
bearsrc.right+=56;
if (bearsrc.left > 56) {
bearsrc.left = 0;
bearsrc.right = 56;
}
SystemClock.sleep(100);
count++;
if (count > 14) {
break;
}
}
SystemClock.sleep(2000);
}
void sqbear1() {
bearsrc.left = 0;
bearsrc.right = 56;
bearsrc.top = (56*4);
bearsrc.bottom = (56*5);
beardst.left = 600;
beardst.right = 656;
beardst.top = 260;
beardst.bottom = 316;
sqbear2();
}
public SurfaceViewer(Context context, AttributeSet attrs) {
super(context, attrs);
bgdst = new Rect();
charsrc = new Rect();
chardst = new Rect();
dogsrc = new Rect();
dogdst = new Rect();
bearsrc = new Rect();
beardst = new Rect();
dialogsrc = new Rect();
dialogdst = new Rect();
mansrc = new Rect();
mandst = new Rect();
mollysrc = new Rect();
mollydst = new Rect();
youngstarsrc = new Rect();
youngstardst = new Rect();
// TODO Auto-generated constructor stub
main = BitmapFactory.decodeResource(getResources(),
R.drawable.moviemysterycharacter);
bgd = BitmapFactory.decodeResource(getResources(),
R.drawable.background);
dog = BitmapFactory.decodeResource(getResources(),
R.drawable.dog);
bear = BitmapFactory.decodeResource(getResources(),
R.drawable.differentbears);
man1 = BitmapFactory.decodeResource(getResources(), R.drawable.man1);
dialog = BitmapFactory.decodeResource(getResources(), R.drawable.dialog1);
molly = BitmapFactory.decodeResource(getResources(), R.drawable.auntmollu);
youngstar = BitmapFactory.decodeResource(getResources(), R.drawable.auntmollu);
p = new Paint();
rstory = new Runnable() {
public void run() {
while(true) {
sequence1();
}
}
};
tstory = new Thread(rstory);
tstory.start();
dstory = new Runnable() {
public void run() {
while(true) {
sqdog1();
}
}
};
tdstory = new Thread(dstory);
tdstory.start();
dbear = new Runnable() {
public void run() {
while(true) {
sqbear1();
}
}
};
tbear = new Thread(dbear);
tbear.start();
dbear = new Runnable() {
public void run() {
while (true) {
sqyoungstar1();
}
}
};
tbear = new Thread(dbear);
tbear.start();
}
@Override
public boolean onTouch(View v, MotionEvent me) {
// TODO Auto-generated method stub
Intent i = new Intent(svc, SecondAct.class);
switch (me.getAction()) {
case MotionEvent.ACTION_DOWN:
svc.startActivity(i);
break;
case MotionEvent.ACTION_UP:
svc.startActivity(i);
break;
}
return true;
}
}