안 드 로 이 드 사용자 정의 뷰 모방 타 오 바 오 택배 물류 정보 시간 축

12347 단어 Android시간 축
안 드 로 이 드 를 배 운 지 오래 되 었 습 니 다.블 로 그 를 쓸 시간 이 없 었 습 니 다.주말 에 시간 이 있 을 때 자신 이 만 든 것 을 적어 놓 았 습 니 다.한편 으로 는 자신의 문 서 를 쓰 는 능력 을 단련 시 키 고 다른 한편 으로 는 코드 를 공유 하 는 동시에 여러분 과 기술 을 교류 하고 함께 공부 하 며 함께 발전 하 기 를 바 랍 니 다.
쓸데없는 말 은 많 지 않 습 니 다.먼저 사용자 정의 view 가 실현 할 효 과 를 살 펴 보 겠 습 니 다.
효과 도

사용자 정의 속성

<resources>
 <declare-styleable name="TimeLineView">
  <attr name="timelineRadius" format="dimension" />
  <attr name="timelineHeadRadius" format="dimension" />
  <attr name="timelineHeadColor" format="color" />
  <attr name="timelineOtherColor" format="color" />
  <attr name="timelineRadiusDistance" format="dimension" />
  <attr name="timelineCount" format="integer" />
  <attr name="timelineMarginTop" format="dimension" />
  <attr name="timelineWidth" format="dimension" />
 </declare-styleable>
</resources>
onDraw 방법
관건 적 인 방법 은 대량의 코드 주석 을 썼 으 니 여러분 들 이 똑똑히 보 셨 으 면 좋 겠 습 니 다.

@Override
 protected void onDraw(Canvas canvas) {
  //            view   
  viewWidth = getMeasuredWidth() / 2;
  //          
  mPaint.setColor(timelineHeadColor);
  /**
   *           ,        
   */
  for (int j = 1; j <= timelineCount; j++) {

   /**
    *  j==1,         ,    ,                
    */
   if (j == 1) {
    //       
    canvas.drawCircle(viewWidth, timelineHeadRadius + marginTop, timelineRadius, mPaint);
    mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setStrokeWidth(5.0f);
    //           
    canvas.drawCircle(viewWidth, timelineHeadRadius + marginTop, timelineHeadRadius, mPaint);
    //      ,          
    mPaint.setColor(timelineOtherColor);
    //       
    mPaint.setStyle(Paint.Style.FILL);
    /**
     *           
     */
    canvas.drawRect(new Rect(viewWidth - timelineWidth / 2, (int) (2 * timelineHeadRadius + marginTop) + 5, viewWidth + timelineWidth / 2, (int) (2 * timelineHeadRadius + timelineRadiusDistance + marginTop + 5)), mPaint);
    continue;
   }
   /**
    *        ,    
    *    x     ,view   
    *    y               ,  :      y                         :timelineHeadRadius + marginTop
    *              y    ,                *   :(2 * timelineRadius + timelineRadiusDistance) * (j - 1) + timelineHeadRadius - timelineRadius + marginTop
    *
    */
   canvas.drawCircle(viewWidth, (2 * timelineRadius + timelineRadiusDistance) * (j - 1) + 2 * timelineHeadRadius - timelineRadius + marginTop, timelineRadius, mPaint);
   /**
    *  
    *      
    * left:                         -1/2        viewWidth - timelineWidth / 2
    * top: (int) (j * (2 * timelineRadius + timelineRadiusDistance) - timelineRadiusDistance + 2 * (timelineHeadRadius-timelineRadius)+ marginTop) 
    * right:                         +1/2        viewWidth + timelineWidth / 2
    * bottom: (int) (j * (2 * timelineRadius + timelineRadiusDistance) + 2 * (timelineHeadRadius-timelineRadius)+ marginTop)
    */

   canvas.drawRect(new Rect(viewWidth - timelineWidth / 2, (int) (j * (2 * timelineRadius + timelineRadiusDistance) - timelineRadiusDistance + 2 * (timelineHeadRadius - timelineRadius) + marginTop), viewWidth + timelineWidth / 2, (int) (j * (2 * timelineRadius + timelineRadiusDistance) + 2 * (timelineHeadRadius - timelineRadius) + marginTop)), mPaint);
  }
 }

마지막 으로 모든 코드 를 첨부 합 니 다.

package com.demo.zes.timelinedemo;

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

/**
 * TODO: document your custom view class.
 */
public class TimeLineView extends View {


 private Paint mPaint;
 /**
  *          
  */
 private float timelineHeadRadius;
 /**
  *          
  */
 private int timelineHeadColor;
 /**
  *          
  */
 private int timelineOtherColor;
 /**
  *        
  */
 private int timelineCount;
 /**
  *       
  */
 private int viewWidth;
 /**
  *            
  */
 private int marginTop;
 /**
  *          
  */
 private int timelineRadius;
 /**
  *           
  */
 private int timelineRadiusDistance;
 /**
  *       
  */
 private int timelineWidth;
 /**
  *       
  */
 private float timeLineViewHeight;

 public TimeLineView(Context context) {
  this(context, null);
 }

 public TimeLineView(Context context, AttributeSet attrs) {
  this(context, attrs, 0);
 }

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

 /**
  *    
  *
  * @param context
  * @param attrs
  * @param defStyle
  */
 private void init(Context context, AttributeSet attrs, int defStyle) {

  final TypedArray a = getContext().obtainStyledAttributes(
    attrs, R.styleable.TimeLineView, defStyle, 0);
  timelineRadiusDistance = (int) a.getDimension(R.styleable.TimeLineView_timelineRadiusDistance, convertDIP2PX(context, 20));
  timelineHeadRadius = a.getDimension(R.styleable.TimeLineView_timelineHeadRadius, convertDIP2PX(context, 10));
  timelineRadius = (int) a.getDimension(R.styleable.TimeLineView_timelineRadius, convertDIP2PX(context, 5));
  timelineHeadColor = a.getColor(R.styleable.TimeLineView_timelineHeadColor, Color.parseColor("#73be36"));
  timelineOtherColor = a.getColor(R.styleable.TimeLineView_timelineOtherColor, Color.parseColor("#e0e0e0"));
  timelineCount = a.getInteger(R.styleable.TimeLineView_timelineCount, 0);
  timelineWidth = (int) a.getDimension(R.styleable.TimeLineView_timelineWidth, convertDIP2PX(context, 1));
  marginTop = (int) a.getDimension(R.styleable.TimeLineView_timelineMarginTop, convertDIP2PX(context, 50));
  a.recycle();

  mPaint = new Paint();
  mPaint.setAntiAlias(true);

 }


 @Override
 protected void onDraw(Canvas canvas) {
  //            view   
  viewWidth = getMeasuredWidth() / 2;
  //          
  mPaint.setColor(timelineHeadColor);
  /**
   *           ,        
   */
  for (int j = 1; j <= timelineCount; j++) {

   /**
    *  j==1,         ,    ,                
    */
   if (j == 1) {
    //       
    canvas.drawCircle(viewWidth, timelineHeadRadius + marginTop, timelineRadius, mPaint);
    mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setStrokeWidth(5.0f);
    //           
    canvas.drawCircle(viewWidth, timelineHeadRadius + marginTop, timelineHeadRadius, mPaint);
    //      ,          
    mPaint.setColor(timelineOtherColor);
    //       
    mPaint.setStyle(Paint.Style.FILL);
    /**
     *           
     */
    canvas.drawRect(new Rect(viewWidth - timelineWidth / 2, (int) (2 * timelineHeadRadius + marginTop) + 5, viewWidth + timelineWidth / 2, (int) (2 * timelineHeadRadius + timelineRadiusDistance + marginTop + 5)), mPaint);
    continue;
   }
   /**
    *        ,    
    *    x     ,view   
    *    y               ,  :      y                         :timelineHeadRadius + marginTop
    *              y    ,                *   :(2 * timelineRadius + timelineRadiusDistance) * (j - 1) + timelineHeadRadius - timelineRadius + marginTop
    *
    */
   canvas.drawCircle(viewWidth, (2 * timelineRadius + timelineRadiusDistance) * (j - 1) + 2 * timelineHeadRadius - timelineRadius + marginTop, timelineRadius, mPaint);
   /**
    *  
    *      
    * left:                         -1/2        viewWidth - timelineWidth / 2
    * top: (int) (j * (2 * timelineRadius + timelineRadiusDistance) - timelineRadiusDistance + 2 * (timelineHeadRadius-timelineRadius)+ marginTop) 
    * right:                         +1/2        viewWidth + timelineWidth / 2
    * bottom: (int) (j * (2 * timelineRadius + timelineRadiusDistance) + 2 * (timelineHeadRadius-timelineRadius)+ marginTop)
    */

   canvas.drawRect(new Rect(viewWidth - timelineWidth / 2, (int) (j * (2 * timelineRadius + timelineRadiusDistance) - timelineRadiusDistance + 2 * (timelineHeadRadius - timelineRadius) + marginTop), viewWidth + timelineWidth / 2, (int) (j * (2 * timelineRadius + timelineRadiusDistance) + 2 * (timelineHeadRadius - timelineRadius) + marginTop)), mPaint);
  }
 }

 public float getTimelineHeadRadius() {
  return timelineHeadRadius;
 }

 public void setTimelineHeadRadius(float timelineHeadRadius) {

  this.timelineHeadRadius = timelineHeadRadius;
  invalidate();
 }

 public int getTimelineHeadColor() {
  return timelineHeadColor;
 }

 public void setTimelineHeadColor(int timelineHeadColor) {

  this.timelineHeadColor = timelineHeadColor;
  invalidate();
 }

 public int getTimelineOtherColor() {
  return timelineOtherColor;
 }

 public void setTimelineOtherColor(int timelineOtherColor) {
  this.timelineOtherColor = timelineOtherColor;
  invalidate();
 }

 public int getTimelineCount() {
  return timelineCount;
 }

 public void setTimelineCount(int timelineCount) {
  this.timelineCount = timelineCount;
  invalidate();
 }

 public int getMarginTop() {
  return marginTop;
 }

 public void setMarginTop(int marginTop) {

  this.marginTop = marginTop;
  invalidate();
 }

 public int getTimelineRadius() {
  return timelineRadius;
 }

 public void setTimelineRadius(int timelineRadius) {

  this.timelineRadius = timelineRadius;
  invalidate();
 }

 public int getTimelineRadiusDistance() {
  return timelineRadiusDistance;
 }

 public void setTimelineRadiusDistance(int timelineRadiusDistance) {

  this.timelineRadiusDistance = timelineRadiusDistance;
  invalidate();
 }

 public int getTimelineWidth() {
  return timelineWidth;
 }

 public void setTimelineWidth(int timelineWidth) {
  this.timelineWidth = timelineWidth;
 }

 public float getTimeLineViewHeight() {
  this.timeLineViewHeight = getMarginTop() + getTimelineCount() * (2 * getTimelineRadius() + getTimelineRadiusDistance());
  return timeLineViewHeight;
 }

 public void setTimeLineViewHeight(float timeLineViewHeight) {
  this.timeLineViewHeight = timeLineViewHeight;
  invalidate();

 }

 public int getViewWidth() {
  return viewWidth;
 }

 public void setViewWidth(int viewWidth) {
  this.viewWidth = viewWidth;
  invalidate();
 }

 /**
  *   dip px
  */
 public static int convertDIP2PX(Context context, int dip) {
  float scale = context.getResources().getDisplayMetrics().density;
  return (int) (dip * scale + 0.5f * (dip >= 0 ? 1 : -1));
 }

}

Github 링크 첨부TimeLineDemo
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기