ainActivity.java 重要 程式碼
public class MainActivity extends Activity{
private Thread thread1;
private Thread thread2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final FrameLayout frame=new FrameLayout(this);
setContentView(frame);
frame.findViewById(R.id.framelayout1);
final MyView view=new MyView(this);
thread1=new Thread(new Runnable(){
public void run(){
view.setOnTouchListener(new OnTouchListener(){
public boolean onTouch(View v,MotionEvent event){
++view.r;
view.invalidate();
return true;
}
});
thread2=new Thread(new Runnable(){
--view.r;
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
thread1.start();
thread2.start();
frame.addView(view);
if (view.r==1)
{
thread2.interrupt();
if (view.r==100){
thread1.interrupt();
class MyView extends View{
int r=10,x,y;
public MyView(Context context){
super(context);
x=context.getResources().getDisplayMetrics().widthPixels/2;
y=context.getResources().getDisplayMetrics().heightPixels/2;
protected void onDraw(Canvas canvas){
super.onDraw(canvas);
canvas.drawColor(Color.WHITE);
Paint paint=new Paint();
paint.setAntiAlias(true);
paint.setStrokeWidth(3);
paint.setStyle(Style.STROKE);
paint.setColor(Color.BLUE);
canvas.drawCircle(x,y,r,paint);
ainActivity.java 重要 程式碼
public class MainActivity extends Activity{
private Thread thread1;
private Thread thread2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final FrameLayout frame=new FrameLayout(this);
setContentView(frame);
frame.findViewById(R.id.framelayout1);
final MyView view=new MyView(this);
thread1=new Thread(new Runnable(){
@Override
public void run(){
view.setOnTouchListener(new OnTouchListener(){
@Override
public boolean onTouch(View v,MotionEvent event){
++view.r;
view.invalidate();
return true;
}
});
}
});
thread2=new Thread(new Runnable(){
@Override
public void run(){
--view.r;
view.invalidate();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
thread1.start();
thread2.start();
frame.addView(view);
if (view.r==1)
{
thread2.interrupt();
}
if (view.r==100){
thread1.interrupt();
}
}
}
class MyView extends View{
int r=10,x,y;
public MyView(Context context){
super(context);
x=context.getResources().getDisplayMetrics().widthPixels/2;
y=context.getResources().getDisplayMetrics().heightPixels/2;
}
@Override
protected void onDraw(Canvas canvas){
super.onDraw(canvas);
canvas.drawColor(Color.WHITE);
Paint paint=new Paint();
paint.setAntiAlias(true);
paint.setStrokeWidth(3);
paint.setStyle(Style.STROKE);
paint.setColor(Color.BLUE);
canvas.drawCircle(x,y,r,paint);
}
}