我是新编写Apps for Java。我设法构建了一个小测试应用程序,其中一个SurfaceView不断在基于线程的循环中重绘。Java Android:onTouchListener无法正常工作
现在我设法将所有这些图形化的东西都处理出来,尽管我想实现一个onTouchListener。我没有,因为我被谷歌告知,并把它写进我的执行OnTouchListener和我SurfaceView设置为OnTouchListener(直接主要活动:Panel.p.setOnTouchListener
和间接:查看view = findViewById(R.id.SurfaceView01);view.setOnTouchListener(this);)
我得到了在两种情况下的结果,虽然我的应用程序设置。到'Fullscreen'只包含一个对Panel的引用
我不知道在那里的错误可能是,一切正常,但onTouchListener。我要添加所有的文件是相关的这里的问题希望有人知道我做错了什么。
XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.example.test.Panel android:id="@+id/SurfaceView01" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:maxHeight="40dip">
</com.example.test.Panel>
主要活动:
//'Constructor'
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
//View view = findViewById(R.id.SurfaceView01);
//view.setOnTouchListener(this);
Panel.p.setOnTouchListener(this);
}
//Called when App's being closed
@Override
public void onPause(){
super.onPause();
CanvasThread._run = false;
}
//Touching - Somehow doesn't seem to want to do anything :(
@Override
public boolean onTouch (View v, MotionEvent event){
int x = (int)event.getX();
int y = (int)event.getY();
CanvasThread.scene.bird.setDestination(x, y);
//Pressing
if(event.getAction()==MotionEvent.ACTION_DOWN){}
//Releasing
if(event.getAction()==MotionEvent.ACTION_UP){}
//Moving
if(event.getAction()==MotionEvent.ACTION_MOVE){}
return false;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
面板(SurfaceView):
CanvasThread canvasthread;
public static Panel p;
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
canvasthread.setRunning(true);
canvasthread.start();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
boolean retry = true;
canvasthread.setRunning(false);
while (retry) {
try {
canvasthread.join();
retry = false;
} catch (InterruptedException e) {}
}
}
public Panel(Context context, AttributeSet attrs) {
super(context, attrs);
getHolder().addCallback(this);
canvasthread = new CanvasThread(getHolder(), this);
setFocusable(true);
p = this;
}
public void onDraw(Canvas canvas, Bitmap img, int x, int y) {
Paint paint = new Paint();
canvas.drawBitmap(img, x, y, null);
}
public void setBlack(Canvas canvas){
canvas.drawColor(Color.BLACK);
}
//-----------Image Management----------------------
public Bitmap chest = BitmapFactory.decodeResource(getResources(), R.drawable.chest);
public Bitmap stairs = BitmapFactory.decodeResource(getResources(), R.drawable.stairs);
public Bitmap[][] blueBird = doubleCrop(BitmapFactory.decodeResource(getResources(), R.drawable.bluebird), 4, 2);
public Bitmap[] tileset = cropIt(BitmapFactory.decodeResource(getResources(), R.drawable.tileset), 10);
public Bitmap[] cropIt(Bitmap set, int length){
int width = set.getWidth()/length;
int height = set.getHeight();
Bitmap[] array = new Bitmap[length];
for(int c=0; c<length; c++){
array[c] = Bitmap.createBitmap(set, c*width, 0, width, height);
}
return array;
}
public Bitmap[][] doubleCrop(Bitmap set, int yLength, int xLength){
int width = set.getWidth()/xLength;
int height = set.getHeight()/yLength;
Bitmap[][] array = new Bitmap[yLength][xLength];
for(int cy=0; cy<yLength; cy++){
for(int cx=0; cx<xLength; cx++){
array[cy][cx] = Bitmap.createBitmap(set, cx*width, cy*height, width, height);
}
}
return array;
}
CanvasThread:
_surfaceHolder = surfaceHolder;
_panel = panel;
scene = new Scene(_panel);
}
public void setRunning(boolean run) {
_run = run;
}
@Override
public void run() {
Canvas c;
while (_run) {
try{Thread.sleep(FRAMEWAIT);}catch(Exception ex){}
c = null;
try {
c = _surfaceHolder.lockCanvas(null);
synchronized (_surfaceHolder) {
scene.circle(_panel, c);
}
} finally {
if (c != null) {
_surfaceHolder.unlockCanvasAndPost(c);
}
}
}
}
有些建议非常感谢。
非常感谢您。
嘿感谢快速回答:) 嗯,我试图做你所说的话:我直接就把setOnTouchListener在面板的构造函数,但它没有绝对没有(我直接把代码在你行标记为“此处的逻辑”,以便它在无论什么时候被调用时都能正常工作)。我尝试在我的原始代码中设置返回true和false,并根据您所说的编辑代码,同样的事情:什么也没有发生...... – Dude
在您的代码中,我没有看到您以编程方式设置高度和宽度的部分你的面板。在XML面板中,高度和宽度是“wrap_content”,所以您的面板宽度和高度可能缩小到零。你看到你的面板了吗?如果没有 - 尝试添加'android:backgraound =“#f00”'参数来查看面板区域。例如,尝试将面板的宽度和高度设置为以100dp为单位的XML。如果这没有帮助 - 尝试(暂时)删除代码,你停止你canvasthread。我的意思是'CanvasThread._run = false;'部分。 –
好吧,它现在可以工作,在输入之前我改变了Bird Class的一个问题。现在它完全符合你给我的代码,谢谢你:D 顺便说一句。有什么办法可以获得类似于System.out.println()的东西,它可以在Android上进行调试吗? – Dude