- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我一直在研究并努力使用我的代码来获取 TextView ,以便在触摸“幽灵”时更新乐谱。
这是我的第一个 Android 应用程序,据我所知(我将 Log.w() 添加到 ontouch,但从未发布日志,分数也没有增加甚至出现)onTouch(View v , MotionEvent 事件)永远不会被调用。下面是我的代码。
package com.cs461.Ian;
//TODO:Add accelerometer support
//TODO:Add Clickable ghosts
//TODO:Add camera background
/*Completed:(As of 2:30am 4/16)
* Activity launches
* Ghost appears on screen
* Ghost will randomly move around the screen
*/
import java.util.Random;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.View;
import android.widget.TextView;
public class CameraActivity extends Activity{
Bitmap g;
Ghost a;
Ghost still;
SurfaceHolder mSurfaceHolder;
boolean firsttime=true;
int draw_x,draw_y,xSpeed,ySpeed,score=0;
TextView t;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
a = new Ghost(getApplicationContext());
still = new Ghost(getApplicationContext());
//requestWindowFeature(Window.FEATURE_NO_TITLE);
a.Initalize(BitmapFactory.decodeResource(getResources(), R.drawable.ghost), 20, 20);
still.Initalize(BitmapFactory.decodeResource(getResources(), R.drawable.ghost), 120, 120);
t = (TextView) findViewById(R.id.t);
t.setText("TEST SCORE TO SEE IF TEXTVIEW SHOWS UP");
a.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN){
score++;
t.setText("Score: "+score);
}
return true;
}
});
still.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN){
score++;
t.setText("Score: "+score);
Log.w("CLICKED","Clicked on ghost "+score+" times");
}
return true;
}
});
setContentView(new Panel(this));
}
class Panel extends View {
public Panel(Context context) {
super(context);
}
@Override
public void onDraw(Canvas canvas) {
//g.Initalise(BitmapFactory.decodeFile("/res/drawable-hdpi/ghost.png"), 200, 150, 5, 5);;
update(canvas);
invalidate();
}
/*Places ghost on screen and bounces it around in the screen. My phone is apparently only API level 4(the most up to date is 15) so i didn't code it
*for the accelerometer yet.
*/
public void update(Canvas canvas) {
Random rand = new Random();
if(firsttime){
draw_x = Math.round(System.currentTimeMillis() % (this.getWidth()*2)) ;
draw_y = Math.round(System.currentTimeMillis() % (this.getHeight()*2)) ;
xSpeed = rand.nextInt(10);
ySpeed = rand.nextInt(10);
firsttime=false;
}
draw_x+=xSpeed;
draw_y+=ySpeed;
draw_x = Math.round(System.currentTimeMillis() % (this.getWidth()*2)) ;
draw_y = Math.round(System.currentTimeMillis() % (this.getHeight()*2)) ;
if (draw_x>this.getWidth()){
draw_x = (this.getWidth()*2)-draw_x;
xSpeed = rand.nextInt(10);
if(xSpeed >=5)
xSpeed=-xSpeed;
}
if (draw_y>this.getHeight()){
draw_y = (this.getHeight()*2)-draw_y;
ySpeed = rand.nextInt(10);
if(ySpeed >=5)
ySpeed=-ySpeed;
}
g = BitmapFactory.decodeResource(getResources(), R.drawable.ghost);
canvas.drawBitmap(g, draw_x, draw_y, null);
still.draw(canvas);
a.update(canvas);
}
}
}
忘记添加类 Ghost:
package com.cs461.Ian;
import java.util.Random;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
public class Ghost extends View implements View.OnTouchListener{
public Ghost(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
private Bitmap mAnimation;
private int mXPos;
private int mYPos;
private Rect mSRectangle;
private int mSpriteHeight;
private int mSpriteWidth;
View v;
Rect dest;
int score = 0;
/*public Ghost() {
mSRectangle = new Rect(0,0,0,0);
mXPos = 80;
mYPos = 200;
}*/
public void Initalize(Bitmap theBitmap, int Height, int Width) {
mSRectangle = new Rect(0,0,0,0);
mXPos = 80;
mYPos = 200;
mAnimation = theBitmap;
mSpriteHeight = Height;
mSpriteWidth = Width;
mSRectangle.top = 0;
mSRectangle.bottom = mSpriteHeight;
mSRectangle.left = 0;
mSRectangle.right = mSpriteWidth;
dest = new Rect(mXPos, mYPos, mXPos + mSpriteWidth,
mYPos + mSpriteHeight);
}
public void draw(Canvas canvas) {
canvas.drawBitmap(mAnimation, mXPos, mYPos, null);
}
public void update(Canvas canvas) {
new Random();
mXPos = Math.round(System.currentTimeMillis() % (canvas.getWidth()*2)) ;
mYPos = Math.round(System.currentTimeMillis() % (canvas.getHeight()*2)) ;
if (mXPos>canvas.getWidth())
mXPos = (canvas.getWidth()*2)-mXPos;
if (mYPos>canvas.getHeight())
mYPos = (canvas.getHeight()*2)-mYPos;
draw(canvas);
}
public Rect getRect() {
return mSRectangle;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
score++;
//CameraActivity.t.setText("Score: "+CameraActivity.score);
Log.w("CLICKED","Clicked on ghost "+score+" times");
return true; //doesn't work if returns false either
}
}
最佳答案
重写 Activity
中的 dispatchTouchEvent(MotionEvent event)
以处理所有可能发生的触摸事件。如果它们处理事件,则返回 false
将 MotionEvent
传递给层次结构中的下一个 View \布局。
关于java - Android onTouch 事件没有发生?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10291101/
我在 ScrollView 中使用 ImageView Controller 。在实现和测试方面,我发现滚动总是覆盖 onTouch 事件而不是 imageView (dragImage)。 我尝试使
运行代码时没有收到任何错误,但触摸屏幕时没有任何反应。全局变量 select 的值应该改变,但没有任何反应。 这是代码 public class NonmultiplierSixGame extend
我发现当我停止在屏幕上移动手指时,Android onTouch 事件不会被触发,因此我无法正确获取游戏中每个刻度的指针计数。 @Override public boolean onTouch(Vie
这个问题已经有答案了: Android beginner: understanding MotionEvent actions (2 个回答) 已关闭 9 年前。 好吧,我正在制作一个简单的秒表,当您
我正在开发一款在屏幕上运行不同位图的游戏(SurfaceView)。由于它们具有不同的速度,有时会发生两个字符叠加的情况,一个在另一个后面。我使用 OnTouch 功能来捕捉玩家触摸角色的瞬间。但是,
我正在使用 onTouch(View arg0, MotionEvent arg1)作为游戏输入。它返回 false 所以它没有被处理。在我运行 4.0.4 的 HTC HD2 上,这足以让播放器足够
我试图在我的 LoadPreview().execute 启动时禁用 onTouch(),因为如果用户点击触发我的 LoadPreview() 的按钮,将会出现重复的预览按钮。所以我想在加载过程中禁用
好的,我正在玩扩展 View 的 ontouch 事件。 我所做的是在触摸时形成一个圆圈。当你移动时,圆圈会随之而来。当你移动另一个圆圈时,它会位于递减半径的位置,直到它消失……(现在最多有 10 个
这是我的布局文件 这是我的自定义 View import android.view.MotionEvent; import android.view.View; import
在我的应用程序中,我想计算两个距离之间的比率,但我不知道为什么变量 dist1 和 dist2 没有保存我在 IF ELSE 语句中计算的值,我的代码有什么问题? 任何指导或帮助将不胜感激。 这是方法
我已将 RelativeLayout 设为可点击,但我希望整个布局在用户触摸时变为橙色。我该怎么做? 最佳答案 您可以使用描述的方法here . 关于android - 如何使用橙色制作相对布局渲
我需要在触摸屏幕时播放文件。 我尝试了以下方法,但没有成功: -(IBAction)play; { CFBundleRef mainBundle = CFBundleGetMainBundle
我在 onCreate 方法中有这段代码: ImageView iv01 = (ImageView)findViewById(R.id.hexagon01); iv01.
我正在尝试用我的观点做 3 件事。第一次“点击”我的屏幕时,我想将 View 顶部的 ImageView 完全设置为 0alpha 以显示下方 View 。当屏幕水龙头被抬起时,我希望 alpha 回
我使用 onTouchListener 在用户手指按下时更改背景颜色,并在用户手指抬起时更改回默认颜色。问题在于,当用户的手指向下时,背景颜色会发生变化,但如果用户没有抬起手指并开始滚动,背景颜色不会
我正在使用 ontouch 事件,但我遇到的问题是它调用了 down 事件,但它没有调用 move 事件或 UP 事件检查以下代码 public class DragNewActivity exten
您好,我正在 http://www.rbgrn.net/content/354-glsurfaceview-adapted-3d-live-wallpapers 的帮助下制作动态壁纸.现在我想在主屏幕
我正在玩 UI 事件处理,我发现了一些我无法从 Android Dev 中找到解释的东西:我有一个 ImageView 和一个 TextView,每当我触摸 ImageView 时,TextView
我有这样的布局: LinearLayout RelativeLayout(id = test) FrameLayout (From a LIB. id = lib) 我尝试将
如果设备上有多根手指,onTouch 监听器是针对每个手指运行一次,还是针对每个事件(手指按下、移动等)运行一次并包括每个 Activity 的触摸指针? 如果 (e.getAction() & Mo
我是一名优秀的程序员,十分优秀!