gpt4 book ai didi

Java Android : onTouchListener not working

转载 作者:行者123 更新时间:2023-12-01 05:08:47 24 4
gpt4 key购买 nike

我是编写 Java 应用程序的新手。我设法构建了一个小测试应用程序,其中的 SurfaceView 在基于线程的循环中不断重绘。

现在我成功地解决了所有这些图形问题,尽管我想实现一个 onTouchListener。我按照 Google 的指示做了,并将其写入我的主要 Activity 中,实现 OnTouchListener 并将 SurfaceView 设置为 OnTouchListener (直接:Panel.p.setOnTouchListener 和间接:View view = findViewById( R.id.SurfaceView01);view.setOnTouchListener(this);)

不过,我在这两种情况下都得到了结果。我的应用程序设置为“全屏”,XML 仅包含对面板的引用。

我不知道错误可能出在哪里,除了 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>

主要 Activity :

//'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;
}

Canvas 线程:

    _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);
}
}
}
}

非常感谢一些建议。

非常感谢您。

最佳答案

您可以直接在面板的构造函数中声明 onTouchListener:

public class Panel extends SurfaceView{
Panel(final Context context, AttributeSet attrs){

setOnTouchListener(new View.OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {
//put your logics here
}
}
}
}

我还认为,由于 CanvasThread 只关心面板上的绘图 - 除了面板之外的任何东西都不应该了解 CanvasThread。

顺便说一句:在您的示例中,您只需将 Activity 的 onTouch 方法中的 return false 设置为 return true 即可。我认为应该可以解决问题。

@Override
public boolean onTouch (View v, MotionEvent event){
//....
return true; //not false
}

关于Java Android : onTouchListener not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12339028/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com