Android 사용자 정의 컨트롤 구현 떡 모양 그림

11564 단어 Android떡 모양 도
본 고 는 그림 에서 보 듯 이 두 부분,왼쪽 떡 모양 그림 과 중간 에 있 는 두 개의 작은 사각형 과 오른쪽 에 있 는 두 줄 의 문 자 를 포함한다.

실현 은 비교적 간단 하 다.단지 일부 그래 픽 API 호출 일 뿐이다.
핵심 코드 는 onDraw 함수 에서 정적 컨트롤 을 그리 면 됩 니 다.

@Override
protected void onDraw(Canvas canvas) {
 super.onDraw(canvas);
 /**
 *     x  
 */
 float centreX= getWidth()/5;
 /**
 *     y  
 */
 float centreY= getHeight()/2;
 /**
 *      
 */
 float textSize=getHeight()/7;
 float width=(float)getWidth();
 float height=(float)getHeight();
 /**
 *            
 */
 float halfSmallRec =((float)getHeight())*3/70;
 percent =((float) mBigBallNumber)/(mBigBallNumber + mSmallBallNumber);
 /**
 *        
 */
 radius= Math.min(getWidth() * 1 / 8, getHeight() * 10 / 35);
 /**
 *        ,             
 */
 rectf=new RectF((int)(centreX-radius),(int)(centreY-radius),(int)(centreX+radius),(int)(centreY+radius));
 /**
 *           ,         
 */
 piePaint.setColor(mBigBallColor);
 /* The arc is drawn clockwise. An angle of 0 degrees correspond to the
 * geometric angle of 0 degrees (3 o'clock on a watch.)*/
 /* drawArc(RectF oval, float startAngle, float sweepAngle, boolean useCenter,Paint paint)*/
 /**
 *         ,float startAngle     0     3    
 *           12      ,       270 
 */
 canvas.drawArc(rectf, 270, 360 * percent, true, piePaint);
 /**
 *     ,           
 */
 piePaint.setColor(mSmallBallColor);
 /**
 *       12    360         ,12          270 
 */
 canvas.drawArc(rectf, 270 + 360 * percent, 360 - 360 * percent, true, piePaint);
           */
 piePaint.setColor(mBigBallColor);
 /**
 *         ,        
 */
 canvas.drawRect(width * 2 / 5 - halfSmallRec, height* 23/ 60 - halfSmallRec, width * 2 / 5 + halfSmallRec, height *23/ 60 + halfSmallRec, piePaint);
 /**
 *            
 */
 piePaint.setColor(mSmallBallColor);
 /**
 *                
 */
 canvas.drawRect(width * 2 / 5 - halfSmallRec, height * 37 / 60 - halfSmallRec, width * 2 / 5 + halfSmallRec, height * 37 / 60 + halfSmallRec, piePaint);
 /**
 *       ,       
 */
 textPaint.setColor(getResources().getColor(typedValue.resourceId));
 /**
 *       
 */
 textPaint.setTextSize(textSize);
 /**
 *     
 */
 String strBig = strBigBallName + mBigBallNumber;
 /**
 *       
 */
 float textBigWidth =textPaint.measureText(strBig);
 Paint.FontMetrics fontMetrics=textPaint.getFontMetrics();
 /**
 *         
 */
 canvas.drawText(strBig, width * 9 / 20 + textBigWidth / 2, height *23/ 60 - fontMetrics.top / 3, textPaint);
 /**
 *     
 */
 String strSmall = strSmallBallName + mSmallBallNumber;
 /**
 *       
 */
 float textUnderWidth=textPaint.measureText(strSmall);
 /**
 *          
 */
 canvas.drawText(strSmall,width*9/20+textUnderWidth/2,height*37/60-fontMetrics.top/3,textPaint);
 /**
 *       ,       
 */
 textPaint.setColor(getResources().getColor(R.color.half_transparent));
 String strBigPercent =" ("+ mPercentBigBall +")";
 /**
 *            
 */
 float bigPercent =textPaint.measureText(strBigPercent);
 /**drawText(String text, float x, float y, Paint paint)
 *      API,           ,    x  ,    y  ,  
 *          ,      xy         
 */
 canvas.drawText(strBigPercent, width * 9 / 20+ textBigWidth + bigPercent /2, height*23 / 60-fontMetrics.top*1/3, textPaint);
 /**
 *              
 */
 String strSmallPercent =" ("+ mPercentSmallBall +")";
 float smallPercent =textPaint.measureText(strSmallPercent);
 canvas.drawText(strSmallPercent,width*9/20+textUnderWidth+ smallPercent /2,height*37/60-fontMetrics.top/3,textPaint);
}

Canvas 가 텍스트 를 그 릴 때 FontMetrics 대상 을 사용 하여 위치의 좌 표를 계산 합 니 다.참고:FontMetrics 대상 으로 위치 좌 표를 계산 합 니 다.

텍스트 그리 기 설정

textPaint.setTextAlign(Paint.Align.CENTER);
x 의 좌 표 는 계산 하기 쉽 고 y 좌 표 는 필요 에 따라 FontMetrics 의 몇 가지 속성 을 사용 하면 된다.
전체 코드 는 다음 과 같 습 니 다:

public class PieHalfView extends View {
 /**
 *         
 */
 private Paint piePaint;
 /**
 *        
 */
 private Paint textPaint;
 /**
 *       
 */
 private float radius;
 private RectF rectf;
 /**
 *                 
 */
 private float percent;
 /**
 *       
 */
 private int mBigBallColor, mSmallBallColor;
 /**
 *       
 */
 private int mBigBallNumber;
 private int mSmallBallNumber;
 /**
 *          
 */
 private String mPercentBigBall;
 private String mPercentSmallBall;
 /**
 *       
 */
 private TypedValue typedValue;
 /**
 *        
 */
 private String strBigBallName;
 private String strSmallBallName;
 
 public PieHalfView(Context context) {
 super(context);
 init(context);
 }
 
 public PieHalfView(Context context, AttributeSet attrs) {
 super(context, attrs);
 init(context);
 }
 
 public PieHalfView(Context context, AttributeSet attrs, int defStyleAttr) {
 super(context, attrs, defStyleAttr);
 init(context);
 }
 private void init(Context context) {
 /**
 *        
 */
 piePaint =new Paint();
 piePaint.setAntiAlias(true);
 piePaint.setStyle(Paint.Style.FILL);
 /**
 *       
 */
 textPaint=new Paint();
 textPaint.setStyle(Paint.Style.STROKE);
 textPaint.setAntiAlias(true);
 textPaint.setTextAlign(Paint.Align.CENTER);
 /**
 *           ,             ,      
 */
 mBigBallColor = 0xFF9CCA5D;
 mSmallBallColor =0xFF5F7048;
 /*TypedValue:Container for a dynamically typed data value. Primarily used with Resources for holding resource values.*/
 typedValue=new TypedValue();
 context.getTheme().resolveAttribute(R.attr.maintextclor,typedValue,true);
 mBigBallNumber =1;
 mSmallBallNumber =3;
 mPercentBigBall ="40%";
 mPercentSmallBall ="60%";
 strBigBallName =getResources().getString(R.string.big);
 strSmallBallName =getResources().getString(R.string.small);
 }
 
 @Override
 protected void onDraw(Canvas canvas) {
 super.onDraw(canvas);
 /**
 *     x  
 */
 float centreX= getWidth()/5;
 /**
 *     y  
 */
 float centreY= getHeight()/2;
 /**
 *      
 */
 float textSize=getHeight()/7;
 float width=(float)getWidth();
 float height=(float)getHeight();
 /**
 *            
 */
 float halfSmallRec =((float)getHeight())*3/70;
 percent =((float) mBigBallNumber)/(mBigBallNumber + mSmallBallNumber);
 /**
 *        
 */
 radius= Math.min(getWidth() * 1 / 8, getHeight() * 10 / 35);
 /**
 *        ,             
 */
 rectf=new RectF((int)(centreX-radius),(int)(centreY-radius),(int)(centreX+radius),(int)(centreY+radius));
 /**
 *           ,         
 */
 piePaint.setColor(mBigBallColor);
 /* The arc is drawn clockwise. An angle of 0 degrees correspond to the
 * geometric angle of 0 degrees (3 o'clock on a watch.)*/
 /* drawArc(RectF oval, float startAngle, float sweepAngle, boolean useCenter,Paint paint)*/
 /*         ,float startAngle     0     3    
 *           12      ,       270 */
 canvas.drawArc(rectf, 270, 360 * percent, true, piePaint);
 /**
 *     ,           
 */
 piePaint.setColor(mSmallBallColor);
 /**
 *       12    360         ,12          270 
 */
 canvas.drawArc(rectf, 270 + 360 * percent, 360 - 360 * percent, true, piePaint);
 /**
 *           */
  piePaint.setColor(mBigBallColor);
 /**
 *         ,        
 */
 canvas.drawRect(width * 2 / 5 - halfSmallRec, height* 23/ 60 - halfSmallRec, width * 2 / 5 + halfSmallRec, height *23/ 60 + halfSmallRec, piePaint);
 /**
 *            
 */
 piePaint.setColor(mSmallBallColor);
 /**
 *                
 */
 canvas.drawRect(width * 2 / 5 - halfSmallRec, height * 37 / 60 - halfSmallRec, width * 2 / 5 + halfSmallRec, height * 37 / 60 + halfSmallRec, piePaint);
 /**
 *       ,       
 */
 textPaint.setColor(getResources().getColor(typedValue.resourceId));
 /**
 *       
 */
 textPaint.setTextSize(textSize);
 /**
 *     
 */
 String strBig = strBigBallName + mBigBallNumber;
 /**
 *       
 */
 float textBigWidth =textPaint.measureText(strBig);
 Paint.FontMetrics fontMetrics=textPaint.getFontMetrics();
 /**
 *         
 */
 canvas.drawText(strBig, width * 9 / 20 + textBigWidth / 2, height *23/ 60 - fontMetrics.top / 3, textPaint);
 /**
 *     
 */
 String strSmall = strSmallBallName + mSmallBallNumber;
 /**
 *       
 */
 float textUnderWidth=textPaint.measureText(strSmall);
 /**
 *          
 */
 canvas.drawText(strSmall,width*9/20+textUnderWidth/2,height*37/60-fontMetrics.top/3,textPaint);
 /**
 *       ,       
 */
 textPaint.setColor(getResources().getColor(R.color.half_transparent));
 String strBigPercent =" ("+ mPercentBigBall +")";
 /**
 *            */
 float bigPercent =textPaint.measureText(strBigPercent);
 /** drawText(String text, float x, float y, Paint paint)
 *      API,           ,    x  ,    y  ,  
 *          ,      xy         
 */
 canvas.drawText(strBigPercent, width * 9 / 20+ textBigWidth + bigPercent /2, height*23 / 60-fontMetrics.top*1/3, textPaint);
 /*
 *              
 */
 String strSmallPercent =" ("+ mPercentSmallBall +")";
 float smallPercent =textPaint.measureText(strSmallPercent);
 canvas.drawText(strSmallPercent,width*9/20+textUnderWidth+ smallPercent /2,height*37/60-fontMetrics.top/3,textPaint);
 }
 public void setPercent(float percent1){
 this.percent =percent1;
 invalidate();
 }
 public void setColor(int mBigBallColor,int mSmallBallColor){
 this.mBigBallColor =mBigBallColor;
 this.mSmallBallColor =mSmallBallColor;
 invalidate();
 }
 
 public void setOverRunner(String bigPecent, String smallPercent, int big, int small,
 int bigColor, int smallColor){
 this.mPercentBigBall = bigPecent;
 this.mPercentSmallBall = smallPercent;
 this.mBigBallNumber = big;
 this.mSmallBallNumber = small;
 this.mBigBallColor = bigColor;
 this.mSmallBallColor = smallColor;
 invalidate();
 }
}
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기