Android 安卓操作系统停止工作
我想做的是,当有人按住屏幕上的手指时,向前看等于真,但当他们把它取下来时,它等于假 所以我尝试使用get_actions()方法 但只有行动被调用 这是我的密码Android 安卓操作系统停止工作,android,touch,Android,Touch,我想做的是,当有人按住屏幕上的手指时,向前看等于真,但当他们把它取下来时,它等于假 所以我尝试使用get_actions()方法 但只有行动被调用 这是我的密码 public class zombView extends SurfaceView{ private Bitmap bmp, grass, joystick; private SurfaceHolder holder; Timer t = new Timer(); float x = 0, y = 0;
public class zombView extends SurfaceView{
private Bitmap bmp, grass, joystick;
private SurfaceHolder holder;
Timer t = new Timer();
float x = 0, y = 0;
boolean forward;
public zombView(Context context) {
super(context);
holder = getHolder();
holder.addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
@Override
public void surfaceCreated(final SurfaceHolder holder) {
t.scheduleAtFixedRate(new TimerTask(){
public void run(){
Canvas c = holder.lockCanvas(null);
onDraw(c);
holder.unlockCanvasAndPost(c);
if(forward){
x = x + 5;
}
onTouchEvent(null);
}
},200,100);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format,
int width, int height) {
}
});
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
grass = BitmapFactory.decodeResource(getResources(), R.drawable.grassland);
joystick = BitmapFactory.decodeResource(getResources(), R.drawable.joystic);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.BLACK);
canvas.drawBitmap(grass, getWidth() - getWidth(), getHeight() - getHeight(), null);
canvas.drawBitmap(joystick, getWidth() - getWidth(),joystick.getHeight(), null);
canvas.drawBitmap(bmp, x, y, null);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
/*switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN: forward = true;
case MotionEvent.ACTION_POINTER_DOWN: forward = true;
case MotionEvent.ACTION_UP: forward = false;
case MotionEvent.ACTION_POINTER_UP:forward = false;
case MotionEvent.ACTION_MOVE: forward = true;
}*/
if(event.getAction()== MotionEvent.ACTION_DOWN){
forward = true;
}if(event.getAction()== MotionEvent.ACTION_UP){
forward = false;
}
return super.onTouchEvent(event);
}
}
包含此SurfaceView的布局未将事件传递到此SurfaceView。您需要做的是重写ontouch方法并在其中返回false。我希望这会有帮助。使用这个
case MotionEvent.ACTION_DOWN:
touch_start(x, y);
invalidate();
break;
case MotionEvent.ACTION_MOVE:
touch_move(x, y);
invalidate();
break;
case MotionEvent.ACTION_UP:
touch_up();
invalidate();
break;
}
UI中的计时器不安全。我可以提出一个我自己的类,目的是保留一些时间和日期,在一些视图中显示它们自己
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import android.os.Handler;
import android.widget.TextView;
/**
* The class for creating and refreshing many different fields on different layouts,
* that can hold actual time and/or date in different formats
* The formats should be as in http://developer.android.com/reference/java/text/SimpleDateFormat.html.
* Only present and visible fields are being actualized, so there is no need to clean the clock list after closing an activity
*
* Examples of use:
*
* Clock.registerClock((TextView) findViewById(R.id.TimeField), "HH:mm");
* Clock.registerClock((TextView) findViewById(R.id.DateField), "d.M.yyyy EEE");
* Clock.start(10000L);
*
* @author Petr Gangnus
*/
public final class Clock {
/**
* the handler that works instead of timer and supports UI
*/
static private Handler handler = new Handler();
/**
* the interval of the time refreshing
*/
static private long refreshStep;
/**
* pairs TextView timer+time/date format
*/
private TextView clockFace;
private String format;
private Clock(TextView clockFace, String format){
this.clockFace=clockFace;
this.format=format;
}
// here is the list of views containing the visual timers that should be held actual
static private ArrayList<Clock> clocks=new ArrayList<Clock>();
/**
* fills all timer fields by actual time value, according to their formats.
*/
static private Runnable mUpdateTimeTask = new Runnable() {
public void run() {
for(Clock clock:clocks){
showActualTimeDate(clock);
}
handler.postDelayed(this,refreshStep);
}
};
//============================================ public members ====================================================================
/**
* add a clock to the list of updating clocks
* @param clockFace - the place where the time or date will be shown
* @param format - the format of the time/date
* @return
*/
public static boolean registerClock(TextView clockFace, String format){
if (clockFace==null) return false;
if(clocks.contains(clockFace)){
// old clockFace
clocks.get(clocks.indexOf(clockFace)).format=format;
} else {
// new clockFace
clocks.add(new Clock(clockFace, format));
}
return true;
}
/**
* remove a clock from the updating list
* @param clockFace
* @return
*/
public static boolean unRegisterClock(TextView clockFace){
if (clockFace==null) return false;
if(clocks.contains(clockFace)){
// found clockFace
clocks.remove(clocks.indexOf(clockFace));
} else {
// not found clockFace
return false;
}
return true;
}
/**
* put in the "place" the actual date/time in the appropriate "format"
* @param place
* @param format
*/
public static void showActualTimeDate(Clock clock){
if (clock.clockFace==null) return;
if (clock.clockFace.getVisibility()!=TextView.VISIBLE) return;
Date thisDate=new Date();
SimpleDateFormat df=new SimpleDateFormat(clock.format);
clock.clockFace.setText(df.format(thisDate));
}
/**
* start the ticking for all clocks
* @param step the tick interval
*/
public static void start(long step) {
refreshStep=step;
handler.removeCallbacks(mUpdateTimeTask);
handler.postDelayed(mUpdateTimeTask, 0);
}
/**
* Stopping ticking all clocks (not removing them)
* the calling could be put somewhere in onStop
*/
public static void stop() {
handler.removeCallbacks(mUpdateTimeTask);
}
}
import java.text.simpleDataFormat;
导入java.util.ArrayList;
导入java.util.Date;
导入android.os.Handler;
导入android.widget.TextView;
/**
*用于在不同布局上创建和刷新许多不同字段的类,
*可以以不同格式保存实际时间和/或日期
*格式应如中所示http://developer.android.com/reference/java/text/SimpleDateFormat.html.
*只有当前字段和可见字段被实现,因此在关闭活动后不需要清理时钟列表
*
*使用示例:
*
*Clock.registerClock((TextView)findViewById(R.id.TimeField),“HH:mm”);
*Clock.registerClock((TextView)findViewById(R.id.DateField),“d.M.yyyy EEE”);
*时钟启动(10000L);
*
*@作者彼得·甘努斯
*/
公共期末钟{
/**
*处理程序,它代替计时器工作并支持UI
*/
静态私有处理程序=新处理程序();
/**
*时间间隔
*/
静态私有长刷新步;
/**
*成对文本视图计时器+时间/日期格式
*/
私有文本视图时钟面;
私有字符串格式;
专用时钟(TextView时钟面,字符串格式){
this.clockFace=clockFace;
this.format=format;
}
//下面是包含可视计时器的视图列表,这些可视计时器应保持为实际状态
静态私有ArrayList时钟=新ArrayList();
/**
*根据实际时间值,按照其格式填充所有计时器字段。
*/
静态私有Runnable mUpdateTimeTask=new Runnable(){
公开募捐{
用于(时钟:时钟){
显示实际时间(时钟);
}
postDelayed(这个,refreshStep);
}
};
//======================================================================================================公共成员====================================================================
/**
*将时钟添加到更新时钟列表中
*@param clockFace-显示时间或日期的位置
*@param format-时间/日期的格式
*@返回
*/
公共静态布尔寄存器锁(TextView时钟面,字符串格式){
if(clockFace==null)返回false;
if(时钟包含(时钟面)){
//旧钟表面
clocks.get(clocks.indexOf(clockFace)).format=format;
}否则{
//新钟面
添加(新时钟(时钟面,格式));
}
返回true;
}
/**
*从更新列表中删除时钟
*@param钟面
*@返回
*/
公共静态布尔注销时钟(TextView时钟面){
if(clockFace==null)返回false;
if(时钟包含(时钟面)){
//发现钟面
时钟。移除(时钟。索引(时钟面));
}否则{
//找不到钟面
返回false;
}
返回true;
}
/**
*以适当的“格式”将实际日期/时间填入“位置”
*@param place
*@param格式
*/
公共静态无效显示实际时间(时钟){
if(clock.clockFace==null)返回;
if(clock.clockFace.getVisibility()!=TextView.VISIBLE)返回;
日期thisDate=新日期();
SimpleDataFormat df=新的SimpleDataFormat(clock.format);
clock.clockFace.setText(df.format(thisDate));
}
/**
*开始所有时钟的滴答声
*@param步进刻度间隔
*/
公共静态无效开始(长步长){
刷新步骤=步骤;
removeCallbacks(mUpdateTimeTask);
handler.postDelayed(mUpdateTimeTask,0);
}
/**
*停止计时所有时钟(不移除它们)
*电话可以放在桌面上的某个地方
*/
公共静态void stop(){
removeCallbacks(mUpdateTimeTask);
}
}
你的意思是我创建了一个名为onTouch和overide的新方法吗??