【Androidカスタムビュー実戦】カスタム評価採点コントロールRatingBarは、星の大きさや間隔をカスタマイズできます


転載は出典を明記してください.http://blog.csdn.net/linglongxin24/article/details/52918701【DylanAndroidのcsdnブログ】
Android開発では、よく商家や商品の評価に用いられ、星を用いて採点されています.しかしAndroidシステムに付属している採点コントロールでは、RatingBarは特に使いにくく、間隔や大きさは変えられない.そこで、特に使いやすい採点コントロールをカスタマイズしました.プロジェクトでは直接使用できますが、特に簡単です.次に、直接図を示します.
効果図
実装の原理
実はカスタムViewがLinearLayoutを継承し、そこに5つのImageViewが動的に追加されました.
実装コード、詳細なコメント
  • attrsで宣言されたxmlで設定可能な変数
    <declare-styleable name="RatingBar">
        <!--   -->
        <attr name="starImageSize" format="dimension" />
        <!--    -->
        <attr name="starPadding" format="dimension" />
        <!--    -->
        <attr name="starCount" format="integer" />
        <!--          -->
        <attr name="starEmpty" format="reference" />
        <!--       -->
        <attr name="starFill" format="reference" />
        <!--       -->
        <attr name="starHalf" format="reference" />
        <!--     boolean -->
        <attr name="clickable" format="boolean" />
        <!--    float -->
        <attr name="starStep" format="float" />
        <!--        ,      -->
        <attr name="stepSize">
            <enum name="Half" value="0" />
            <enum name="Full" value="1" />
        </attr>
    </declare-styleable>
  • RatingBarソース
  • import android.content.Context;
    import android.content.res.TypedArray;
    import android.graphics.drawable.Drawable;
    import android.util.AttributeSet;
    import android.view.View;
    import android.widget.ImageView;
    import android.widget.LinearLayout;
    
    import com.kejiang.yuandl.R;
    
    import java.math.BigDecimal;
    
    /** * Created by dylan on 2015/6/11. *        RatingBar *              * Correction clickEvent from Xml */
    public class RatingBar extends LinearLayout {
        /** *       */
        private boolean mClickable;
        /** *      */
        private int starCount;
        /** *         */
        private OnRatingChangeListener onRatingChangeListener;
        /** *         */
        private float starImageSize;
        /** *         */
        private float starPadding;
        /** *        ,      */
        private float starStep;
        /** *           */
        private Drawable starEmptyDrawable;
        /** *            */
        private Drawable starFillDrawable;
        /** *        */
        private Drawable starHalfDrawable;
        /** *                    */
        private StepSize stepSize;
    
        /** *             * * @param starHalfDrawable */
        public void setStarHalfDrawable(Drawable starHalfDrawable) {
            this.starHalfDrawable = starHalfDrawable;
        }
    
        /** *             * * @param starFillDrawable */
        public void setStarFillDrawable(Drawable starFillDrawable) {
            this.starFillDrawable = starFillDrawable;
        }
    
        /** *                * * @param starEmptyDrawable */
        public void setStarEmptyDrawable(Drawable starEmptyDrawable) {
            this.starEmptyDrawable = starEmptyDrawable;
        }
    
        /** *              * * @param clickable */
        public void setClickable(boolean clickable) {
            this.mClickable = clickable;
        }
    
        /** *          * * @param onRatingChangeListener */
        public void setOnRatingChangeListener(OnRatingChangeListener onRatingChangeListener) {
            this.onRatingChangeListener = onRatingChangeListener;
        }
    
        /** *         * * @param starImageSize */
        public void setStarImageSize(float starImageSize) {
            this.starImageSize = starImageSize;
        }
    
        public void setStepSize(StepSize stepSize) {
            this.stepSize = stepSize;
        }
    
        /** *      *   xml         * * @param context * @param attrs */
        public RatingBar(Context context, AttributeSet attrs) {
            super(context, attrs);
            setOrientation(LinearLayout.HORIZONTAL);
            TypedArray mTypedArray = context.obtainStyledAttributes(attrs, R.styleable.RatingBar);
            starImageSize = mTypedArray.getDimension(R.styleable.RatingBar_starImageSize, 20);
            starPadding = mTypedArray.getDimension(R.styleable.RatingBar_starPadding, 10);
            starStep = mTypedArray.getFloat(R.styleable.RatingBar_starStep, 1.0f);
            stepSize = StepSize.fromStep(mTypedArray.getInt(R.styleable.RatingBar_stepSize, 1));
            starCount = mTypedArray.getInteger(R.styleable.RatingBar_starCount, 5);
            starEmptyDrawable = mTypedArray.getDrawable(R.styleable.RatingBar_starEmpty);
            starFillDrawable = mTypedArray.getDrawable(R.styleable.RatingBar_starFill);
            starHalfDrawable = mTypedArray.getDrawable(R.styleable.RatingBar_starHalf);
            mClickable = mTypedArray.getBoolean(R.styleable.RatingBar_clickable, true);
            mTypedArray.recycle();
            for (int i = 0; i < starCount; ++i) {
                final ImageView imageView = getStarImageView();
                imageView.setImageDrawable(starEmptyDrawable);
                imageView.setOnClickListener(
                        new OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                if (mClickable) {
                                    //        
                                    int fint = (int) starStep;
                                    BigDecimal b1 = new BigDecimal(Float.toString(starStep));
                                    BigDecimal b2 = new BigDecimal(Integer.toString(fint));
                                    //        
                                    float fPoint = b1.subtract(b2).floatValue();
                                    if (fPoint == 0) {
                                        fint -= 1;
                                    }
    
                                    if (indexOfChild(v) > fint) {
                                        setStar(indexOfChild(v) + 1);
                                    } else if (indexOfChild(v) == fint) {
                                        if (stepSize == StepSize.Full) {//              
                                            return;
                                        }
                                        //              ,         
                                        if (imageView.getDrawable().getCurrent().getConstantState().equals(starHalfDrawable.getConstantState())) {
                                            setStar(indexOfChild(v) + 1);
                                        } else {
                                            setStar(indexOfChild(v) + 0.5f);
                                        }
                                    } else {
                                        setStar(indexOfChild(v) + 1f);
                                    }
    
                                }
    
                            }
                        }
                );
                addView(imageView);
            }
            setStar(starStep);
        }
    
        /** *           * * @return */
        private ImageView getStarImageView() {
            ImageView imageView = new ImageView(getContext());
    
            LinearLayout.LayoutParams layout = new LinearLayout.LayoutParams(
                    Math.round(starImageSize), Math.round(starImageSize));//              
            layout.setMargins(0, 0, Math.round(starPadding), 0);//              
            imageView.setLayoutParams(layout);
            imageView.setAdjustViewBounds(true);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setImageDrawable(starEmptyDrawable);
            imageView.setMinimumWidth(10);
            imageView.setMaxHeight(10);
            return imageView;
    
        }
    
    
        /** *         * * @param rating */
    
        public void setStar(float rating) {
    
            if (onRatingChangeListener != null) {
                onRatingChangeListener.onRatingChange(rating);
            }
            this.starStep = rating;
            //        
            int fint = (int) rating;
            BigDecimal b1 = new BigDecimal(Float.toString(rating));
            BigDecimal b2 = new BigDecimal(Integer.toString(fint));
            //        
            float fPoint = b1.subtract(b2).floatValue();
    
            //       
            for (int i = 0; i < fint; ++i) {
                ((ImageView) getChildAt(i)).setImageDrawable(starFillDrawable);
            }
            //         
            for (int i = fint; i < starCount; i++) {
                ((ImageView) getChildAt(i)).setImageDrawable(starEmptyDrawable);
            }
            //          
            if (fPoint > 0) {
                ((ImageView) getChildAt(fint)).setImageDrawable(starHalfDrawable);
            }
        }
    
        /** *           */
        public interface OnRatingChangeListener {
            /** *          * * @param RatingCount */
            void onRatingChange(float ratingCount);
    
        }
    
        /** *                ,     *    View.GONE */
        public enum StepSize {
            Half(0), Full(1);
            int step;
    
            StepSize(int step) {
                this.step = step;
            }
    
            public static StepSize fromStep(int step) {
                for (StepSize f : values()) {
                    if (f.step == step) {
                        return f;
                    }
                }
                throw new IllegalArgumentException();
            }
        }
    }
  • xmlでの使用法
    
    <com.kejiang.yuandl.view.RatingBar  android:id="@+id/rb" android:layout_width="360dp" android:layout_height="50dp" app:starCount="5" app:starEmpty="@mipmap/star_grey" app:starFill="@mipmap/star_yellow" app:starHalf="@mipmap/star_half_yellow" app:starImageSize="40dp" app:starPadding="20dp" app:starStep="1.5" app:stepSize="Half"></com.kejiang.yuandl.view.RatingBar>
  • Activityでの設定
  • RatingBar ratingBar= (RatingBar) findViewById(R.id.rb);
        ratingBar.setClickable(true);//      
        ratingBar.setStar(2.5f);//         
        ratingBar.setStepSize(RatingBar.StepSize.Half);//                
        ratingBar.setOnRatingChangeListener(new RatingBar.OnRatingChangeListener() {
            @Override
            public void onRatingChange(float ratingCount) {//            
                Log.d("RatingBar","RatingBar-Count="+ratingCount);
            }
        });