Android Canvas图纸

时间:2015-11-13 08:24:52

标签: android android-canvas

我尝试使用canvas制作customView,但有一些奇怪的bug。我无法理解为什么......

 public class CustomDisplayView extends View {

      //circle and text colors
      private int circleCol, labelCol;
      private int circleCol2, labelCol2;
      //label text
      private String circleText;
      private String circleText2;
      //paint for drawing custom view
      private Paint circlePaint;
      //private Path mPath;


      final Path path = new Path();
      final Paint paint = new Paint();

      /**
       * Constructor method for custom view
       * - calls superclass method and adds custom processing
       *
       * @param context
       * @param attrs
       */
      public CustomDisplayView(Context context, AttributeSet attrs){
          super(context, attrs);

          //paint object for drawing in onDraw
          circlePaint = new Paint();

          //get the attributes specified in attrs.xml using the name we included
          TypedArray a = context.getTheme().obtainStyledAttributes(attrs,
                  R.styleable.CustomDisplayView, 0, 0);

          try {
              //get the text and colors specified using the names in attrs.xml


              circleText = a.getString(R.styleable.CustomDisplayView_circleLabel);
              circleText2 =  a.getString(R.styleable.CustomDisplayView_circleLabel2);

              circleCol = a.getInteger(R.styleable.CustomDisplayView_circleColor, 0);//0 is
  default
              labelCol = a.getInteger(R.styleable.CustomDisplayView_labelColor, 0);
              circleCol2 = a.getInteger(R.styleable.CustomDisplayView_circleColor2, 0);//0 is
  default
          } finally {
              a.recycle();
          }
      }

      /**
       * Override the onDraw method to specify custom view appearance using canvas
       */
      @Override
      protected void onDraw(Canvas canvas) {


          //get half of the width and height as we are working with a circle
          int viewWidthHalf = this.getMeasuredWidth()/2;
          int viewHeightHalf = this.getMeasuredHeight()/2;

          int viewWidth = this.getMeasuredWidth();
          int viewHeight = this.getMeasuredHeight();

          int viewWidthHalfS = this.getMeasuredWidth()/3;
          int viewHeightHalfS = this.getMeasuredHeight()/3;


          //get the radius as half of the width or height, whichever is smaller
          //subtract ten so that it has some space around it
          int radius = 0;
          int radius2 = 0;
          if(viewWidthHalf viewHeightHalf)
          {
              radius=viewHeightHalf-10;

          }
          else
          {
              radius=viewWidthHalf-10;
          }
          radius2=radius/2;

          int black= 0xff000000;

          //set drawing properties
          circlePaint.setStyle(Style.FILL);
          circlePaint.setAntiAlias(true);
          //set the paint color using the circle color specified
          circlePaint.setColor(0xff000000);
          //draw the circle using the properties defined
          canvas.drawCircle(viewWidthHalf, viewHeightHalf, radius, circlePaint);


  /*
          //set the paint color using the circle color specified circleCol circleCol2
          circlePaint.setColor(black);

          canvas.drawCircle(viewWidthHalfS, viewHeightHalfS, radius2, circlePaint);*/
          //set the text color using the color specified
          circlePaint.setColor(labelCol);
          //set text properties
          circlePaint.setTextAlign(Paint.Align.CENTER);
          circlePaint.setTextSize(50);




          Typeface plain = Typeface.createFromAsset(getContext().getAssets(),
  "fonts/digital.ttf");


          circlePaint.setTypeface(plain);
          canvas.drawText("43", viewWidthHalfS, viewHeightHalfS + (viewHeightHalfS / 9), circlePaint);

          circlePaint.setTextSize(80);

但是如果我用circleText2改变circleText应用程序崩溃......但没有区别。所以在下一个String我用circleText2更改circleText并且它崩溃了...为什么?我认为它必须简单易行,但我花了一天时间没有任何东西......

    canvas.drawText(circleText, viewWidthHalf, viewHeightHalf + (viewHeightHalf / 9), circlePaint);



    paint.setColor(Color.RED);
    paint.setAntiAlias(true);
    paint.setStyle(Paint.Style.STROKE);

   // path.moveTo(0, 0);
  //  path.lineTo(0, viewWidth);
  //  path.lineTo(viewHeight, viewWidth);
 //   path.lineTo(viewHeight, 0);
    path.cubicTo(300, 50, 100, 400, 400, 400);
   // path.close();

  //  path.moveTo(150, 150);
  //  path.lineTo(180, 150);
  //  path.lineTo(180, 180);
  //  path.lineTo(150, 180);
  //  path.close();

    //path.setFillType(Path.FillType.EVEN_ODD);
    canvas.drawPath(path, paint);


}

//each custom attribute should have a get and set method
//this allows updating these properties in Java
//we call these in the main Activity class

/**
 * Get the current circle color
 * @return color as an int
 */
public int getCircleColor(){
    return circleCol;
}

/**
 * Set the circle color
 * @param newColor new color as an int
 */
public void setCircleColor(int newColor){
    //update the instance variable
    circleCol=newColor;
    //redraw the view
    invalidate();
    requestLayout();
}

/**
 * Get the current text label color
 * @return color as an int
 */
public int getLabelColor(){
    return labelCol;
}

/**
 * Set the label color
 * @param newColor new color as an int
 */
public void setLabelColor(int newColor){
    //update the instance variable
    labelCol=newColor;
    //redraw the view
    invalidate();
    requestLayout();
}

/**
 * Get the current label text
 * @return text as a string
 */
public String getLabelText(){
    return circleText;
}

/**
 * Set the label text
 * @param newLabel text as a string
 */
public void setLabelText(String newLabel,int id){
    //update the instance variable

        circleText=newLabel;


}
public void setLabelText2(String newLabel,int id){
    //update the instance variable

    circleText2=newLabel;


}

public void redraw(){
     //redraw the view
    invalidate();
    requestLayout();
}

}

0 个答案:

没有答案