2012-07-15 6 views
5

사용자 정의 View에 선을 그려 시도하고 있습니다. 여기서는 하나의 세그먼트로 구성된 Path을 작성하여 PathShape을 작성한 후 에 붙이겠습니다. 안에 Canvas을 그려야합니다. 그러나 이것은 작동하지 않습니다. 내 예를 보라.PathShape와 함께 ShapeDrawable을 사용하여 커스텀보기에 선을 그리는 방법은 무엇입니까?

package com.example.test; 

import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Paint; 
import android.graphics.Path; 
import android.graphics.drawable.ShapeDrawable; 
import android.graphics.drawable.shapes.PathShape; 
import android.util.Log; 
import android.view.View; 

public class TestView extends View { 

    private Path mPath = null; 
    private Paint mPaint = null; 
    private PathShape mPathShape = null; 
    private ShapeDrawable mShapeDrawable = null; 

    public TestView(Context context) { 
     super(context); 
    } 

    private void init() { 
     int width = this.getWidth()/2; 
     int height = this.getHeight()/2; 

     Log.d("init", String.format("width: %d; height: %d", width, height)); 

     this.mPath = new Path(); 
     this.mPath.moveTo(0, 0); 
     this.mPath.lineTo(width, height); 

     this.mPaint = new Paint(); 
     this.mPaint.setColor(Color.RED); 

     this.mPathShape = new PathShape(this.mPath, 1, 1); 

     this.mShapeDrawable = new ShapeDrawable(this.mPathShape); 
     this.mShapeDrawable.getPaint().set(this.mPaint); 
     this.mShapeDrawable.setBounds(0, 0, width, height); 
    } 

    @Override 
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) { 
     super.onLayout(changed, left, top, right, bottom); 

     // Doing this here because in the constructor we don't have the width and height of the view, yet 
     this.init(); 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     super.onDraw(canvas); 

     Log.d("onDraw", "Drawing"); 

     // This works, but won't let me do what I'm really trying to do 
     canvas.drawLine(0.0f, 0.0f, this.getWidth()/2.0f, this.getHeight()/2.0f, this.mPaint); 

     // This should work, but does not 
     //this.mPathShape.draw(canvas, this.mPaint); 

     // This should work, but does not 
     //this.mShapeDrawable.draw(canvas); 
    } 

} 

당신이 onDraw() 방법에 내 의견에서 볼 수 있듯이, 어느 것도 PathShape이나 ShapeDrawable을 사용하면 Canvas 상에 Path 실제로 작동 그릴 수 없습니다. 시도 할 때 아무 것도 그려지지 않습니다. 왜 아무 생각 없어?

내가 테스트중인 기기는 Android 4.1.1입니다.

+0

단지 궁금해서 - onDraw 안에 새로운 Paint 객체를 만들려고 했습니까? 나는 그 일을하고 있고 비트 맵으로 성공을 거두고 있지만 그것이 필요한지 아닌지 확실하지 않았다. – JavaCoderEx

+0

onDraw에서 객체를 생성하고 싶지 않았기 때문에 나는 그것을 가지고 있지 않았다. (실제로 Lint의 경고를 일으킨다.) 그러나, 나는 지금 그것을 시도하고 동일한 결과를 얻었다. 성공하지 못했습니다. – Daniel

+0

좀 더 조사하고 응답이 부족한 상황에서 이것이 단순히 부러 졌다고 생각하고 버그 보고서를 제출했습니다. http://code.google.com/p/android/issues/detail?id=35229 – Daniel

답변

12

두 가지 문제가 있습니다.

첫 번째는 Paint 스타일입니다. 기본값은 Paint.Stroke.FILL이지만 줄 바꿈으로 채울 내용이 없습니다.

this.mPaint.setStyle(Paint.Style.STROKE); 

두 번째 문제는 PathShape의 표준 높이와 너비가 정확하지 것입니다 :이 (감사 Romain Guy)을 추가 할 필요가 있었다. 나는 이것에 관해 the documentation를 읽었지만 올바르게 이해하지 못했다. 첫 번째 문제를 해결하면이 점이 분명 해졌습니다. 이를 전체 뷰에 걸쳐 그리기 때문에 내 사용자 정의 뷰의 높이와 너비로 설정하면이 문제가 해결됩니다. 또한 일치하도록 ShapeDrawable의 범위를 변경해야했습니다.

this.mShapeDrawable.setBounds(0, 0, this.getWidth(), this.getHeight()); 

this.mPathShape = new PathShape(this.mPath, this.getWidth(), this.getHeight()); 

는 희망이 미래에 다른 사람을 도움이됩니다.

+0

감사합니다. 나는 똑같은 문제를 안고 있었다. –

+1

경로의 크기가 최대 가로 세로 비율보다 크면 LogCat에서 경고가 표시되는 경우가 발생할 수있는 또 다른 문제입니다. –

관련 문제