2011-08-10 11 views
9

मैं path.addCircle() और path.addRect() का उपयोग कर कैनवास पर सर्कल और आयताकार खींचने में सक्षम हूं। जैसे कि मैं स्पर्श पर चाहता हूं मैं सक्षम हूं त्रिकोण, स्टार, वर्ग और दिल खींचने के लिए। मैं यह कैसे कर सकता हूं? मुझे नमूना उदाहरणों के साथ एक रास्ता दें। धन्यवादएंड्रॉइड कैनवास पर त्रिकोण, स्टार, स्क्वायर, दिल को कैसे आकर्षित करें

+0

अरे nickfrancis.me, मैं एक ही बात करना चाहता हूँ बनाएँ। क्या आप कृपया मेरे साथ कोड साझा कर सकते हैं? धन्यवाद। – anddev

उत्तर

9

आपको उन आंकड़ों के पीछे गणित पता होना है। त्रिकोण और स्टार आकर्षित करने के लिए काफी आसान हैं। यहां बताया गया है कि आप दिल कैसे आकर्षित कर सकते हैं: http://www.mathematische-basteleien.de/heart.htm

विशेष पथ बनाने के लिए आपको अंक, इलिप्स आदि जोड़कर उन्हें बनाना चाहिए। कैनवास निर्दिष्ट पथ के क्लिपिंग मास्क का समर्थन करता है, ताकि आप दिल के क्लिपिंग मुखौटा को सेट कर सकें , मैट्रिक्स के पथ को धक्का दें, दिल की सामग्री खींचें, और फिर इसे फिर से पॉप करें।

है यही तो मैं Andriod पर एक नकली 2D पेज कर्ल प्रभाव को प्राप्त करने कर रहा हूँ: http://code.google.com/p/android-page-curl/

आशा इस मदद करता है!

1

इसके अलावा दीर्घवृत्त और आयताकार आप उन दो (के रूप में कम से कम) की आवश्यकता होगी करने के लिए:

drawLine(float startX, float startY, float stopX, float stopY, Paint paint)
drawLines(float[] pts, int offset, int count, Paint paint)

उदाहरण: How to draw a line in android

प्रलेखन पर कैनवस: http://developer.android.com/reference/android/graphics/Canvas.html

+0

मुझे आपको नहीं मिला। Plz मुझे समझाओ। –

+0

आपको जटिल आकारों को आदिम लोगों को विभाजित करने की आवश्यकता है। उदाहरण के लिए आप दो आधा सर्कल और दो लाइनों का उपयोग करके दिल खींच सकते हैं। त्रिकोण - 3 बिंदुओं का चयन करें और उन्हें drawLines() या ड्रॉलाइन() के लिए बेहतर पास सरणी पीएफ पॉइंट्स से कनेक्ट करें; – Im0rtality

21

के लिए भविष्य के प्रत्यक्ष उत्तर तलाशने वाले, मैंने लगभग एक सममित सितारा खींचा है , कैनवास ing के रूप में छवि में दिखाया गया:

Star Image

मुख्य उपकरण पथ उपयोग कर रहा है।

Paint paint = new Paint(); 
paint.setColor(Color.WHITE); 
paint.setAntiAlias(true); 
paint.setStyle(Paint.Style.STROKE); 

Path path = new Path(); 

तो फिर तुम में OnDraw आप पथ का उपयोग कर सकते है जैसे मैं नीचे कार्य करें:

आप यह मानते हुए सेटअप। यह कैनवास

@Override 
protected void onDraw(Canvas canvas) { 

    float mid = getWidth()/2; 
    float min = Math.min(getWidth(), getHeight()); 
    float fat = min/17; 
    float half = min/2; 
    float rad = half - fat; 
    mid = mid - half; 

    paint.setStrokeWidth(fat); 
    paint.setStyle(Paint.Style.STROKE); 

    canvas.drawCircle(mid + half, half, rad, paint); 

    path.reset(); 

    paint.setStyle(Paint.Style.FILL); 


     // top left 
     path.moveTo(mid + half * 0.5f, half * 0.84f); 
     // top right 
     path.lineTo(mid + half * 1.5f, half * 0.84f); 
     // bottom left 
     path.lineTo(mid + half * 0.68f, half * 1.45f); 
     // top tip 
     path.lineTo(mid + half * 1.0f, half * 0.5f); 
     // bottom right 
     path.lineTo(mid + half * 1.32f, half * 1.45f); 
     // top left 
     path.lineTo(mid + half * 0.5f, half * 0.84f); 

     path.close(); 
     canvas.drawPath(path, paint); 

    super.onDraw(canvas); 

} 
+2

अन्य उत्तरों का कहना है कि आपको प्रत्येक कॉल के बाद चाल को कॉल करने की आवश्यकता है ताकि एक पूर्ण बहुभुज बनाने के लिए, जो मेरे लिए काम नहीं करता है। शुक्र है, आपके जवाब ने चाल की है! – npace

8

इस विधि किसी भी आकार के लिए ठीक से स्केल दिया चौड़ाई का एक वर्ग के अंदर दिया कोनों की संख्या के साथ एक पथ वापस आ जाएगी होगा। छोटे त्रिज्या और ऐसी चीजों को संभालने के लिए और पैरामीटर जोड़ें।

private Path createStarBySize(float width, int steps) { 
    float halfWidth = width/2.0F; 
    float bigRadius = halfWidth; 
    float radius = halfWidth/2.0F; 
    float degreesPerStep = (float) Math.toRadians(360.0F/(float) steps); 
    float halfDegreesPerStep = degreesPerStep/2.0F; 
    Path ret = new Path(); 
    ret.setFillType(FillType.EVEN_ODD); 
    float max = (float) (2.0F* Math.PI); 
    ret.moveTo(width, halfWidth); 
    for (double step = 0; step < max; step += degreesPerStep) { 
     ret.lineTo((float)(halfWidth + bigRadius * Math.cos(step)), (float)(halfWidth + bigRadius * Math.sin(step))); 
     ret.lineTo((float)(halfWidth + radius * Math.cos(step + halfDegreesPerStep)), (float)(halfWidth + radius * Math.sin(step + halfDegreesPerStep))); 
    } 
    ret.close(); 
    return ret; 
} 
+0

इस तरह के एक भयानक दिनचर्या में कोई अपवर्तक नहीं है! गजब का! – rupps

0

आप एक वर्ग के अंदर एक स्टार आकर्षित करने के लिए की जरूरत है, तो आप नीचे दिए गए कोड का उपयोग कर सकते हैं।

posX और posY वर्ग के ऊपरी बाएं कोने के लिए निर्देशांक हैं जो स्टार की युक्तियों को संलग्न करता है (वर्ग वास्तव में तैयार नहीं होता है)।

size वर्ग की चौड़ाई और ऊंचाई है।

a स्टार की शीर्ष युक्ति है। पथ घड़ी की दिशा में बनाया गया है।

यह किसी भी सही समाधान से नहीं है, लेकिन यह काम बहुत जल्दी हो जाता है।

public void drawStar(float posX, float posY, int size, Canvas canvas){ 

      int hMargin = size/9; 
      int vMargin = size/4; 

      Point a = new Point((int) (posX + size/2), (int) posY); 
      Point b = new Point((int) (posX + size/2 + hMargin), (int) (posY + vMargin)); 
      Point c = new Point((int) (posX + size), (int) (posY + vMargin)); 
      Point d = new Point((int) (posX + size/2 + 2*hMargin), (int) (posY + size/2 + vMargin/2)); 
      Point e = new Point((int) (posX + size/2 + 3*hMargin), (int) (posY + size)); 
      Point f = new Point((int) (posX + size/2), (int) (posY + size - vMargin)); 
      Point g = new Point((int) (posX + size/2 - 3*hMargin), (int) (posY + size)); 
      Point h = new Point((int) (posX + size/2 - 2*hMargin), (int) (posY + size/2 + vMargin/2)); 
      Point i = new Point((int) posX, (int) (posY + vMargin)); 
      Point j = new Point((int) (posX + size/2 - hMargin), (int) (posY + vMargin)); 

      Path path = new Path(); 
      path.moveTo(a.x, a.y); 
      path.lineTo(b.x, b.y); 
      path.lineTo(c.x, c.y); 
      path.lineTo(d.x, d.y); 
      path.lineTo(e.x, e.y); 
      path.lineTo(f.x, f.y); 
      path.lineTo(f.x, f.y); 
      path.lineTo(g.x, g.y); 
      path.lineTo(h.x, h.y); 
      path.lineTo(i.x, i.y); 
      path.lineTo(j.x, j.y); 
      path.lineTo(a.x, a.y); 

      path.close(); 

      canvas.drawPath(path, paint); 
} 
8

हर कोई के लिए है कि एक दिल के आकार की जरूरत है:

import android.content.Context; 
    import android.graphics.Canvas; 
    import android.graphics.Color; 
    import android.graphics.Paint; 
    import android.graphics.Paint.Style; 
    import android.graphics.Path; 
    import android.view.View; 

    public class Heart extends View { 

     private Path path; 

     private Paint paint; 

     public Heart(Context context) { 
      super(context); 

      path = new Path(); 
      paint = new Paint(Paint.ANTI_ALIAS_FLAG); 
     } 

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

       // Fill the canvas with background color 
       canvas.drawColor(Color.WHITE); 
       paint.setShader(null); 

       float width = getContext().getResources().getDimension(R.dimen.heart_width); 
       float height = getContext().getResources().getDimension(R.dimen.heart_height); 

       // Starting point 
       path.moveTo(width/2, height/5); 

       // Upper left path 
       path.cubicTo(5 * width/14, 0, 
         0, height/15, 
         width/28, 2 * height/5); 

       // Lower left path 
       path.cubicTo(width/14, 2 * height/3, 
         3 * width/7, 5 * height/6, 
         width/2, height); 

       // Lower right path 
       path.cubicTo(4 * width/7, 5 * height/6, 
         13 * width/14, 2 * height/3, 
         27 * width/28, 2 * height/5); 

       // Upper right path 
       path.cubicTo(width, height/15, 
         9 * width/14, 0, 
         width/2, height/5); 

       paint.setColor(Color.RED); 
       paint.setStyle(Style.FILL); 
       canvas.drawPath(path, paint); 

      } 
    } 

सभी नंबरों के लिए क्षमा करें, लेकिन इन मेरे लिए सबसे अच्छा काम किया :) परिणाम इस प्रकार है:

enter image description here

+0

छवि फसल .. – Prabs

0

आकार वर्ग के उदाहरण का उपयोग करना बहुत अच्छा है))

@Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    HeartShape shape = new HeartShape(); 
    ShapeDrawable shapeDrawable = new ShapeDrawable(shape); 
    shapeDrawable.setColorFilter(new LightingColorFilter(0, Color.BLUE)); 

    LinearLayout linearLayout = new LinearLayout(this); 
    linearLayout.setLayoutParams(new LinearLayout.LayoutParams(350 * 3, 350 * 3)); 
    linearLayout.setBackground(shapeDrawable); 

    setContentView(linearLayout); 
    } 

एक आकार वर्ग था जो अच्छा हार्ट प्रस्तुत करना

public class HeartShape extends Shape { 

    private final int INVALID_SIZE = -1; 

    private Path mPath = new Path(); 
    private RectF mRectF = new RectF(); 

    private float mOldWidth = INVALID_SIZE; 
    private float mOldHeight = INVALID_SIZE; 

    private float mScaleX, mScaleY; 

    public HeartShape() { 

    } 

    @Override 
    public void draw(Canvas canvas, Paint paint) { 
    canvas.save(); 
    canvas.scale(mScaleX, mScaleY); 

    float width = mRectF.width(); 
    float height = mRectF.height(); 

    float halfWidth = width/2; 
    float halfHeight = height/2; 

    float stdDestX = 5 * width/14; 
    float stdDestY = 2 * height/3; 

    PointF point1 = new PointF(stdDestX, 0); 
    PointF point2 = new PointF(0, height/15); 
    PointF point3 = new PointF(stdDestX/5, stdDestY); 
    PointF point4 = new PointF(stdDestX, stdDestY); 

    // Starting point 
    mPath.moveTo(halfWidth, height/5); 

    mPath.cubicTo(point1.x, point1.y, point2.x, point2.y, width/28, 2 * height/5); 
    mPath.cubicTo(point3.x, point3.y, point4.x, point4.y, halfWidth, height); 

    canvas.drawPath(mPath, paint); 

    canvas.scale(-mScaleX, mScaleY, halfWidth, halfHeight); 
    canvas.drawPath(mPath, paint); 

    canvas.restore(); 
    } 

    @Override 
    protected void onResize(float width, float height) { 
    mOldWidth = mOldWidth == INVALID_SIZE ? width : Math.max(1, mOldWidth); 
    mOldHeight = mOldHeight == INVALID_SIZE ? height : Math.max(1, mOldHeight); 

    width = Math.max(1, width); 
    height = Math.max(1, height); 

    mScaleX = width/mOldWidth; 
    mScaleY = height/mOldHeight; 

    mOldWidth = width; 
    mOldHeight = height; 


    mRectF.set(0, 0, width, height); 
    } 

    @Override 
    public void getOutline(@NonNull Outline outline) { 
    // HeartShape not supported outlines 
    } 

    @Override 
    public HeartShape clone() throws CloneNotSupportedException { 
    HeartShape shape = (HeartShape) super.clone(); 
    shape.mPath = new Path(mPath); 
    return shape; 
    } 

} 

enter image description here

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