我的观点课
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,getMeasureDWIDth(),getMeasuredHeight(),paint); // 3.143 is a good approximation for the circle canvas.drawline(xPoint,(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,paint); } public voID updateData(float position) { this.position = position; invalIDate(); }}解决方法 上述代码已被弃用.更新的代码可用 here
XML布局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>
主要活动
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,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中制作自定义指南针视图全部内容,希望文章能够帮你解决如何在android中制作自定义指南针视图所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)