我先说一下情况,我自定义 了一个View类MyView,在其中用来绘图,如果flag=1,隔一秒重绘一次。否则不绘图。
代码如下:1、if(flag==1)
{
handler.postDelayed(runnable, delay);
} runnable = new Runnable() {
@Override
public void run() { invalidate();
}
};
2、在Activity中有两个按钮,分别用来设置MyView中flag,btn12_6用来暂停绘图,btn12_7用来恢复绘图。
代码如下:
bn12_6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View source) {
if (flag == 0) { MyView.flag = 0; flag = 1;
} }
});
bn12_7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View source) {
if (flag == 1) { MyView.flag = 1; flag = 0;
}
结果是能暂停绘图,但就是恢复不了?我觉得很诡异,请问各位大虾,这是怎么回事?
代码如下:1、if(flag==1)
{
handler.postDelayed(runnable, delay);
} runnable = new Runnable() {
@Override
public void run() { invalidate();
}
};
2、在Activity中有两个按钮,分别用来设置MyView中flag,btn12_6用来暂停绘图,btn12_7用来恢复绘图。
代码如下:
bn12_6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View source) {
if (flag == 0) { MyView.flag = 0; flag = 1;
} }
});
bn12_7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View source) {
if (flag == 1) { MyView.flag = 1; flag = 0;
}
结果是能暂停绘图,但就是恢复不了?我觉得很诡异,请问各位大虾,这是怎么回事?
1、if(flag==1)
{
handler.postDelayed(runnable, delay);
} runnable = new Runnable() {
@Override
public void run() { invalidate(); }
如果这个线程还在运行的话,可能你的flag值没更新到
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Service;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Bundle;
import android.os.Handler;
import android.os.Vibrator;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;public class EcgActivity extends Activity
{
static int pauseflag = 0; static int alarmflag = 0;
Vibrator vibrator; @Override
public void onCreate(Bundle savedInstanceState)
{ super.onCreate(savedInstanceState);
setContentView( R.layout.ecg);
Button bn12_2 = (Button)findViewById(R.id.button12_2);
bn12_2.setTextColor(Color.RED);
Button bn12_3 = (Button)findViewById(R.id.button12_3);
bn12_3.setTextColor(Color.YELLOW);
Button bn12_4 = (Button)findViewById(R.id.button12_4);
bn12_4.setTextColor(Color.GREEN);
Button bn12_5 = (Button)findViewById(R.id.button12_5);
bn12_5.setTextColor(Color.MAGENTA);
final Button bn12_6 = (Button)findViewById(R.id.button12_6);
bn12_6.setTextColor(Color.BLUE);
final Button bn12_7 = (Button)findViewById(R.id.button12_7);
bn12_7.setTextColor(Color.BLUE); TextView textView12_10 = (TextView)findViewById(R.id.textView12_10);
textView12_10.setTextColor(Color.RED);
TextView textView12_11 = (TextView)findViewById(R.id.textView12_11);
textView12_11.setTextColor(Color.RED);
TextView textView12_12 = (TextView)findViewById(R.id.textView12_12);
textView12_12.setTextColor(Color.RED); final Button bn12_13 = (Button)findViewById(R.id.button12_13);
bn12_13.setTextColor(Color.RED); bn12_2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View source) {
MyView.enlarge = 50;
MyView.up_px = 50;
}
});
bn12_3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View source) {
MyView.enlarge = 100;
MyView.up_px = 100;
}
});
bn12_4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View source) {
MyView.enlarge = 200;
MyView.up_px = 200;
}
});
bn12_5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View source) {
MyView.enlarge = 400;
MyView.up_px = 400;
}
});
bn12_6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View source) {
if (pauseflag == 0) { MyView.delay = 3000;
pauseflag = 1;
} }
});
bn12_7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View source) {
if (pauseflag == 1) { MyView.delay = 1000; pauseflag = 0;
} }
}); bn12_13.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View source) {
if (alarmflag == 0) {
bn12_13.setText("关闭报警");
alarmflag = 1; bn12_13.setTextColor(Color.BLUE);
} else {
bn12_13.setText("打开报警");
alarmflag = 0;
bn12_13.setTextColor(Color.RED);
vibrator.cancel();
}
if (alarmflag == 1) {
vibrator = (Vibrator)getSystemService(Service.VIBRATOR_SERVICE);
vibrator.vibrate(10000);
}
}
}); }
}@SuppressLint({
"DrawAllocation", "DrawAllocation", "DrawAllocation"
})
class MyView extends View {
private final Paint paint1 = new Paint(); static int enlarge = 50; static int up_px = 50; static int delay = 1000; Handler handler = new Handler(); Runnable runnable;
float y = 0; private boolean useCenter = true; @Override
public boolean onTouchEvent(MotionEvent event) {
if (useCenter) {
useCenter = false;
paint1.setColor(Color.RED); paint1.setStrokeWidth(6); } else {
useCenter = true;
paint1.setColor(Color.BLUE); paint1.setStrokeWidth(2); } return super.onTouchEvent(event);
} public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
setBackgroundColor(Color.BLACK); paint1.setColor(Color.BLACK);
paint1.setStrokeWidth(2); paint1.setStyle(Paint.Style.STROKE); } @Override
protected void onDraw(Canvas canvas) { canvas.drawColor(Color.YELLOW);
RectF bounds = new RectF();
canvas.translate(0 - bounds.bottom, 420 - bounds.top);
y = enlarge * (float)Math.random() - up_px; for (int i = 0; i < 16; i++) {
if (i == 0) {
canvas.drawLine(0, y, 30, y = enlarge * (float)Math.random() - up_px, paint1);
} else {
canvas.drawLine(i * 30, y, (i + 1) * 30,
y = enlarge * (float)Math.random() - up_px, paint1);
} } handler.postDelayed(runnable, delay);
runnable = new Runnable() {
@Override
public void run() { invalidate();
}
};
}
}
我说一下我想达到的效果,什么都不做的话,自动绘图,这个已经实现,当我按下冻结按钮,停止绘图,如果我按下释放按钮,继续绘图,就这么简单,由于上面的方法实现不了,还老卡,我改成现在这样,但是不是我理想中的结果
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Service;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Bundle;
import android.os.Handler;
import android.os.Vibrator;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;public class EcgActivity extends Activity
{
static int pauseflag = 0; static int alarmflag = 0;
Vibrator vibrator; @Override
public void onCreate(Bundle savedInstanceState)
{ super.onCreate(savedInstanceState);
setContentView( R.layout.ecg);
Button bn12_2 = (Button)findViewById(R.id.button12_2);
bn12_2.setTextColor(Color.RED);
Button bn12_3 = (Button)findViewById(R.id.button12_3);
bn12_3.setTextColor(Color.YELLOW);
Button bn12_4 = (Button)findViewById(R.id.button12_4);
bn12_4.setTextColor(Color.GREEN);
Button bn12_5 = (Button)findViewById(R.id.button12_5);
bn12_5.setTextColor(Color.MAGENTA);
final Button bn12_6 = (Button)findViewById(R.id.button12_6);
bn12_6.setTextColor(Color.BLUE);
final Button bn12_7 = (Button)findViewById(R.id.button12_7);
bn12_7.setTextColor(Color.BLUE); TextView textView12_10 = (TextView)findViewById(R.id.textView12_10);
textView12_10.setTextColor(Color.RED);
TextView textView12_11 = (TextView)findViewById(R.id.textView12_11);
textView12_11.setTextColor(Color.RED);
TextView textView12_12 = (TextView)findViewById(R.id.textView12_12);
textView12_12.setTextColor(Color.RED); final Button bn12_13 = (Button)findViewById(R.id.button12_13);
bn12_13.setTextColor(Color.RED); bn12_2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View source) {
MyView.enlarge = 50;
MyView.up_px = 50;
}
});
bn12_3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View source) {
MyView.enlarge = 100;
MyView.up_px = 100;
}
});
bn12_4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View source) {
MyView.enlarge = 200;
MyView.up_px = 200;
}
});
bn12_5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View source) {
MyView.enlarge = 400;
MyView.up_px = 400;
}
});
bn12_6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View source) {
if (pauseflag == 0) { MyView.delay = 3000;
pauseflag = 1;
} }
});
bn12_7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View source) {
if (pauseflag == 1) { MyView.delay = 1000; pauseflag = 0;
} }
}); bn12_13.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View source) {
if (alarmflag == 0) {
bn12_13.setText("关闭报警");
alarmflag = 1; bn12_13.setTextColor(Color.BLUE);
} else {
bn12_13.setText("打开报警");
alarmflag = 0;
bn12_13.setTextColor(Color.RED);
vibrator.cancel();
}
if (alarmflag == 1) {
vibrator = (Vibrator)getSystemService(Service.VIBRATOR_SERVICE);
vibrator.vibrate(10000);
}
}
}); }
}@SuppressLint({
"DrawAllocation", "DrawAllocation", "DrawAllocation"
})
class MyView extends View {
private final Paint paint1 = new Paint(); static int enlarge = 50; static int up_px = 50; static int delay = 1000; Handler handler = new Handler(); Runnable runnable;
float y = 0; private boolean useCenter = true; @Override
public boolean onTouchEvent(MotionEvent event) {
if (useCenter) {
useCenter = false;
paint1.setColor(Color.RED); paint1.setStrokeWidth(6); } else {
useCenter = true;
paint1.setColor(Color.BLUE); paint1.setStrokeWidth(2); } return super.onTouchEvent(event);
} public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
setBackgroundColor(Color.BLACK); paint1.setColor(Color.BLACK);
paint1.setStrokeWidth(2); paint1.setStyle(Paint.Style.STROKE); } @Override
protected void onDraw(Canvas canvas) { canvas.drawColor(Color.YELLOW);
RectF bounds = new RectF();
canvas.translate(0 - bounds.bottom, 420 - bounds.top);
y = enlarge * (float)Math.random() - up_px; for (int i = 0; i < 16; i++) {
if (i == 0) {
canvas.drawLine(0, y, 30, y = enlarge * (float)Math.random() - up_px, paint1);
} else {
canvas.drawLine(i * 30, y, (i + 1) * 30,
y = enlarge * (float)Math.random() - up_px, paint1);
} } handler.postDelayed(runnable, delay);
runnable = new Runnable() {
@Override
public void run() { invalidate();
}
};
}
}
我说一下我想达到的效果,什么都不做的话,自动绘图,这个已经实现,当我按下冻结按钮,停止绘图,如果我按下释放按钮,继续绘图,就这么简单,由于上面的方法实现不了,还老卡,我改成现在这样,但是不是我理想中的结果