2012-07-15 4 views
5

मैं एक कस्टम 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 का उपयोग कर आकर्षित करने के लिए PathCanvas पर वास्तव में काम करता है। कोशिश करते समय कुछ भी नहीं खींचा जाता है। क्या किसी को कोई विचार है क्यों?

डिवाइस मैं पर Android 4.1.1 चल रहा है इस परीक्षण कर रहा हूँ।

+0

बस उत्सुक - आप OnDraw के भीतर एक नया पेंट वस्तु बनाने की कोशिश की है। मैं ऐसा कर रहा हूं और बिटमैप्स के साथ सफलता प्राप्त कर रहा हूं लेकिन यह सुनिश्चित नहीं था कि यह आवश्यक था या नहीं। – JavaCoderEx

+0

मैंने नहीं किया था, क्योंकि मैं ऑर्डर ड्रा में ऑब्जेक्ट नहीं बनाना चाहता था (यह वास्तव में लिंट से चेतावनी का कारण बनता है)। हालांकि, मैंने अभी कोशिश की और एक ही परिणाम मिला। कोई सफलता नहीं। – 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.mPathShape = new PathShape(this.mPath, this.getWidth(), this.getHeight()); 

और

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

उम्मीद है कि यह भविष्य में किसी और में मदद करता है।

+0

इस के लिए धन्यवाद! मेरे पास एक ही समस्या थी। –

+1

एक अन्य समस्या जो इसे प्रकट नहीं कर सकती है वह यह है कि यदि आपके पथ के आयाम आपके अधिकतम पहलू अनुपात से बड़े हैं, जिसके परिणामस्वरूप आपके लॉगकाट में चेतावनियां होती हैं। –

संबंधित मुद्दे