使用自定义方法在画布上绘制?

4

我正在编写一款Android 2D游戏,目前遇到了一些问题。

在我的包中,我有一个线程,它会在画布的实例上进行绘制。我从资源中绘制Drawable位图。我想要做的是让线程处理背景图片的绘制,同时允许自定义对象使用drawable对象在同一个画布上进行绘制。这在逻辑上是可行的,但我无法使其正常工作。每当我尝试在自定义类中检索资源时,应用程序就会在启动时崩溃。

以下是我的一些尝试:(如果我做了什么愚蠢的事情,请不要笑话我,我正在努力。)

public class Worker{

//Get drawables
//  Resources res = getResources();
//  Drawable man1 = res.getDrawable(R.drawable.workertest);


//  Context mContext;
//  Resources res = mContext.getResources();

//  Drawable man1 = mContext.getResources().getDrawable(R.drawable.workertest);
//  Drawable man2 = mContext.getResources().getDrawable(R.drawable.workertest1);

//  Drawable man1 = res.getDrawable(R.drawable.workertest);
//  Drawable man2 = res.getDrawable(R.drawable.workertest1);

}

你可以看到,我尝试了几种不同的方法,并且还尝试将(extends activity)添加到我的类中,但我无法解决这个问题。

这段代码并没有涉及实际绘制到画布上,我还没有跨越这个障碍

编辑:这是Activity。你可以看到它调用lunarview中的一个线程。该线程实例化需要检索Drawable的对象。神啊,我如何将应用程序上下文传递给对象?或者还有其他方法吗?是的,这是来自Android示例代码的代码。

public class LunarLander extends Activity {
    private static final int MENU_EASY = 1;

    private static final int MENU_HARD = 2;

    private static final int MENU_MEDIUM = 3;

    private static final int MENU_PAUSE = 4;

    private static final int MENU_RESUME = 5;

    private static final int MENU_START = 6;

    private static final int MENU_STOP = 7;


    /** A handle to the thread that's actually running the animation. */
    public static LunarThread mLunarThread;

    /** A handle to the View in which the game is running. */
    public LunarView mLunarView;



    /**
     * Invoked during init to give the Activity a chance to set up its Menu.
     *
     * @param menu the Menu to which entries may be added
     * @return true
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);

        menu.add(0, MENU_START, 0, R.string.menu_start);
        menu.add(0, MENU_STOP, 0, R.string.menu_stop);
        menu.add(0, MENU_PAUSE, 0, R.string.menu_pause);
        menu.add(0, MENU_RESUME, 0, R.string.menu_resume);
        menu.add(0, MENU_EASY, 0, R.string.menu_easy);
        menu.add(0, MENU_MEDIUM, 0, R.string.menu_medium);
        menu.add(0, MENU_HARD, 0, R.string.menu_hard);

        return true;
    }

    /**
     * Invoked when the user selects an item from the Menu.
     *
     * @param item the Menu entry which was selected
     * @return true if the Menu item was legit (and we consumed it), false
     *         otherwise
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case MENU_START:
                mLunarThread.doStart();
                return true;
            case MENU_STOP:
                mLunarThread.setState(LunarThread.STATE_LOSE,
                        getText(R.string.message_stopped));
                return true;
            case MENU_PAUSE:
                mLunarThread.pause();
                return true;
            case MENU_RESUME:
                mLunarThread.unpause();
                return true;
            case MENU_EASY:
                mLunarThread.setDifficulty(LunarThread.DIFFICULTY_EASY);
                return true;
            case MENU_MEDIUM:
                mLunarThread.setDifficulty(LunarThread.DIFFICULTY_MEDIUM);
                return true;
            case MENU_HARD:
                mLunarThread.setDifficulty(LunarThread.DIFFICULTY_HARD);
                return true;
        }

        return false;
    }

    /**
     * Invoked when the Activity is created.
     *
     * @param savedInstanceState a Bundle containing state saved from a previous
     *        execution, or null if this is a new execution
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // tell system to use the layout defined in our XML file
        setContentView(R.layout.lunar_layout);

        // get handles to the LunarView from XML, and its LunarThread
        mLunarView = (LunarView) findViewById(R.id.lunar);
        mLunarThread = mLunarView.getThread();


        // give the LunarView a handle to the TextView used for messages
        mLunarView.setTextView((TextView) findViewById(R.id.text));




        if (savedInstanceState == null) {
            // we were just launched: set up a new game
            mLunarThread.setState(LunarThread.STATE_READY);
            Log.w(this.getClass().getName(), "SIS is null");
        } else {
            // we are being restored: resume a previous game
            mLunarThread.restoreState(savedInstanceState);
            Log.w(this.getClass().getName(), "SIS is nonnull");
        }
    }

    /**
     * Invoked when the Activity loses user focus.
     */
    @Override
    protected void onPause() {
        super.onPause();
        mLunarView.getThread().pause(); // pause game when Activity pauses
    }

    /**
     * Notification that something is about to happen, to give the Activity a
     * chance to save state.
     *
     * @param outState a Bundle into which this Activity should save its state
     */
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        // just have the View's thread save its state into our Bundle
        super.onSaveInstanceState(outState);
        mLunarThread.saveState(outState);
        Log.w(this.getClass().getName(), "SIS called");
    }
    public boolean onTouchEvent(MotionEvent event){
        int clickX = Math.round(event.getX()/10)*10;
        int clickY = Math.round(event.getY()/10)*10;

        LunarView.xCo = clickX;
        LunarView.yCo = clickY;

        return true;

    }

那么...你的应用程序中有主要活动吗?如果您可以发布崩溃信息,那将有所帮助。但我猜测,您的应用程序中没有必要运行Android应用程序的活动。 - Noel
不,我没有这样做。应用程序中有几个类,而主要活动在另一个类中。实际上,在上述引用类中还有其他方法可以完美地工作。唯一出现崩溃的情况是当我尝试在此自定义类中使用Drawable资源时。当我将上述代码注释掉时,应用程序运行得很完美,否则在启动时会出现“对不起,应用程序意外停止,请重试”等错误提示。没有编译器错误。 - cody
我想到了一个临时解决办法...由于线程在处理可绘制对象时没有出错,我在线程中创建了一个方法来绘制图像,然后在我的自定义类中创建了一个调用线程中绘制方法的方法。这似乎是一种非常低效的方式,但目前看起来是有效的。 - cody
我就是不明白为什么这个不起作用。为什么一个类不能有从资源绘制到画布的方法?它肯定可以,我只是做错了什么。 - cody
哇,那很有道理,我会尝试一下并回复你。 - cody
显示剩余4条评论
1个回答

1
正如Greg所建议的那样,问题在于获取正确的Context以检索资源。解决这个问题的简单方法是在Worker类构造函数中将主Activity作为参数传递,并将其保存为private Context字段。例如:
public class Worker{
  private Context mContext;
  public Worker (Context context) {
    mContext = context;
    Resources res = mContext.getResources();
    // all the rest of your code
  }
}

你将在主活动中使用它:

Worker worker = new Worker(this);

我看到的唯一问题是,我需要在其中实现Worker类的代码是一个用于绘制UI和处理点击等操作的线程。因此,我的困难在于理解如何将实际的应用程序上下文传递给构造函数。换句话说,上面实现(new Worker(this))的代码不起作用。 "This"被读取为类名LunarView。我不知道这是否有意义。 - cody
你需要自己想出来。如果你的LunarView类是Activity的内部类,那么请使用MyActivity.this。如果不是,则在从Activity实例化LunarView类时使用相同的思路(通过构造函数传递上下文)。 - Aleadam
哎呀,它不工作了,哈哈。我需要什么确切的语法? - cody
我看不到你在哪里实例化Worker类,所以很难告诉你具体该怎么做。但是LunarView和LunarThread类已经有指向正确上下文的字段了:在LunarThread中称为mContext。在Worker类的构造函数中使用它。 - Aleadam

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接