我是为 Java 编写应用程序的新手。我设法构建了一个小测试应用程序,它带有一个在基于线程的循环中不断重绘的 SurfaceView。
现在我设法解决了所有这些图形的问题,尽管我想实现一个 onTouchListener。我按照 Google 的要求做了,并将其写入实现 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>
主要活动:
//'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;
}
画布线程:
_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);
}
}
}
}
非常感谢一些建议。
已经非常感谢了。