मैं एक कस्टम View
में एक रेखा खींच करने का प्रयास कर रहा हूँ। यहां मैंने केवल एक ही सेगमेंट के साथ एक सरल Path
बनाया है, जिसने PathShape
बनाया है, और onDraw()
के अंदर Canvas
पर आकर्षित करने के इरादे से इसे 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
का उपयोग कर आकर्षित करने के लिए Path
Canvas
पर वास्तव में काम करता है। कोशिश करते समय कुछ भी नहीं खींचा जाता है। क्या किसी को कोई विचार है क्यों?
डिवाइस मैं पर Android 4.1.1 चल रहा है इस परीक्षण कर रहा हूँ।
बस उत्सुक - आप OnDraw के भीतर एक नया पेंट वस्तु बनाने की कोशिश की है। मैं ऐसा कर रहा हूं और बिटमैप्स के साथ सफलता प्राप्त कर रहा हूं लेकिन यह सुनिश्चित नहीं था कि यह आवश्यक था या नहीं। – JavaCoderEx
मैंने नहीं किया था, क्योंकि मैं ऑर्डर ड्रा में ऑब्जेक्ट नहीं बनाना चाहता था (यह वास्तव में लिंट से चेतावनी का कारण बनता है)। हालांकि, मैंने अभी कोशिश की और एक ही परिणाम मिला। कोई सफलता नहीं। – Daniel
कुछ और जांच के बाद, और यहां प्रतिक्रिया की कमी के बाद, मुझे लगता है कि यह बस टूटा हुआ है और मैंने एक बग रिपोर्ट सबमिट की है। http://code.google.com/p/android/issues/detail?id=35229 – Daniel