Tôi đang cố gắng vẽ một đường trong một tùy chỉnh View
. Ở đây tôi đã tạo ra một đơn giản Path
chỉ với một đoạn duy nhất, tạo ra một PathShape
từ đó, và cuối cùng bị mắc kẹt vào một ShapeDrawable
với ý định sử dụng để vẽ trênbên trong onDraw()
. Tuy nhiên, điều này không hoạt động. Xem ví dụ của tôi ở đây.Bạn định sử dụng ShapeDrawable bằng PathShape để vẽ một đường trên Chế độ xem tùy chỉnh như thế nào?
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);
}
}
Như bạn có thể nhìn thấy từ ý kiến của tôi trong phương pháp onDraw()
, không sử dụng PathShape
lẫn ShapeDrawable
để vẽ Path
vào Canvas
thực sự hoạt động. Không có gì được rút ra khi tôi cố gắng. Có ai có bất kỳ ý tưởng tại sao?
Thiết bị tôi đang thử nghiệm này đang chạy Android 4.1.1.
Chỉ cần tò mò - bạn đã thử tạo một đối tượng Paint mới bên trong onDraw chưa. Tôi đang làm điều đó và thành công với bitmap nhưng không chắc liệu nó có cần thiết hay không. – JavaCoderEx
Tôi đã không, vì tôi không muốn tạo ra các đối tượng trong onDraw (nó thực sự gây ra một cảnh báo từ Lint). Tuy nhiên, tôi đã thử nó ngay bây giờ và có kết quả tương tự. Không thành công. – Daniel
Sau khi một số điều tra thêm, và thiếu phản ứng ở đây, tôi nghĩ rằng điều này chỉ đơn giản là bị hỏng và tôi đã gửi một báo cáo lỗi. http://code.google.com/p/android/issues/detail?id=35229 – Daniel