Android自定義View實現(xiàn)五星好評效果
更新時間:2019年11月03日 10:15:23 作者:AD鈣奶-lalala
這篇文章主要為大家詳細(xì)介紹了Android自定義View實現(xiàn)五星好評效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了Android實現(xiàn)五星好評效果的具體代碼,供大家參考,具體內(nèi)容如下

這個效果想必大家都非常熟悉,那么Android如何自定義實現(xiàn)這種效果呢?
首先自定義屬性:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="RatingStar">
<attr name="starNormal" format="reference"/>
<attr name="starFocus" format="reference"/>
<attr name="starNumber" format="integer"/>
</declare-styleable>
</resources>
下面看看具體實現(xiàn):
/**
* Created by Michael on 2019/11/1.
*/
public class RatingStar extends View {
private int normalId;
private int focusId;
private Bitmap normalImg;
private Bitmap focusImg;
private int number;
private int w1;
private int h1;
private int marginLeft;
private int marginTop;
private int marginBottom;
private int marginRight;
private int height;
private int width;
private int p;
private float w0;
private int i0;
private int mGrade;
public RatingStar(Context context) {
this(context,null);
}
public RatingStar(Context context, @Nullable AttributeSet attrs) {
this(context, attrs,0);
}
public RatingStar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray array = context.obtainStyledAttributes(attrs,R.styleable.RatingStar);
normalId = array.getResourceId(R.styleable.RatingStar_starNormal,0);
focusId = array.getResourceId(R.styleable.RatingStar_starFocus,0);
normalImg = BitmapFactory.decodeResource(getResources(), normalId);
focusImg = BitmapFactory.decodeResource(getResources(), focusId);
number = array.getInteger(R.styleable.RatingStar_starNumber,5);
array.recycle();
i0 = -1;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
w1 = normalImg.getWidth();
h1 = normalImg.getHeight();
//中間間隔
p = 30;
marginTop = 20;
marginBottom = 20;
marginLeft = 20;
marginRight = 20;
height = h1 + marginTop + marginBottom;
width = w1 *number+(number-1)*p +marginLeft+marginRight;
setMeasuredDimension(width, height);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
for (int i = 0; i < number; i++) {
if (i <= i0){
canvas.drawBitmap(focusImg,i*w1+marginLeft+i*p,marginTop,null);
mGrade = i+1;
}else{
canvas.drawBitmap(normalImg,i*w1+marginLeft+i*p,marginTop,null);
}
}
Log.e("msg","我被調(diào)用了!");
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();//相對于控件自身的距離
//event.getRawX() 相對于屏幕的距離
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
//case MotionEvent.ACTION_UP:
w0 = getWidth()/5;
i0 = (int) (x/w0);
//性能優(yōu)化,減少onDraw()調(diào)用
if (mGrade == i0+1){
return true;
}
invalidate();
break;
}
return true;
}
}
最后看看具體布局中使用:
<com.example.star.RatingStar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:starNormal="@mipmap/star_normal"
app:starFocus="@mipmap/star_selected"
app:starNumber="5"
/>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android USB轉(zhuǎn)串口通信開發(fā)實例詳解
這篇文章主要介紹了 Android USB轉(zhuǎn)串口通信開發(fā)實例詳解的相關(guān)資料,需要的朋友可以參考下2017-04-04
Android編程實現(xiàn)禁止系統(tǒng)鎖屏與解鎖亮屏的方法
這篇文章主要介紹了Android編程實現(xiàn)禁止系統(tǒng)鎖屏與解鎖亮屏的方法,實例分析了Android關(guān)閉屏幕、鎖屏及解鎖屏幕的相關(guān)技巧,需要的朋友可以參考下2015-12-12
Android開發(fā)之Kotlin委托的原理與使用詳解
我們常用的委托模式怎么使用?在?Java?語言中需要我們手動的實現(xiàn),而在?Kotlin?語言中直接通過關(guān)鍵字?by?就可以實現(xiàn)委托,下面我們就一起看看不同種類的委托使用以及在?Android?常見的一些場景中的使用2023-03-03
Android利用Service開發(fā)簡單的音樂播放功能
這篇文章主要介紹了Android利用Service開發(fā)簡單的音樂播放功能,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-04-04

