Android的借鉴使用SurfaceView和主题 [英] Android draw using SurfaceView and Thread

查看:104
本文介绍了Android的借鉴使用SurfaceView和主题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图扳回一球,以使用3班我的屏幕。我读了一些关于这一点,我发现了一个code段,工程使用3个班在一个页面上,<一个href="https://web.archive.org/web/20121204020009/http://www.droidnova.com/playing-with-graphics-in-android-part-ii,160.html"相对=nofollow>与图形玩安卓

我改变了code,让我有一个球被移动和移动碰壁像下面的图片时,方向(这是使用code中的链接)

现在我喜欢的课程分成3个不同的页面不作出一切都那么拥挤,一切都设置了同样的方式。

以下是3班我。

  1. BallActivity.java
  2. Ball.java
  3. BallThread.java

 包com.brick.breaker;
进口android.app.Activity;
进口android.os.Bundle;
进口android.view.Window;
进口android.view.WindowManager;



公共类BallActivity延伸活动{

私人球球;

@覆盖
保护无效的onCreate(包savedInstanceState){
    // TODO自动生成方法存根
    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

    球=新球(本);
    的setContentView(球);
}

@覆盖
保护无效的onPause(){
    // TODO自动生成方法存根
    super.onPause();

    的setContentView(空);
    球= NULL;

    完();
}

}
 


 包com.brick.breaker;

进口android.content.Context;
进口android.graphics.Bitmap;
进口android.graphics.BitmapFactory;
进口android.graphics.Canvas;
进口android.view.SurfaceHolder;
进口android.view.SurfaceView;

公共类球延伸SurfaceView实现SurfaceHolder.Callback {

私人BallThread ballThread = NULL;

私人位图位图;

私人持股量X,Y;
私人浮动VX,VY;

公共球(上下文的背景下){
    超(上下文);
    // TODO自动生成构造函数存根
    位= BitmapFactory.de codeResource(getResources(),R.drawable.ball);

    X = 50.0f;
    Y = 50.0f;

    VX = 10.0f;
    VY = 10.0f;

    。getHolder()的addCallback(本);
    ballThread =新BallThread(getHolder(),这一点);
}

保护无效的OnDraw(帆布油画){

    更新(画布);

    canvas.drawBitmap(位图,X,Y,空);
}

公共无效更新(帆布油画){

    checkCollisions(画布);

    X + = VX;
    Y + = VY;
}

公共无效checkCollisions(帆布油画){

    如果(X  -  VX℃,){

        VX = Math.abs(VX);

    }否则如果(X + VX&GT; canvas.getWidth() -  getBitmapWidth()){

        VX = -Math.abs(VX);
    }

    如果(γ -  VY℃,){

        VY = Math.abs(VY);

    }否则如果(Y + VY&GT; canvas.getHeight() -  getBitmapHeight()){

        VY = -Math.abs(VY);
    }
}

公众诠释getBitmapWidth(){

    如果(位图!= NULL){

        返回bitmap.getWidth();

    } 其他 {

        返回0;
    }
}

公众诠释getBitmapHeight(){

    如果(位图!= NULL){

        返回bitmap.getHeight();

    } 其他 {

        返回0;
    }
}

公共无效surfaceChanged(SurfaceHolder持有人,INT格式,诠释的宽度,
        INT高度){
    // TODO自动生成方法存根

}

公共无效surfaceCreated(SurfaceHolder持有者){
    // TODO自动生成方法存根
    ballThread.setRunnable(真正的);
    ballThread.start();

}

公共无效surfaceDestroyed(SurfaceHolder持有者){
    // TODO自动生成方法存根

    布尔重试= TRUE;
    ballThread.setRunnable(假);

    而(重试){

        尝试 {

            ballThread.join();
            重试= FALSE;

        }赶上(InterruptedException的IE){

            //再试一次,再而三
        }

        打破;
    }

    ballThread = NULL;

}

}
 


 包com.brick.breaker;

进口android.graphics.Canvas;
进口android.view.SurfaceHolder;

公共类BallThread继承Thread {

私人SurfaceHolder SH;
私人球球;

私人帆布油画;

私人布尔运行= FALSE;

公共BallThread(SurfaceHolder _holder,球_ball){

    SH = _holder;
    球= _ball;
}

公共无效setRunnable(布尔_run){

    运行= _run;
}

公共无效的run(){

    而(运行){

        帆布= NULL;

        尝试 {

            帆布= sh.lockCanvas(空);

            同步(SH){

                ball.onDraw(画布);
            }

        } 最后 {

            如果(帆布!= NULL){

                sh.unlockCanvasAndPost(画布);
            }

        }

    }
}

公共帆布getCanvas(){

    如果(帆布!= NULL){

        返回帆布;

    } 其他 {

        返回null;
    }
}
}
 


下面是一张图片,显示这些类的结果。

我试图弄清楚这一点,但因为我是pretty的新Android开发我想我可以寻求帮助。

有没有人知道是什么原因造成了球会得出这样呢? 在code是pretty的大致相同的对视了一眼中的链接,我试图尝试找到一个解决方案,但没有运气。

THX预先的任何帮助=)

解决方案

好了,你可以在图像上看到的,你只画了球。相反,你需要在每次绘制球,然后再重新画了一个黑色的背景(或任何你愿意的话)。

另外,你只能在previous位置画一个黑色区域,但你可能有问题,后来,当你使用多个对象。

这里有一个很好的样本,类似于你做

I am trying to draw a ball to my screen using 3 classes. I have read a little about this and I found a code snippet that works using the 3 classes on one page, Playing with graphics in Android

I altered the code so that I have a ball that is moving and shifts direction when hitting the wall like the picture below (this is using the code in the link).

Now I like to separate the classes into 3 different pages for not making everything so crowded, everything is set up the same way.

Here are the 3 classes I have.

  1. BallActivity.java
  2. Ball.java
  3. BallThread.java


package com.brick.breaker;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;



public class BallActivity extends Activity {

private Ball ball;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

    ball = new Ball(this);
    setContentView(ball);
}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();

    setContentView(null);
    ball = null;

    finish();
}

}


package com.brick.breaker;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class Ball extends SurfaceView implements SurfaceHolder.Callback {

private BallThread ballThread = null;

private Bitmap bitmap;

private float x, y;
private float vx, vy;

public Ball(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
    bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ball);

    x = 50.0f;
    y = 50.0f;

    vx = 10.0f;
    vy = 10.0f;

    getHolder().addCallback(this);
    ballThread = new BallThread(getHolder(), this);
}

protected void onDraw(Canvas canvas) {

    update(canvas);

    canvas.drawBitmap(bitmap, x, y, null);
}

public void update(Canvas canvas) {

    checkCollisions(canvas);

    x += vx;
    y += vy;
}

public void checkCollisions(Canvas canvas) {

    if(x - vx < 0) {

        vx = Math.abs(vx);

    } else if(x + vx > canvas.getWidth() - getBitmapWidth()) {

        vx = -Math.abs(vx);
    }

    if(y - vy < 0) {

        vy = Math.abs(vy);

    } else if(y + vy > canvas.getHeight() - getBitmapHeight()) {

        vy = -Math.abs(vy);
    }
}

public int getBitmapWidth() {

    if(bitmap != null) {

        return bitmap.getWidth();

    } else {

        return 0;
    }
}

public int getBitmapHeight() {

    if(bitmap != null) {

        return bitmap.getHeight();

    } else {

        return 0;
    }
}

public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {
    // TODO Auto-generated method stub

}

public void surfaceCreated(SurfaceHolder holder) {
    // TODO Auto-generated method stub
    ballThread.setRunnable(true);
    ballThread.start();

}

public void surfaceDestroyed(SurfaceHolder holder) {
    // TODO Auto-generated method stub

    boolean retry = true;
    ballThread.setRunnable(false);

    while(retry) {

        try {

            ballThread.join();
            retry = false;

        } catch(InterruptedException ie) {

            //Try again and again and again
        }

        break;
    }

    ballThread = null;

}

}


package com.brick.breaker;

import android.graphics.Canvas;
import android.view.SurfaceHolder;

public class BallThread extends Thread {

private SurfaceHolder sh;
private Ball ball;

private Canvas canvas;

private boolean run = false;

public BallThread(SurfaceHolder _holder,Ball _ball) {

    sh = _holder;
    ball = _ball;
}

public void setRunnable(boolean _run) {

    run = _run;
}

public void run() {

    while(run) {

        canvas = null;

        try {

            canvas = sh.lockCanvas(null);

            synchronized(sh) {

                ball.onDraw(canvas);
            }

        } finally {

            if(canvas != null) {

                sh.unlockCanvasAndPost(canvas);
            }

        }

    }
}

public Canvas getCanvas() {

    if(canvas != null) {

        return canvas;

    } else {

        return null;
    }
}
}


Here is a picture that shows the outcome of these classes.

I've tried to figure this out but since I am pretty new to Android development I thought I could ask for help.

Does any one know what is causing the ball to be draw like that? The code is pretty much the same as the one in the link and I have tried to experiment to find a solution but no luck.

Thx in advance for any help=)

解决方案

well , as you can see on the image , you only drew the ball . instead , you need to re-drew a black background (or whatever that you wish) before each time you draw the ball.

alternatively , you can draw a black area only on the previous position , but you might have problems with it later , when you use more objects.

here's a nice sample, similar to what you do

这篇关于Android的借鉴使用SurfaceView和主题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆