2011-02-22 3 views
1

필자는 4 개의 동심원 (원한다면 레이더 효과)을 포함하려는 RelativeLayout을 가지고 있습니다. RelativeLayout을 숨기고 표시하고 싶습니다. 그래서 Canvas와 Paint를 사용하여 정확히 어떻게 RelativeLayout에 원을 그리는 지 궁금합니다. 누군가가 작동하는 방법의 수명주기를 설명 할 수 있다면 매우 유용 할 것입니다. 지금 당장 :Android : 기본 원형 개요를 RelativeLayout에 그리기

setContentView(R.layout.applayout); 
myRelLayout = (RelativeLayout) findViewById(R.id.RLRadar); 
Canvas canvas = new Canvas(); 
Paint circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG); 
circlePaint.setColor(0xFF000000); 
circlePaint.setStyle(Style.STROKE); 
canvas.drawCircle((float) centerX, (float) centerY, (float) (maxRadius/4), circlePaint); 

나는 올바른 길을 가고 있습니까? 캔버스를 일종의 ImageView로 변환하여 RelativeLayout에 추가해야합니까? 아니면 여기서 완전히 벗어나지?

도움 주셔서 감사합니다.

편집 : 다음은 작동 코드입니다.

// Add the radar to the RadarRL 
Picture picture = new Picture(); 
Canvas canvas = picture.beginRecording(screenWidth, screenHeight); 
// Draw on the canvas 
Paint circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG); 
circlePaint.setColor(0xFF000000); 
circlePaint.setStyle(Style.STROKE); 
circlePaint.setStrokeWidth((float) 5.0); 
canvas.drawCircle((float) centerX, (float) centerY, (float) (maxRadius/4), circlePaint); 
canvas.drawCircle((float) centerX, (float) centerY, (float) ((3 * maxRadius)/4), circlePaint); 
canvas.drawCircle((float) centerX, (float) centerY, (float) (maxRadius/2), circlePaint); 
canvas.drawCircle((float) centerX, (float) centerY, (float) (maxRadius), circlePaint);  
picture.endRecording(); 

답변

2

사용자 지정보기를 만들지 않으려면 setBackgroundDrawable을 사용할 수 있습니다. 예를 들어, PicturePictureDrawable를 사용하여 :

// Create the picture.  
Picture picture = new Picture(); 
Canvas canvas = beginRecording(WIDTH, HEIGHT); 
// Draw on the canvas. 
picture.endRecording(); 

// Now set it as the background drawable. 
PictureDrawable drawable = new PictureDrawable(picture); 
relativeLayout = (RelativeLayout) findViewById(R.id.RLRadar); 
relativeLayout.setBackgroundDrawable(drawable); 
+0

안녕하세요 팀, 내가이 구현하지만, 아무것도 내 RelativeLayout의에 표시되지 않습니다했습니다. 내가 사용한 코드로 원래 게시물을 편집했습니다. 내가 잘못한 것을 확인할 수 있니? – Rockmaninoff

+0

Nevermind가 작동했습니다! – Rockmaninoff

+0

'setBackgroundDrawable'은 더 이상 사용되지 않습니다. –

2

당신은 Custom View를 작성하고 View.draw(Canvas) 메서드를 재정의해야합니다. 그런 다음 canvas 매개 변수를 사용하여 레이더 효과를 그릴 수 있습니다. 뷰는 모든 레이아웃에 배치 할 수 있습니다.

관련 문제