首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android On Touch

Android On Touch
EN

Stack Overflow用户
提问于 2011-04-12 15:16:09
回答 2查看 430关注 0票数 0

我一直在尝试让一个程序在我触摸屏幕时画圆圈,但没有成功,有人能告诉我怎么做吗?或者一个好的教程,告诉我如何..。我的代码中不断出现错误

代码语言:javascript
复制
import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.text.format.Time;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import java.lang.Math;

public class GameView extends View{

        private final float x;
        private final float y;
        private final int r;
        private final Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

        public GameView(Context context, float x, float y, int r) {
            super(context);
            setFocusable(true);
            mPaint.setColor(0xFFFF0000);
            this.x = x;
            this.y = y;
            this.r = r;
        }

        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            canvas.drawCircle(x, y, r, mPaint);
        } 


}

GameActivity.java

代码语言:javascript
复制
import android.app.Activity; 
import android.content.Context;
import android.os.Bundle;   
import android.os.Handler;
import android.os.Message;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;

public class GameActivity extends Activity {    
    /** Called when the activity is first created. */ 
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        FrameLayout main = (FrameLayout) findViewById(R.id.my_view); 

        main.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(final View v, MotionEvent e) {

                 final float x = e.getX();
                 final float y = e.getY();
                  final Handler handler = new Handler() 
                  {                  
                    public void handleMessage(Message msg) {



                      }                 
                    };          
                    Thread graphicThread = new Thread()       
                    {                        
                        public void run() {                              

                            try {

                                //Do the drop
                                   FrameLayout flView = (FrameLayout) v;
                                 GameView gm = new GameView(getParent(), x,y,25);
                                  flView.addView(gm);


                                     Thread.sleep(1000);

                                     handler.sendMessage(handler.obtainMessage());
                                }
                             catch (InterruptedException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }

                        }                     
                        };                
                         boolean isRunning=true;
                        graphicThread.start();   
                        return true;

                        }           


        }); 


      }
    }
EN

回答 2

Stack Overflow用户

发布于 2011-04-12 19:26:24

如果不是子活动,就不应该在活动中使用getParent()

改用下面的代码:

GameView gm = new GameView(this, x, y, 25);

票数 0
EN

Stack Overflow用户

发布于 2011-04-12 19:32:12

因为NPE问题已经回答了,所以我想提供教程问题的答案。我已经写了一个系列(部分更新),你可以在这里找到:我已经为游戏写了一个关于2d图形的教程。请阅读:http://www.droidnova.com/2d-tutorial-series

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5631684

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档