2014-10-16 94 views
-1

我是Android新手,目前正在学习画布,我想创建以下类型的图片。 我搜索了它,但无法得到正确的例子。如何使用画布绘制图片

enter image description here

谁能帮助我?我如何创建它?

+1

到目前为止你有什么?代码和输出 – Yazan 2014-10-16 13:10:12

+0

得到了答案!!!!!!!!!! – Jaydeep 2014-10-17 07:36:02

回答

2

看到这个代码第一:

public class CustomView extends View { 

    public static final int NO_OF_VERTICAL_CIRCLES = 5; 
    public static final int NO_OF_HORIZONTAL_CIRCLES = 4; 

    public static final float RADIUS = 60f; 

    private Paint mPaintImage; 

    private RectF[] rectangles = new RectF[NO_OF_HORIZONTAL_CIRCLES 
      * NO_OF_VERTICAL_CIRCLES]; 

    // Just creating a view for drawing 
    public CustomView(Context context) { 
     super(context); 
     init(); 
    } 

    public CustomView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     init(); 
    } 

    public CustomView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     init(); 
    } 

    private void init() { 
     // This is paint for your drawing 
     mPaintImage = new Paint(); 

     mPaintImage.setAntiAlias(true); 
     mPaintImage.setFilterBitmap(true); 
     mPaintImage.setDither(true); 
     // color for circle 
     mPaintImage.setColor(Color.RED); 
     // fill style 
     mPaintImage.setStyle(Paint.Style.FILL); 

    } 

    @Override 
    protected void onSizeChanged(int w, int h, int oldw, int oldh) { 

     // Account for padding 
     float xpad = (float) (getPaddingLeft() + getPaddingRight()); 
     float ypad = (float) (getPaddingTop() + getPaddingBottom()); 
     // getting area for drawing you can also get it using getWidth() and getHeight() 
     float ww = (float) w - xpad; 
     float hh = (float) h - ypad; 

     Creating Rectangle for drawing your circles 
     for (short i = 0; i < NO_OF_HORIZONTAL_CIRCLES; i++) { 
      for (short j = 0; j < NO_OF_VERTICAL_CIRCLES; j++) { 
       // calculating horizontal centers 
       float horizontalCenter = RADIUS + i * (ww - RADIUS * 2) 
         /(NO_OF_HORIZONTAL_CIRCLES - 1); 
       // calculating vertical centers 
       float verticalCenter = RADIUS + j * (hh - RADIUS * 2) 
         /(NO_OF_VERTICAL_CIRCLES - 1); 

       rectangles[j + NO_OF_VERTICAL_CIRCLES * i] = new RectF(
         horizontalCenter - RADIUS, verticalCenter - RADIUS, 
         horizontalCenter + RADIUS, verticalCenter + RADIUS); 

      } 
     } 

     super.onSizeChanged(w, h, oldw, oldh); 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     // Here drawing circles 
     for (RectF rect : rectangles) { 
      canvas.drawCircle(rect.centerX(), rect.centerY(), RADIUS, 
        mPaintImage); 
     } 
     super.onDraw(canvas); 
    } 

这里,我所做的电网只是划分筛网,然后找出中心,并相应地绘制圆。

以类似的方式,您可以绘制位图,矩形。

您还可以在空位图上绘图。

我希望这会有所帮助。

PS(截屏)::

enter image description here

+0

感谢您的帮助。它的工作。 – Jaydeep 2014-10-17 05:11:51

2

既然你是新的Android和这里学习画布是延伸的观点一个简单的类代码 - 你可以覆盖帆布的onDraw画在画布上

import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Paint; 
import android.util.AttributeSet; 
import android.view.View; 

public class MyView extends View { 

    private Paint p; 

    public MyView(Context context, AttributeSet aSet) { 

        super(context, aSet); 

        //it's best not to create any new objects in the onDraw 

        //initialize as class variables 

        p = new Paint(); 

    } 


     @Override 

     public void onDraw(Canvas canvas) { 

      //paint specs 

      p.setColor(Color.BLACK); 
      p.setStyle(Paint.Style.FILL); 
      p.setDither(true); 
      p.setAntiAlias(true); 
      p.setStrokeJoin(Paint.Join.ROUND); 
      p.setStrokeCap(Paint.Cap.ROUND); 

      // space between dots 

      int delta = 50; 

      // dot radius 

      int r = 10; 

      // 1st row 

      canvas.drawCircle(canvas.getWidth()/4, canvas.getHeight()/4, r, p); 
      canvas.drawCircle(canvas.getWidth()/4+delta, canvas.getHeight()/4, r, p); 
      canvas.drawCircle(canvas.getWidth()/4+delta*2, canvas.getHeight()/4, r, p); 
      canvas.drawCircle(canvas.getWidth()/4+delta*3, canvas.getHeight()/4, r, p); 

      // 2nd row 

      canvas.drawCircle(canvas.getWidth()/4, canvas.getHeight()/4+delta, r, p); 
      canvas.drawCircle(canvas.getWidth()/4+delta, canvas.getHeight()/4+delta, r, p); 
      canvas.drawCircle(canvas.getWidth()/4+delta*2, canvas.getHeight()/4+delta, r, p); 
      canvas.drawCircle(canvas.getWidth()/4+delta*3, canvas.getHeight()/4+delta, r, p); 

      // 3rd row 

      canvas.drawCircle(canvas.getWidth()/4, canvas.getHeight()/4+delta*2, r, p); 
      canvas.drawCircle(canvas.getWidth()/4+delta, canvas.getHeight()/4+delta*2, r, p); 
      canvas.drawCircle(canvas.getWidth()/4+delta*2, canvas.getHeight()/4+delta*2, r, p); 
      canvas.drawCircle(canvas.getWidth()/4+delta*3, canvas.getHeight()/4+delta*2, r, p); 


      // 4th row 

      canvas.drawCircle(canvas.getWidth()/4, canvas.getHeight()/4+delta*3, r, p); 
      canvas.drawCircle(canvas.getWidth()/4+delta, canvas.getHeight()/4+delta*3, r, p); 
      canvas.drawCircle(canvas.getWidth()/4+delta*2, canvas.getHeight()/4+delta*3, r, p); 
      canvas.drawCircle(canvas.getWidth()/4+delta*3, canvas.getHeight()/4+delta*3, r, p); 

     } 


} 
+0

感谢您的帮助。它的工作。 – Jaydeep 2014-10-17 05:12:25