如何使在android系统定制的指南针视图 [英] How to make a customized compass view in android
本文介绍了如何使在android系统定制的指南针视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我用一个简单的罗盘应用程序的工作。我需要设置为我选择一个指南针视图类传感器活动指南针视图。但它只是画一个圆圈和罗盘视图线。我需要用我自己绘制的图像有人请帮我解决这个问题要更换此圆和直线来定制这个观点。
我的视图类
进口android.content.Context;
进口android.graphics.Canvas;
进口android.graphics.Color;
进口android.graphics.Paint;
进口android.view.View;公共类MyCompassView扩展视图{ 私人涂料粉刷;
私人浮动位置= 0; 公共MyCompassView(上下文的背景下){
超级(上下文);
在里面();
} 私人无效的init(){
油漆=新的油漆();
paint.setAntiAlias(真);
paint.setStrokeWidth(2);
paint.setTextSize(25);
paint.setStyle(Paint.Style.STROKE);
paint.setColor(Color.WHITE);
} @覆盖
保护无效的onDraw(帆布油画){
INT XPOINT = getMeasuredWidth()/ 2;
INT yPoint = getMeasuredHeight()/ 2; 浮半径=(浮点)(Math.max(XPOINT,yPoint)* 0.6);
canvas.drawCircle(XPOINT,yPoint,半径,油漆);
canvas.drawRect(0,0,getMeasuredWidth(),getMeasuredHeight(),漆); // 3.143是圆一个很好的近似
canvas.drawLine(XPOINT,
yPoint,
(浮点)(XPOINT +半径
* Math.sin((双)(位置),/ 180 * 3.143)),
(浮点)(yPoint - 半径
* Math.cos((双)(位置),/ 180 * 3.143)),涂料); canvas.drawText(将String.valueOf(位置),XPOINT,yPoint,油漆);
} 公共无效了updateData(浮动位){
this.position =位置;
无效();
}}
解决方案
以上code已被德precated。更新code可这里
XML布局activty_main
<的RelativeLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:背景=#FFF>
<的TextView
机器人:ID =@ + ID / tvHeading
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_centerHorizontal =真
机器人:layout_marginBottom =40dp
机器人:layout_marginTop =20dp
机器人:文字=标题:0.0/>
< ImageView的
机器人:ID =@ + ID / imageViewCompass
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_below =@ + ID / tvHeading
机器人:layout_centerHorizontal =真
机器人:SRC =@绘制/ img_compass/>
< / RelativeLayout的>
MainActivity
公共类MainActivity扩展活动实现SensorEventListener { //定义显示屏部件罗盘图片
私人ImageView的形象; //记录翻了指南针画角
私人浮动currentDegree = 0F; //设备传感器管理
私人的SensorManager mSensorManager; TextView的tvHeading; @覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main); //
图像=(ImageView的)findViewById(R.id.main_iv); // TextView中会告诉用户他走向什么程度
tvHeading =(的TextView)findViewById(R.id.tvHeading); //初始化Android设备传感器功能
mSensorManager =(的SensorManager)getSystemService(SENSOR_SERVICE);
} @覆盖
保护无效onResume(){
super.onResume(); //为系统的方向传感器注册的侦听器
mSensorManager.registerListener(这一点,mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION)
SensorManager.SENSOR_DELAY_GAME);
} @覆盖
保护无效的onPause(){
super.onPause(); //停止监听器和节省电池
mSensorManager.unregisterListener(本);
} @覆盖
公共无效onSensorChanged(SensorEvent事件){ //获得绕z轴的角度旋转
浮度= Math.round(event.values [0]); tvHeading.setText(标题:+ Float.toString(度)+度); //创建一个旋转动画(反向转度度)
RotateAnimation RA =新RotateAnimation(
currentDegree,
-度,
Animation.RELATIVE_TO_SELF,0.5F,
Animation.RELATIVE_TO_SELF,
0.5F); //动画需要多长时间的地方
ra.setDuration(210); //的保留状态结束后设置动画
ra.setFillAfter(真); //开始动画
image.startAnimation(RA);
currentDegree = -degree; } @覆盖
公共无效onAccuracyChanged(传感器传感器,精度INT){
//在不使用
}
}
I am working with a simple compass application. I need to set the compass view with the sensor activity for that i choose a compass view class. But it is only drawing a circle and a line for the compass view. I need to customize this view by replacing this circle and line with my own drawable images somebody please help me to fix this.
My view class
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;
public class MyCompassView extends View {
private Paint paint;
private float position = 0;
public MyCompassView(Context context) {
super(context);
init();
}
private void init() {
paint = new Paint();
paint.setAntiAlias(true);
paint.setStrokeWidth(2);
paint.setTextSize(25);
paint.setStyle(Paint.Style.STROKE);
paint.setColor(Color.WHITE);
}
@Override
protected void onDraw(Canvas canvas) {
int xPoint = getMeasuredWidth() / 2;
int yPoint = getMeasuredHeight() / 2;
float radius = (float) (Math.max(xPoint, yPoint) * 0.6);
canvas.drawCircle(xPoint, yPoint, radius, paint);
canvas.drawRect(0, 0, getMeasuredWidth(), getMeasuredHeight(), paint);
// 3.143 is a good approximation for the circle
canvas.drawLine(xPoint,
yPoint,
(float) (xPoint + radius
* Math.sin((double) (-position) / 180 * 3.143)),
(float) (yPoint - radius
* Math.cos((double) (-position) / 180 * 3.143)), paint);
canvas.drawText(String.valueOf(position), xPoint, yPoint, paint);
}
public void updateData(float position) {
this.position = position;
invalidate();
}
}
解决方案
The above code has been deprecated . an updated code is available here
XML Layout activty_main
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff" >
<TextView
android:id="@+id/tvHeading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginBottom="40dp"
android:layout_marginTop="20dp"
android:text="Heading: 0.0" />
<ImageView
android:id="@+id/imageViewCompass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tvHeading"
android:layout_centerHorizontal="true"
android:src="@drawable/img_compass" />
</RelativeLayout>
MainActivity
public class MainActivity extends Activity implements SensorEventListener {
// define the display assembly compass picture
private ImageView image;
// record the compass picture angle turned
private float currentDegree = 0f;
// device sensor manager
private SensorManager mSensorManager;
TextView tvHeading;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//
image = (ImageView) findViewById(R.id.main_iv);
// TextView that will tell the user what degree is he heading
tvHeading = (TextView) findViewById(R.id.tvHeading);
// initialize your android device sensor capabilities
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
}
@Override
protected void onResume() {
super.onResume();
// for the system's orientation sensor registered listeners
mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
SensorManager.SENSOR_DELAY_GAME);
}
@Override
protected void onPause() {
super.onPause();
// to stop the listener and save battery
mSensorManager.unregisterListener(this);
}
@Override
public void onSensorChanged(SensorEvent event) {
// get the angle around the z-axis rotated
float degree = Math.round(event.values[0]);
tvHeading.setText("Heading: " + Float.toString(degree) + " degrees");
// create a rotation animation (reverse turn degree degrees)
RotateAnimation ra = new RotateAnimation(
currentDegree,
-degree,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF,
0.5f);
// how long the animation will take place
ra.setDuration(210);
// set the animation after the end of the reservation status
ra.setFillAfter(true);
// Start the animation
image.startAnimation(ra);
currentDegree = -degree;
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// not in use
}
}
这篇关于如何使在android系统定制的指南针视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文