【问题标题】:Simple flip card game in Android using layouts instead of a surfaceview?Android中使用布局而不是surfaceview的简单翻转纸牌游戏?
【发布时间】:2014-07-23 08:14:55
【问题描述】:

我的项目是在 Android 中创建一个翻转纸牌游戏,例如 http://partyhatmy.blogspot.kr/2012/08/angry-bird-matching-card-game.html。不同之处在于屏幕上会出现 3 x 4 总共 12 张牌,并且游戏会有计时器。因此,如果计时器到期或用户找到所有对,则新阶段开始。

我的问题是我确实知道如何使用 SurfaceView 实现这一点,但由于所有卡片都在固定位置,我认为可以使用 xml 中的布局来实现游戏,但我不知道如何。网络上有没有可用的起点资源?


第 1 版

我的代码是这样的:我只想先将一个 TextView 的剩余时间打印到屏幕上。问题是屏幕全黑(没有 runOnUiThread() 调用,活动完美地绘制了给定的布局 activity_game。

public class GameActivity extends Activity {

private TextView mTimerTextView;
private int mRemainingTime = 30;

@Override
protected void onCreate(Bundled savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_game);

    mTimerTextView = (TextView)findViewById(R.id.remaining_time);
    this.runOnUiThread(new Runnable() {
        public void run() {
            long lastSystemTime = 0;
            mTimeTextView.setText(String.valueOf(mRemainingTime));
            while (mRemainingTime > 0) {
                if (lastSystemTime == 0) { // initial run
                    lastSystemTime = System.currentTimeMillis();
                    continue;
                }

                long currentTime = System.currentTimeMillis();
                long elapsedTime = currentTime - lastSystemTime;
                lastSystemTime = currentTime;

                if (elapsedTime > 1000) {
                    mRemainingTime--;
                    mTimeTextView.setText(String.valueOf(mRemainingTime));
                }

                // To avoid excessive loop
                try {
                    Thread.sleep(100);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    });
}

}

【问题讨论】:

    标签: android layout surfaceview


    【解决方案1】:

    查看此链接,它将帮助您进行翻转动画

    Displaying card flip animation on old android

    http://www.techrepublic.com/blog/software-engineer/use-androids-scale-animation-to-simulate-a-3d-flip/

    使用此代码在 ui 线程中进行更改

     MainActivity.this.runOnUiThread(new Runnable() {
    
        public void run() {
           //do ui changes here
    
        }
    });
    

    【讨论】:

    • 非常感谢。但是另一个线程是否可以动态地操作 ImageView 对象?例如,我想要一个计时器,如果计时器即将到期(比如 5 秒),我想在剩余的卡片上添加一个红色闪烁动画。您的链接仅显示如何使用 onClickListener 处理翻转
    • 太棒了!你知道什么时候将 runOnUiThread() 放在 MainActivity 中吗?我先把它放在MainActivity的onCreate()方法中的setContentView()之后,程序似乎运行不正常.....
    • 把它放在你的计时器类中......而不是在oncreate中......或者你可以发布你的代码......这样我就可以说你必须在哪里添加它
    猜你喜欢
    • 2017-12-17
    • 2011-08-02
    • 2023-03-28
    • 2010-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-01
    相关资源
    最近更新 更多