RatingBar Android星星評(píng)分控件

簡(jiǎn)述:

一個(gè)的自定義的Android評(píng)分控件(極簡(jiǎn)潔,易用)!
可更改圖標(biāo),大小,間距,可選半圖模式,點(diǎn)擊模式,拖動(dòng)模式,另外有回調(diào)監(jiān)聽。

實(shí)現(xiàn):(直接ctrl +c,v)

在project 的 build.gradle 下:(studio現(xiàn)在會(huì)默認(rèn)實(shí)現(xiàn))

allprojects {
  repositories {
        jcenter()
    }
}

在module 的 build.gradle 下:

compile 'com.xl.ratingbar:ratingbar:0.1.1'

在activity_main XML中:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center_horizontal"
    tools:context="com.xl.ratingbar.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="80dp"
        android:textSize="15sp"
        android:textColor="#4D4D4D"
        android:text="@string/bar1_name"
        />

    <com.xl.ratingbar.MyRatingBar
        android:id="@+id/bar1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        app:clickable="true"
        app:starCount="5"
        app:starNum="1"
        app:halfstart="true"
        app:starEmpty="@mipmap/star_empty"
        app:starHalf="@mipmap/star_half"
        app:starFill="@mipmap/star_full"
        app:starImageHeight="30dp"
        app:starImageWidth="30dp"
        app:starImagePadding="15dp"
        >

    </com.xl.ratingbar.MyRatingBar>

    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:textSize="15sp"
        android:textColor="#4D4D4D"
        android:text="@string/score"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:textSize="15sp"
        android:textColor="#4D4D4D"
        android:text="@string/bar2_name"
        />

    <com.xl.ratingbar.MyRatingBar
        android:id="@+id/bar2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        app:touchable="true"
        app:clickable="false"
        app:starCount="5"
        app:starNum="2"
        app:halfstart="true"
        app:starEmpty="@mipmap/fat_star_empty"
        app:starHalf="@mipmap/fat_star_half"
        app:starFill="@mipmap/fat_star_full"
        app:starImageHeight="30dp"
        app:starImageWidth="30dp"
        app:starImagePadding="15dp"
        >
    </com.xl.ratingbar.MyRatingBar>

    <TextView
        android:id="@+id/text2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:textSize="15sp"
        android:textColor="#4D4D4D"
        android:text="@string/score"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:textSize="15sp"
        android:textColor="#4D4D4D"
        android:text="@string/bar3_name"
        />

    <com.xl.ratingbar.MyRatingBar
        android:id="@+id/bar3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        app:touchable="true"
        app:starCount="5"
        app:starNum="3"
        app:halfstart="false"
        app:starEmpty="@mipmap/amimia"
        app:starFill="@mipmap/smiling"
        app:starImageHeight="30dp"
        app:starImageWidth="30dp"
        app:starImagePadding="15dp"
        >
    </com.xl.ratingbar.MyRatingBar>

    <TextView
        android:id="@+id/text3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:textSize="15sp"
        android:textColor="#4D4D4D"
        android:text="@string/comment"
        />

</LinearLayout>

MainActivity :

package com.xl.ratingbar;

import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements MyRatingBar.OnRatingChangeListener {

    private TextView text1;
    private TextView text2;
    private TextView text3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        text1 = (TextView)findViewById(R.id.text1);
        text2 = (TextView)findViewById(R.id.text2);
        text3 = (TextView)findViewById(R.id.text3);

        MyRatingBar bar1 = (MyRatingBar)findViewById(R.id.bar1);
        MyRatingBar bar2 = (MyRatingBar)findViewById(R.id.bar2);
        MyRatingBar bar3 = (MyRatingBar)findViewById(R.id.bar3);

        bar1.setOnRatingChangeListener(this);
        bar2.setOnRatingChangeListener(this);
        bar3.setOnRatingChangeListener(this);
    }

    @Override
    public void onRatingChange(MyRatingBar bar, float RatingCount) {
        switch (bar.getId()) {
            case R.id.bar1:
                text1.setText(getResources().getString(R.string.score).concat(String.valueOf(RatingCount)));
                break;
            case R.id.bar2:
                text2.setText(getResources().getString(R.string.score).concat(String.valueOf(RatingCount)));
                break;
            case R.id.bar3:
                changeByStar(bar,RatingCount,text3);
                break;
        }
    }

    private void changeByStar(MyRatingBar bar,float star,TextView textView){
        Drawable amimia = ContextCompat.getDrawable(this, R.mipmap.amimia);
        Drawable crying = ContextCompat.getDrawable(this, R.mipmap.crying);
        Drawable smiling = ContextCompat.getDrawable(this, R.mipmap.smiling);

        if (star == 1.0f) {
            chage(bar, textView, amimia, crying, getResources().getString(R.string.Worst));
        }else if (star == 2.0f) {
            chage(bar, textView, amimia, crying, getResources().getString(R.string.Bad));
        }else if (star == 3.0f) {
            chage(bar, textView, amimia, smiling, getResources().getString(R.string.Good));
        }else if (star == 4.0f) {
            chage(bar, textView, amimia, smiling, getResources().getString(R.string.Better));
        }else if (star == 5.0f) {
            chage(bar, textView, amimia, smiling, getResources().getString(R.string.Best));
        }
    }

    private void chage(MyRatingBar bar, TextView textView, Drawable empty, Drawable full, String string) {
        textView.setText(string);
        bar.setStarEmptyDrawable(empty);
        bar.setStarFillDrawable(full);
    }
}

OK,看效果?。ㄓ浀?github star一下呦。)

初始化.jpg

選擇.jpg

屬性:

image.png

注意 :

  • touchable、clickable 同時(shí)為true 時(shí) 默認(rèn)為拖動(dòng)模式
  • starHalf 不是半個(gè)圖,而是一半選擇狀態(tài)一般為選擇狀態(tài),例:
fat_star_half.jpg
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容