package com.panxuesen.gravity2;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.Toast;
public class MainActivity extends Activity implements SensorEventListener
{
// 定义系统的Sensor管理器
SensorManager sensorManager;
private Button button1,button2,button3,button4;
private FrameLayout frame1,frame2,frame3;
private float[] values;
private int coordinatex=0,coordinatey,tempx=0,tempy=0,tempy2=0,tempy3=0;
private int coordinatey2;
private int coordinatey3;
private int canvaswidth,canvasheight;
private boolean isInterrupt=true;
private int enlargeMultiple=10;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 获取系统的传感器管理服务
sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
frame1 = (FrameLayout) findViewById(R.id.mainFrameLayout1);
frame2 = (FrameLayout) findViewById(R.id.mainFrameLayout2);
frame3 = (FrameLayout) findViewById(R.id.mainFrameLayout3);
button1 = (Button) findViewById(R.id.forwardButton);
button1.setOnClickListener(listener);
button2 = (Button) findViewById(R.id.backButton);
button2.setOnClickListener(listener);
button3 = (Button) findViewById(R.id.enlargeButton);
button3.setOnClickListener(listener);
button4 = (Button) findViewById(R.id.reduceButton);
button4.setOnClickListener(listener);
canvaswidth = this.getResources().getDisplayMetrics().widthPixels;
canvasheight = (this.getResources().getDisplayMetrics().heightPixels - 80) / 3;
DrawBack my2 = new DrawBack(MainActivity.this);
frame1.addView(my2);
my2 = null;
my2 = new DrawBack(MainActivity.this);
frame2.addView(my2);
my2 = null;
my2 = new DrawBack(MainActivity.this);
frame3.addView(my2);
my2 = null;
//Toast.makeText(MainActivity.this,""+my.getHeight(),Toast.LENGTH_LONG).show();
}
private View.OnClickListener listener=new View.OnClickListener(){
@Override
public void onClick(View p1)
{
// TODO: Implement this method
Button button=(Button)p1;
switch (button.getId())
{
case R.id.forwardButton:
beginDraw1();
break;
case R.id.backButton:
if(thread!=null)
{
thread.interrupt();
thread = null;
isInterrupt = true;
}
break;
case R.id.enlargeButton:
enlargeMultiple++;
Toast.makeText(MainActivity.this, "放大倍数为" + enlargeMultiple, Toast.LENGTH_SHORT).show();
break;
case R.id.reduceButton:
enlargeMultiple--;
if (enlargeMultiple < 1)
enlargeMultiple = 1;
Toast.makeText(MainActivity.this, "放大倍数为" + enlargeMultiple, Toast.LENGTH_SHORT).show();
break;
}
}
};
private class MyView1 extends View
{
private Paint paint;
private int x1,x2,y1,y2;
public MyView1(Context context)
{
super(context);
}
public void setParameter(int x1, int y1, int x2, int y2)
{
this.x1 = x1;this.x2 = x2;
this.y1 = y1;this.y2 = y2;
}
@Override
protected void onDraw(Canvas canvas)
{
// TODO: Implement this method
super.onDraw(canvas);
paint = new Paint();
paint.setColor(Color.BLUE);
paint.setAntiAlias(true);
paint.setStrokeWidth(3);
paint.setStyle(android.graphics.Paint.Style.FILL);
canvas.drawLine((float)x1, (float)y1, (float)x2, (float)y2, paint);
}
}
private class DrawBack extends View
{
private Paint paint;
public DrawBack(Context context)
{
super(context);
}
@Override
protected void onDraw(Canvas canvas)
{
// TODO: Implement this method
super.onDraw(canvas);
paint = new Paint();
paint.setColor(Color.BLACK);
paint.setAntiAlias(true);
paint.setStrokeWidth(3);
paint.setStyle(android.graphics.Paint.Style.FILL);
canvas.drawColor(Color.WHITE);
canvas.drawLine(0, canvasheight / 2, canvaswidth, canvasheight / 2, paint);
//canvaswidth = canvas.getWidth();
//canvasheight = canvas.getHeight();
}
}
private Thread thread=null;
private void beginDraw1()
{
thread = new Thread(new Runnable(){
@Override
public void run()
{
while (!isInterrupt)
{
// TODO: Implement this method
if (coordinatex > canvaswidth)
{
coordinatex = 0;
tempx = 0;
Message m=new Message();
m.what = 0x111;
handler.sendMessage(m);
}
else
{
if (tempx == 0)
{
tempy = coordinatey;
tempy2 = coordinatey2;
tempy3 = coordinatey3;
}
coordinatey = (int)(canvasheight / 2 - values[0] * enlargeMultiple);
coordinatey2 = (int)(canvasheight / 2 - values[1] * enlargeMultiple);
coordinatey3 = (int)(canvasheight / 2 - values[2] * enlargeMultiple);
Message m=new Message();
int data[]=new int[]{tempx,tempy,coordinatex,coordinatey,tempy2,tempy3,coordinatey2,coordinatey3};
m.obj = data;
m.what = 0x101;
handler.sendMessage(m);
tempx = coordinatex;tempy = coordinatey;
tempy2 = coordinatey2;tempy3 = coordinatey3;
coordinatex += 5;
}
try
{
thread.sleep(50);
}
catch (InterruptedException e)
{}
}
}
});
//my2 = null;
if (isInterrupt)
{
thread.start();
isInterrupt = false;
}
}
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg)
{
// TODO: Implement this method
super.handleMessage(msg);
if (msg.what == 0x101)
{
int []data;
data = (int[]) msg.obj;
//my = null;
MyView1 my = new MyView1(MainActivity.this);
my.setParameter(data[0], data[1], data[2], data[3]);
//my.setParameter(tempx, tempy, coordinatex, coordinatey);
frame1.addView(my);
my = null;
my = new MyView1(MainActivity.this);
my.setParameter(data[0], data[4], data[2], data[6]);
//my.setParameter(tempx, tempy2, coordinatex, coordinatey2);
frame2.addView(my);
my = null;
my = new MyView1(MainActivity.this);
my.setParameter(data[0], data[5], data[2], data[7]);
//my.setParametr(tempx, tempy3, coordinatex, coordinatey3);
frame3.addView(my);
my = null;
//my.invalidate();//重绘
}
else
{
//my2 = null;
DrawBack my2 = new DrawBack(MainActivity.this);
frame1.addView(my2);
my2 = null;
my2 = new DrawBack(MainActivity.this);
frame2.addView(my2);
my2 = null;
my2 = new DrawBack(MainActivity.this);
frame3.addView(my2);
my2 = null;
//my2.invalidate();
}
}
};
@Override
public void onSensorChanged(SensorEvent event)
{
// TODO: Implement this method
values = event.values;
// StringBuilder sb = new StringBuilder();
// sb.append("X方向上的加速度:");
// sb.append(values[0]);
// sb.append("\nY方向上的加速度:");
// sb.append(values[1]);
// sb.append("\nZ方向上的加速度:");
// sb.append(values[2]);
}
@Override
public void onAccuracyChanged(Sensor p1, int p2)
{
// TODO: Implement this method
}
@Override
protected void onResume()
{
// TODO: Implement this method
super.onResume();
// 为系统的加速度传感器注册监听器
sensorManager.registerListener