Android 사용자 정의 보기 대중 평가 별 평가 컨트롤

본 논문 의 사례 는 안 드 로 이 드 가 대중 을 모방 하여 별 평가 컨트롤 의 구체 적 인 코드 를 공유 하여 여러분 께 참고 하 시기 바 랍 니 다.구체 적 인 내용 은 다음 과 같 습 니 다.

말 이 많 지 않 아 코드 를 직접 올 리 고 여 기 는 자정 View 를 사용 합 니 다.

public class RatingBar extends View {
 //   、        
 private Bitmap mStarNormal, mStarHalf, mStarSelected;
 //     
 private int mStartTotalNumber = 5;
 //       
 private float mSelectedNumber;
 //        
 private int mStartDistance;
 //     
 private Status mStatus = Status.FULL;
 //      
 private float mStarWidth;
 private float mStarHeight;
 //          
 private OnStarChangeListener mOnStarChangeListener;
 //       ,       
 private boolean isFull;
 //   
 private Paint mPaint = new Paint();
 //          ,    
 private enum Status {
  FULL, HALF
 }
 public void setOnStarChangeListener(OnStarChangeListener OnStarChangeListener) {
  this.mOnStarChangeListener = OnStarChangeListener;
 }
 public RatingBar(Context context) {
  this(context, null);
 }

 public RatingBar(Context context, @Nullable AttributeSet attrs) {
  this(context, attrs, 0);
 }

 public RatingBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
  super(context, attrs, defStyleAttr);
  TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.RatingBar);

  //         
  int starNormalId = array.getResourceId(R.styleable.RatingBar_starEmptyRes, 0);
  if (starNormalId == 0) {
   throw new IllegalArgumentException("      starNormal");
  }
  mStarNormal = BitmapFactory.decodeResource(getResources(), starNormalId);
  //          
  int starHalfId = array.getResourceId(R.styleable.RatingBar_starHalfRes, 0);
  if (starHalfId != 0) {
   mStarHalf = BitmapFactory.decodeResource(getResources(), starHalfId);
  }
  //          
  int starSelectedId = array.getResourceId(R.styleable.RatingBar_starSelectedRes, 0);
  if (starSelectedId == 0) {
   throw new IllegalArgumentException("      starSelected");
  }
  mStarSelected = BitmapFactory.decodeResource(getResources(), starSelectedId);
  //             ,       
  if (starHalfId == 0) {
   mStarHalf = mStarSelected;
  }

  mStartTotalNumber = array.getInt(R.styleable.RatingBar_startTotalNumber, mStartTotalNumber);
  mSelectedNumber = array.getFloat(R.styleable.RatingBar_selectedNumber, mSelectedNumber);
  mStartDistance = (int) array.getDimension(R.styleable.RatingBar_starDistance, 0);
  mStarWidth = array.getDimension(R.styleable.RatingBar_starWidth, 0);
  mStarHeight = array.getDimension(R.styleable.RatingBar_starHeight, 0);
  isFull = array.getBoolean(R.styleable.RatingBar_starIsFull, true);
  array.recycle();

  //       ,              (      )
  int starWidth = (int) Math.max(mStarWidth, mStarHeight);
  if (starWidth > 0) {
   mStarNormal = resetBitmap(mStarNormal, starWidth);
   mStarSelected = resetBitmap(mStarSelected, starWidth);
   mStarHalf = resetBitmap(mStarHalf, starWidth);
  }
  //         (        0.5       )
  if (!isFull) {
   int num = (int) mSelectedNumber;
   if (mSelectedNumber <= (num + 0.5f)) {
    mStatus = Status.HALF;
   }
  }
 }

 /**
  *          
  *
  * @param bitmap
  */
 public void allImage(Bitmap bitmap) {
  mStarSelected = bitmap;
  int starWidth = (int) Math.max(mStarWidth, mStarHeight);
  if (starWidth > 0) {
   mStarNormal = resetBitmap(mStarNormal, starWidth);
   mStarSelected = resetBitmap(mStarSelected, starWidth);
   mStarHalf = resetBitmap(mStarHalf, starWidth);
  }
  invalidate();
 }

 /**
  *          
  *
  * @param bitmap
  */
 public void mStarHalf(Bitmap bitmap) {
  mStarHalf = bitmap;
  int starWidth = (int) Math.max(mStarWidth, mStarHeight);
  if (starWidth > 0) {
   mStarNormal = resetBitmap(mStarNormal, starWidth);
   mStarSelected = resetBitmap(mStarSelected, starWidth);
   mStarHalf = resetBitmap(mStarHalf, starWidth);
  }
  invalidate();
 }

 /**
  *             ,       
  */
 public Bitmap resetBitmap(Bitmap bitMap, int startWidth) {
  //       
  return Bitmap.createScaledBitmap(bitMap, startWidth, startWidth, true);
 }

 /**
  *          
  */
 public void setSelectedNumber(int selectedNumber) {
  if (selectedNumber >= 0 && selectedNumber <= mStartTotalNumber) {
   this.mSelectedNumber = selectedNumber;
   invalidate();
  }
 }

 /**
  *         
  */
 public void setStartTotalNumber(int startTotalNumber) {
  if (startTotalNumber > 0) {
   this.mStartTotalNumber = startTotalNumber;
   invalidate();
  }
 }

 @Override
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  //               
  int height = getPaddingTop() + getPaddingBottom() + mStarNormal.getHeight();
  //   =      *   +      *(  -1) +padding
  int width = getPaddingLeft() + getPaddingRight() + mStarNormal.getWidth() * mStartTotalNumber + mStartDistance * (mStartTotalNumber - 1);
  setMeasuredDimension(width, height);
 }

 @Override
 protected void onDraw(Canvas canvas) {
  //     
  for (int i = 0; i < mStartTotalNumber; i++) {
   float left = getPaddingLeft();
   //         ,         
   if (i > 0) {
    left = getPaddingLeft() + i * (mStarNormal.getWidth() + mStartDistance);
   }
   float top = getPaddingTop();
   //        
   if (i < mSelectedNumber) {
    //          
    if (i < mSelectedNumber - 1) {
     canvas.drawBitmap(mStarSelected, left, top, mPaint);
    } else {
     //              
     if (mStatus == Status.FULL) {
      canvas.drawBitmap(mStarSelected, left, top, mPaint);
     } else {
      canvas.drawBitmap(mStarHalf, left, top, mPaint);
     }
    }
   } else {
    //        
    canvas.drawBitmap(mStarNormal, left, top, mPaint);
   }
  }
 }

 @Override
 public boolean onTouchEvent(MotionEvent event) {
  switch (event.getAction()) {
   //    
   case MotionEvent.ACTION_MOVE:
    //        x  
    float x = event.getX();
    //         
    int startWidth = getWidth() / mStartTotalNumber;
    //            
    int position = (int) (x / startWidth + 1);
    if (position < 0) {
     position = 0;
    }
    if (position > mStartTotalNumber) {
     position = mStartTotalNumber;
    }
    //             
    float result = x - startWidth * (position - 1);
    Status status;
    //           
    if (result > startWidth * 0.5f) {
     //   
     status = Status.FULL;
    } else {
     //    
     status = Status.HALF;
    }
    if (isFull) {
     status = Status.FULL;
    }
    //    
    if (mSelectedNumber != position || status != mStatus) {
     mSelectedNumber = position;
     mStatus = status;
     invalidate();
     if (mOnStarChangeListener != null) {
      position = (int) (mSelectedNumber - 1);
      //      :     (1.0  ),   (0.5  )
      float selectedNumber = status == Status.FULL ? mSelectedNumber
        : (mSelectedNumber - 0.5f);
      mOnStarChangeListener.OnStarChanged(selectedNumber,
        position < 0 ? 0 : position);
     }
    }
    break;
  }
  return true;
 }

 //     (     ,  )
 public interface OnStarChangeListener {
  void OnStarChanged(float selectedNumber, int position);
 }
}
사용자 정의 속성

<declare-styleable name="RatingBar">
  <!--   -->
  <attr name="starEmptyRes" format="reference" />
  <!--    -->
  <attr name="starHalfRes" format="reference" />
  <!--    -->
  <attr name="starSelectedRes" format="reference" />
  <!--     -->
  <attr name="startTotalNumber" format="integer" />
  <!--        -->
  <attr name="selectedNumber" format="float" />
  <!--     -->
  <attr name="starDistance" format="dimension" />
  <!--     -->
  <attr name="starWidth" format="dimension" />
  <!--     -->
  <attr name="starHeight" format="dimension" />
  <!--         ,    -->
  <attr name="starIsFull" format="boolean" />
</declare-styleable>
xml 설정

<com.demo.mvp_demo.ui.shop.RatingBar
  android:id="@+id/rb"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_gravity="center"
  app:starHeight="30dp"
  app:starWidth="30dp"
  app:selectedNumber="0.0"
  app:starDistance="15dp"
  app:starEmptyRes="@drawable/no"
  app:starHalfRes="@drawable/twoban"
  app:starIsFull="false"
  app:starSelectedRes="@drawable/two" />
activity 에서 rb 는 RatingBar rb 를 말 합 니 다.

대략 이 모양 입 니 다.구체 적 인 수 요 는 UI 에 따라 그림 을 제공 해 야 합 니 다.
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기