사용자 정의 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입니다.
단지 궁금해서 - onDraw 안에 새로운 Paint 객체를 만들려고 했습니까? 나는 그 일을하고 있고 비트 맵으로 성공을 거두고 있지만 그것이 필요한지 아닌지 확실하지 않았다. – JavaCoderEx
onDraw에서 객체를 생성하고 싶지 않았기 때문에 나는 그것을 가지고 있지 않았다. (실제로 Lint의 경고를 일으킨다.) 그러나, 나는 지금 그것을 시도하고 동일한 결과를 얻었다. 성공하지 못했습니다. – Daniel
좀 더 조사하고 응답이 부족한 상황에서 이것이 단순히 부러 졌다고 생각하고 버그 보고서를 제출했습니다. http://code.google.com/p/android/issues/detail?id=35229 – Daniel