-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #818 from zhuoxw/master
- Loading branch information
Showing
11 changed files
with
301 additions
and
0 deletions.
There are no files selected for viewing
162 changes: 162 additions & 0 deletions
162
...ava/edu/hzuapps/androidworks/homeworks/net1314080903148/Net1314080903148MainActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
package net1314080903148.homeworks.androidworks.hzuapps.edu.net1314080903148; | ||
|
||
import android.support.v7.app.AppCompatActivity; | ||
import android.os.Bundle; | ||
import android.hardware.SensorListener; | ||
import android.hardware.SensorManager; | ||
import android.view.Menu; | ||
|
||
import net1314080903148.homeworks.androidworks.hzuapps.edu.net1314080903148.R; | ||
|
||
@SuppressWarnings("deprecation") | ||
|
||
public class Net1314080903148MainActivity extends AppCompatActivity { | ||
int k = 45; //灵敏度 | ||
Net1314080903148MainView mv; | ||
//真机 | ||
SensorManager mySensorManager; | ||
|
||
//测试时 | ||
// SensorManagerSimulator mySensorManager; | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_net1314080903148_main); | ||
mv = (Net1314080903148MainView) findViewById(R.id.mainView); | ||
|
||
mySensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);//真机 | ||
} | ||
|
||
|
||
private final SensorListener mSensorLisener =new SensorListener(){ | ||
|
||
@Override | ||
public void onAccuracyChanged(int sensor, int accuracy) { } | ||
|
||
public boolean isContain(int x, int y){//判断点是否在圆内 | ||
|
||
int tempx =(int) (x + mv.zhongBitmap2.getWidth()/2.0); | ||
int tempy =(int) (y + mv.zhongBitmap2.getWidth()/2.0); | ||
int ox = (int) (mv.zhong1_X+ mv.zhongBitmap1.getWidth()/2.0); | ||
int oy = (int) (mv.zhong1_X+ mv.zhongBitmap1.getWidth()/2.0); | ||
if(Math.sqrt((tempx-ox)*(tempx-ox)+(tempy-oy)*(tempy-oy))>(mv.zhongBitmap1.getWidth()/2.0-mv.zhongBitmap2.getWidth()/2.0)){ | ||
//不在圆内 | ||
return false; | ||
}else{ | ||
//在圆内时 | ||
return true; | ||
} | ||
} | ||
|
||
@Override | ||
public void onSensorChanged(int sensor, float[] values) { | ||
if(sensor == SensorManager.SENSOR_ORIENTATION){ | ||
double pitch = values[SensorManager.DATA_Y]; | ||
double roll = values[SensorManager.DATA_Z]; | ||
int x=0; int y=0;//临时变量,算中间水泡坐标时用 | ||
int tempX=0; int tempY=0;//下面气泡的临时变量 | ||
|
||
//开始调整x 的值 | ||
if(Math.abs(roll)<=k){ | ||
mv.shang2_X = mv.shang1_X //上面的 | ||
+ (int)(((mv.shangBitmap1.getWidth() | ||
-mv.shangBitmap2.getWidth())/2.0) | ||
-(((mv.shangBitmap1.getWidth() | ||
-mv.shangBitmap2.getWidth())/2.0)*roll)/k); | ||
|
||
x = mv.zhong1_X //中间的 | ||
+ (int)(((mv.zhongBitmap1.getWidth() | ||
-mv.zhongBitmap2.getWidth())/2.0) | ||
-(((mv.zhongBitmap1.getWidth() | ||
-mv.zhongBitmap2.getWidth())/2.0)*roll)/k); | ||
}else if(roll>k){ | ||
mv.shang2_X=mv.shang1_X; x = mv.zhong1_X; | ||
}else{ | ||
mv.shang2_X=mv.shang1_X+ | ||
mv.shangBitmap1.getWidth() | ||
- mv.shangBitmap2.getWidth(); | ||
|
||
x = mv.zhong1_X+ mv.zhongBitmap1.getWidth() | ||
- mv.zhongBitmap2.getWidth(); | ||
} | ||
|
||
//开始调整y 的值 | ||
|
||
if(Math.abs(pitch)<=k){ | ||
mv.zuo2_Y=mv.zuo1_Y //左面的 | ||
+ (int)(((mv.zuoBitmap1.getHeight() | ||
-mv.zuoBitmap2.getHeight())/2.0) | ||
+(((mv.zuoBitmap1.getHeight() | ||
-mv.zuoBitmap2.getHeight())/2.0)*pitch)/k); | ||
|
||
y =mv.zhong1_Y+ //中间的 | ||
(int)(((mv.zhongBitmap1.getHeight() | ||
-mv.zhongBitmap2.getHeight())/2.0) | ||
+(((mv.zhongBitmap1.getHeight() | ||
-mv.zhongBitmap2.getHeight())/2.0)*pitch)/k); | ||
}else if(pitch>k){ | ||
mv.zuo2_Y=mv.zuo1_Y | ||
+mv.zuoBitmap1.getHeight() | ||
-mv.zuoBitmap2.getHeight(); | ||
|
||
y=mv.zhong1_Y+mv.zhongBitmap1.getHeight() | ||
-mv.zhongBitmap2.getHeight(); | ||
}else{ | ||
mv.zuo2_Y = mv.zuo1_Y; y = mv.zhong1_Y; | ||
} | ||
|
||
//下面的 | ||
tempX = -(int) (((mv.xiaBitmap1.getWidth()/2-28)*roll | ||
+(mv.xiaBitmap1.getWidth()/2-28)*pitch)/k); | ||
|
||
tempY = -(int) ((-(mv.xiaBitmap1.getWidth()/2-28)*roll | ||
-(mv.xiaBitmap1.getWidth()/2-28)*pitch)/k); | ||
|
||
//限制下面的气泡范围 | ||
if(tempY>mv.xiaBitmap1.getHeight()/2-28){ | ||
tempY = mv.xiaBitmap1.getHeight()/2-28; | ||
} | ||
if(tempY < -mv.xiaBitmap1.getHeight()/2+28){ | ||
tempY = -mv.xiaBitmap1.getHeight()/2+28; | ||
} | ||
|
||
if(tempX > mv.xiaBitmap1.getWidth()/2-28){ | ||
tempX = mv.xiaBitmap1.getWidth()/2-28; | ||
} | ||
|
||
if(tempX < -mv.xiaBitmap1.getWidth()/2+28){ | ||
tempX = -mv.xiaBitmap1.getWidth()/2+28; | ||
} | ||
|
||
mv.xia2_X = tempX + mv.xia1_X + mv.xiaBitmap1.getWidth()/2 -mv.xiaBitmap2.getWidth()/2; | ||
mv.xia2_Y = tempY + mv.xia1_Y + mv.xiaBitmap1.getHeight()/2 - mv.xiaBitmap2.getWidth()/2; | ||
|
||
if(isContain(x, y)){//中间的水泡在圆内才改变坐标 | ||
mv.zhong2_X = x; mv.zhong2_Y = y; | ||
} | ||
mv.postInvalidate();//重绘MainView | ||
} | ||
} //传感器监听器类 | ||
|
||
}; | ||
|
||
@Override | ||
protected void onResume(){ //添加监听 | ||
mySensorManager.registerListener(mSensorLisener,SensorManager.SENSOR_ORIENTATION); | ||
super.onResume(); | ||
} | ||
|
||
@Override | ||
protected void onPause() { //取消监听 | ||
mySensorManager.unregisterListener (mSensorLisener); | ||
super.onPause(); | ||
} | ||
|
||
@Override | ||
public boolean onCreateOptionsMenu(Menu menu) { | ||
// Inflate the menu; this adds items to the action bar if it is present. | ||
getMenuInflater().inflate(R.menu.activity_level, menu); | ||
return true; | ||
} | ||
} |
117 changes: 117 additions & 0 deletions
117
...in/java/edu/hzuapps/androidworks/homeworks/net1314080903148/Net1314080903148MainView.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package net1314080903148.homeworks.androidworks.hzuapps.edu.net1314080903148; | ||
|
||
/** | ||
* Created by zhuoxw on 2016/5/12. | ||
*/ | ||
|
||
import android.content.Context; | ||
import android.graphics.Bitmap; | ||
import android.graphics.BitmapFactory; | ||
import android.graphics.Canvas; | ||
import android.graphics.Color; | ||
import android.graphics.Paint; | ||
import android.graphics.Paint.Style; | ||
import android.graphics.RectF; | ||
import android.util.AttributeSet; | ||
import android.view.View; | ||
|
||
import net1314080903148.homeworks.androidworks.hzuapps.edu.net1314080903148.R; | ||
|
||
public class Net1314080903148MainView extends View { | ||
Paint paint = new Paint(); //画笔 | ||
Bitmap shangBitmap1; //上面的大矩形 | ||
Bitmap shangBitmap2; //上面的气泡 | ||
Bitmap zuoBitmap1; //左面的大矩形图 | ||
Bitmap zuoBitmap2; //左面图的气泡 | ||
Bitmap zhongBitmap1; //中间的大圆图 | ||
Bitmap zhongBitmap2; //中间的小气泡 | ||
Bitmap xiaBitmap1; //右下的矩形图 | ||
Bitmap xiaBitmap2; //右下的气泡 | ||
|
||
//背景矩形的位置声明 | ||
int shang1_X = 60; //上面的大矩形图 | ||
int shang1_Y = 12; | ||
int zuo1_X = 12; //左面的大矩形图 | ||
int zuo1_Y = 60; | ||
int zhong1_X = 65; //中间的大圆图 | ||
int zhong1_Y = 65; | ||
int xia1_X = 145; //右下的矩形图 | ||
int xia1_Y = 145;//水泡的位置声明 | ||
int shang2_X; //上面的气泡XY 坐标 | ||
int shang2_Y; | ||
int zuo2_X; //左面图的气泡XY 坐标 | ||
int zuo2_Y; | ||
int zhong2_X; //中间的小气泡XY 坐标 | ||
int zhong2_Y; | ||
int xia2_X; //右下的气泡XY 坐标 | ||
int xia2_Y; | ||
|
||
public Net1314080903148MainView(Context context, AttributeSet attrs){ | ||
super(context, attrs); | ||
initBitmap(); //初始化图片资源 | ||
initLocation(); //初始化气泡的位置 | ||
} | ||
|
||
private void initBitmap(){ //初始化图片的方法 | ||
shangBitmap1 = BitmapFactory.decodeResource(getResources(), R.drawable.net1314080903148_level_shang1); | ||
shangBitmap2 = BitmapFactory.decodeResource(getResources(), R.drawable.net1314080903148_level_shang2); | ||
zuoBitmap1 = BitmapFactory.decodeResource(getResources(), R.drawable.net1314080903148_level_zuo1); | ||
zuoBitmap2 = BitmapFactory.decodeResource(getResources(), R.drawable.net1314080903148_level_zuo2); | ||
zhongBitmap1 = BitmapFactory.decodeResource(getResources(), R.drawable.net1314080903148_level_zhong1); | ||
zhongBitmap2 = BitmapFactory.decodeResource(getResources(), R.drawable.net1314080903148_level_zhong2); | ||
xiaBitmap1 = BitmapFactory.decodeResource(getResources(), R.drawable.net1314080903148_level_xia1); | ||
xiaBitmap2 = BitmapFactory.decodeResource(getResources(), R.drawable.net1314080903148_level_xia2); | ||
} | ||
|
||
private void initLocation(){ //初始化气泡位置的方法 | ||
shang2_X = shang1_X + shangBitmap1.getWidth()/2- shangBitmap2.getWidth()/2; | ||
shang2_Y = shang1_Y + shangBitmap1.getHeight()/2- shangBitmap2.getHeight()/2; | ||
zuo2_X = zuo1_X + zuoBitmap1.getWidth()/2- zuoBitmap2.getWidth()/2; | ||
zuo2_Y = zuo1_Y + zuoBitmap1.getHeight()/2- zuoBitmap2.getHeight()/2; | ||
zhong2_X = zhong1_X + zhongBitmap1.getWidth()/2- zhongBitmap2.getWidth()/2; | ||
zhong2_Y = zhong1_Y + zhongBitmap1.getHeight()/2- zhongBitmap2.getHeight()/2; | ||
xia2_X = xia1_X + xiaBitmap1.getWidth()/2- xiaBitmap2.getWidth()/2; | ||
xia2_Y = xia1_Y + xiaBitmap1.getHeight()/2- xiaBitmap2.getHeight()/2; | ||
} | ||
|
||
@Override | ||
protected void onDraw(Canvas canvas){//重写的绘制方法 | ||
super.onDraw(canvas); | ||
canvas.drawColor(Color.WHITE); //设置背景色为白色 | ||
|
||
paint.setColor(Color.BLUE); //设置画笔颜色 | ||
paint.setStyle(Style.STROKE); //设置画笔为不填充 | ||
|
||
|
||
//画背景矩形 | ||
canvas.drawBitmap(shangBitmap1, shang1_X,shang1_Y, paint); //上 | ||
canvas.drawBitmap(zuoBitmap1, zuo1_X,zuo1_Y, paint); //左 | ||
canvas.drawBitmap(zhongBitmap1, zhong1_X,zhong1_Y, paint); //中 | ||
canvas.drawBitmap(xiaBitmap1, xia1_X,xia1_Y, paint); //下 | ||
|
||
//开始绘制气泡 | ||
canvas.drawBitmap(shangBitmap2, shang2_X,shang2_Y, paint); //上 | ||
canvas.drawBitmap(zuoBitmap2, zuo2_X,zuo2_Y, paint); //左 | ||
canvas.drawBitmap(zhongBitmap2, zhong2_X,zhong2_Y, paint); //中 | ||
canvas.drawBitmap(xiaBitmap2, xia2_X, xia2_Y, paint);//下 | ||
|
||
paint.setColor(Color.GRAY);//设置画笔颜色用来绘制刻度 | ||
|
||
//绘制上面方框中的刻度 | ||
canvas.drawLine (shang1_X+shangBitmap1.getWidth()/2-7,shang1_Y, shang1_X+shangBitmap1.getWidth()/2-7,shang1_Y+shangBitmap1.getHeight()-2, paint); | ||
canvas.drawLine (shang1_X+shangBitmap1.getWidth()/2+7,shang1_Y, shang1_X+shangBitmap1.getWidth()/2+7,shang1_Y+shangBitmap1.getHeight()-2, paint); | ||
|
||
//绘制左面方框中的刻度 | ||
canvas.drawLine(zuo1_X,zuo1_Y+zuoBitmap1.getHeight()/2-7,zuo1_X+zuoBitmap1.getWidth()-2,zuo1_Y+zuoBitmap1.getHeight()/2-7, paint);canvas.drawLine(zuo1_X,zuo1_Y+zuoBitmap1.getHeight()/2+7,zuo1_X+zuoBitmap1.getWidth()-2,zuo1_Y+zuoBitmap1.getHeight()/2+7, paint); | ||
|
||
//绘制下面方框中的刻度 | ||
|
||
canvas.drawLine(xia1_X+xiaBitmap1.getWidth()/2-10,xia1_Y+xiaBitmap1.getHeight()/2-20,xia1_X+xiaBitmap1.getWidth()/2+20,xia1_Y+xiaBitmap1.getHeight()/2+10, paint); | ||
canvas.drawLine(xia1_X+xiaBitmap1.getWidth()/2-20,xia1_Y+xiaBitmap1.getHeight()/2-10,xia1_X+xiaBitmap1.getWidth()/2+10,xia1_Y+xiaBitmap1.getHeight()/2+20, paint); | ||
|
||
//中间圆圈中的刻度(小圆) | ||
RectF oval = new RectF(zhong1_X+zhongBitmap1.getWidth()/2-10,zhong1_Y+zhongBitmap1.getHeight()/2-10,zhong1_X+zhongBitmap1.getWidth()/2+10,zhong1_Y+zhongBitmap1.getHeight()/2+10); | ||
|
||
canvas.drawOval(oval, paint);//绘制基准线(圆) | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 22 additions & 0 deletions
22
app/src/main/res/layout/activity_net1314080903148_main.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:paddingBottom="@dimen/activity_vertical_margin" | ||
android:paddingLeft="@dimen/activity_horizontal_margin" | ||
android:paddingRight="@dimen/activity_horizontal_margin" | ||
android:paddingTop="@dimen/activity_vertical_margin" | ||
tools:context="net1314080903148.homeworks.androidworks.hzuapps.edu.net1314080903148.Net1314080903148MainActivity"> | ||
|
||
<net1314080903148.homeworks.androidworks.hzuapps.edu.net1314080903148.Net1314080903148MainView | ||
android:orientation="vertical" | ||
android:layout_width="match_parent" | ||
android:layout_height="fill_parent" | ||
android:id="@+id/mainView" | ||
android:layout_alignParentTop="true" | ||
android:layout_alignParentLeft="true" | ||
android:layout_alignParentStart="true" | ||
android:layout_marginTop="36dp"> | ||
</net1314080903148.homeworks.androidworks.hzuapps.edu.net1314080903148.Net1314080903148MainView> | ||
</RelativeLayout> |