Campass指南针绘制

public class CampassActivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.campass); } }

campass.xml:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <com.gao.apidemo.view.CampassView android:id="@+id/campass" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout>public class CampassView extends View{ private Paint mMarkerPaint; private Paint mTextPaint; private Paint mCirclePaint; private String mNorthText; private String mSouthText; private String mEastText; private String mWestText; private int mTextHeight; public CampassView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); initView(); } public CampassView(Context context, AttributeSet attrs) { super(context, attrs); initView(); } public CampassView(Context context) { super(context); initView(); } private void initView() { setFocusable(true); Resources resources=getResources(); mNorthText=resources.getString(R.string.cardinal_north); mSouthText=resources.getString(R.string.cardinal_south); mEastText=resources.getString(R.string.cardinal_east); mWestText=resources.getString(R.string.cardinal_west); mCirclePaint=new Paint(Paint.ANTI_ALIAS_FLAG); mCirclePaint.setColor(resources.getColor(R.color.background_color)); mCirclePaint.setStrokeWidth(1); mCirclePaint.setStyle(Style.FILL_AND_STROKE); mTextPaint=new Paint(Paint.ANTI_ALIAS_FLAG); mTextPaint.setColor(resources.getColor(R.color.text_color)); mTextHeight=(int)mTextPaint.measureText("yY"); mMarkerPaint=new Paint(Paint.ANTI_ALIAS_FLAG); mMarkerPaint.setColor(resources.getColor(R.color.marker_color)); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // The compass is a circle that fills as much space as possible. // Set the measured dimensions by figuring out the shortest boundary, // height or width. int measureWidth=measure(widthMeasureSpec); int measureHeight=measure(heightMeasureSpec); int d=Math.min(measureWidth, measureHeight); setMeasuredDimension(d, d); } private int measure(int measureSpec) { int result=0; // Decode the measurement specifications. int specMode=MeasureSpec.getMode(measureSpec); int specSize=MeasureSpec.getSize(measureSpec); if (specMode==MeasureSpec.UNSPECIFIED) { result=200; }else { result=specSize; } return result; } @Override protected void onDraw(Canvas canvas) { int px=getMeasuredWidth()/2; int py=getMeasuredHeight()/2; int radius=Math.min(px, py); // Draw the background canvas.drawCircle(px, py, radius, mCirclePaint); int textWidth=(int)mTextPaint.measureText("W"); int cardinalX=px-textWidth/2; int cardinalY=py-radius+mTextHeight; // Draw the marker every 15 degrees and text every 45. for (int i = 0; i < 24; i++) { // Draw a marker. canvas.drawLine(px, py-radius, px, py-radius+10, mMarkerPaint); canvas.save(); canvas.translate(0,mTextHeight); // Draw the cardinal points if (i%6==0) { String dirText=""; switch (i) { case 0:{ dirText=mNorthText; int arrowY=2*mTextHeight; canvas.drawLine(px, arrowY, px-5, 3*mTextHeight, mTextPaint); canvas.drawLine(px, arrowY, px+5, 3*mTextHeight, mTextPaint); } break; case 6: dirText=mEastText; break; case 12: dirText=mSouthText; break; case 18: dirText=mWestText; break; default: break; } canvas.drawText(dirText, cardinalX, cardinalY, mTextPaint); }else if (i%3==0) { // Draw the text every alternate 45deg String angle=String.valueOf(15*i); int angleTextWidth=(int)mTextPaint.measureText(angle); int angleTextX=px-angleTextWidth/2; int angleTextY=py-radius+mTextHeight; canvas.drawText(angle, angleTextX, angleTextY, mTextPaint); } canvas.restore(); canvas.rotate(15, px, py); } } } 效果图:


在做的时候将

canvas.restore(); canvas.rotate(15, px, py);写反了

canvas.rotate(15, px, py); canvas.restore();结果所有的数字和字母都画在了12点钟方向,也就是说我旋转了15度,但是马上就又restore恢复到了原来的角度。

canvas.rotate可以将这个看成是在没有旋转的情况下画,然后又旋转了相应的角度。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值