自定义评分条RatingBar
2018-03-13 10:38
148 查看
package com.ioc.cn.custom; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.support.annotation.Nullable; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; import com.ioc.cn.R; /** * Created by Cwm on 2018/3/13. * 自定义评分条RatingBar */ public class RatingBar extends View { private Bitmap mStarNormal, mStarSelect; private int mStarCount, SelectCount; 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 typedArray = context.obtainStyledAttributes(attrs, R.styleable.RatingBar); int starNormal = typedArray.getResourceId(R.styleable.RatingBar_starNormal, 0); int starSelect = typedArray.getResourceId(R.styleable.RatingBar_starSelect, 0); mStarCount = typedArray.getInt(R.styleable.RatingBar_starCount, 0); typedArray.recycle(); if (starNormal == 0) { throw new UnsupportedOperationException("未设置 starNormal 属性"); } if (starSelect == 0) { throw new UnsupportedOperationException("未设置 starSelect 属性"); } if (mStarCount <= 0) { throw new UnsupportedOperationException("未设置 starCount 属性"); } mStarNormal = BitmapFactory.decodeResource(getResources(), starNormal); mStarSelect = BitmapFactory.decodeResource(getResources(), starSelect); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); //星星宽度+间隔 星星高度 int width = (mStarNormal.getWidth() * mStarCount) + (mStarNormal.getWidth() / 3 * (mStarCount - 1)); int height = mStarNormal.getHeight(); setMeasuredDimension(width, height); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); for (int i = 0; i < mStarCount; i++) { int left = 0;//设置星星之间间隔 if (i == 0) { left = i * mStarNormal.getWidth();//第一个间隔为0 } else {//其余的间隔为当前星星数量/3 left = i * mStarNormal.getWidth() + i * (mStarNormal.getWidth() / 3); } if (i < SelectCount) { canvas.drawBitmap(mStarSelect, left, 0, null); } else { canvas.drawBitmap(mStarNormal, left, 0, null); } } } @Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { // case MotionEvent.ACTION_DOWN://三个事件处理的逻辑都一样 case MotionEvent.ACTION_MOVE: float x = event.getX(); int number = (int) (x / ((mStarNormal.getWidth()) + (mStarNormal.getWidth() / 3)) + 1); if (number <= 0) {//判断范围 number = 0; } if (number > mStarCount) { number = mStarCount; } if (number == SelectCount) {//判断是否和上次的星星数量一致,优化 return true; } SelectCount = number; invalidate();//刷新事件 } return true; } }使用
<com.ioc.cn.custom.RatingBar
android:id="@+id/ratingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
app:starCount="5"
app:starNormal="@drawable/star_normal"
app:starSelect="@drawable/star_selected"
/>
相关文章推荐
- IOS-一步一步教你自定义评分星级条RatingBar
- IOS-一步一步教你自定义评分星级条RatingBar
- IOS-一步一步教你自定义评分星级条RatingBar ——转载的
- iOS开发篇(二)自定义评分星级条RatingBar
- lucene3.6.2之自定义评分
- Android自定义RatingBar(评分控件)
- Lucene4.10使用教程(七):Lucene的自定义评分
- 自定义RatingBar(评分控件)
- Android UI控件RatingBar实现自定义星星评分效果
- Lucene 3.6.2入门:高级搜索之自定义评分
- Android自定义RatingBar(评分控件)
- lucene 自定义评分 (给lucene自带的评分*我们filed的系数) 如搜索结果时间的加权
- Lucene4.10使用教程(七):Lucene的自定义评分
- Lucene4.10使用教程(八):Lucene的自定义评分(2)
- Android 自定义RatingBar评分控件
- Lucene3.5自定义评分以及根据域进行自定义评分设定
- Android自定义星星评分控件,高效
- android 自定义评分
- Android自定义评分控件:RatingStarView
- Lucene4.10使用教程(七):Lucene的自定义评分