Android自定義View實(shí)現(xiàn)五星好評效果
本文實(shí)例為大家分享了Android實(shí)現(xiàn)五星好評效果的具體代碼,供大家參考,具體內(nèi)容如下
這個(gè)效果想必大家都非常熟悉,那么Android如何自定義實(shí)現(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>
下面看看具體實(shí)現(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í)有所幫助,也希望大家多多支持我們。
上一篇:Android通過ViewModel保存數(shù)據(jù)實(shí)現(xiàn)多頁面的數(shù)據(jù)共享功能
欄 目:Android
下一篇:兩個(gè)surfaceView實(shí)現(xiàn)切換效果
本文標(biāo)題:Android自定義View實(shí)現(xiàn)五星好評效果
本文地址:http://www.jygsgssxh.com/a1/Android/9113.html
您可能感興趣的文章
- 01-10Android自定義View之繪制圓形頭像功能
- 01-10Android實(shí)現(xiàn)雙擊返回鍵退出應(yīng)用實(shí)現(xiàn)方法詳解
- 01-10android實(shí)現(xiàn)記住用戶名和密碼以及自動(dòng)登錄
- 01-10android實(shí)現(xiàn)簡單計(jì)算器功能
- 01-10Android 友盟第三方登錄與分享的實(shí)現(xiàn)代碼
- 01-10C++自定義API函數(shù)實(shí)現(xiàn)大數(shù)相乘算法
- 01-10android實(shí)現(xiàn)指紋識(shí)別功能
- 01-10Emoji表情在Android JNI中的兼容性問題詳解
- 01-10Android實(shí)現(xiàn)圓形漸變加載進(jìn)度條
- 01-10android開發(fā)環(huán)境中SDK文件夾下的所需內(nèi)容詳解


閱讀排行
本欄相關(guān)
- 01-10Android自定義View之繪制圓形頭像功能
- 01-10Android實(shí)現(xiàn)雙擊返回鍵退出應(yīng)用實(shí)現(xiàn)方
- 01-10android實(shí)現(xiàn)簡單計(jì)算器功能
- 01-10android實(shí)現(xiàn)記住用戶名和密碼以及自動(dòng)
- 01-10C++自定義API函數(shù)實(shí)現(xiàn)大數(shù)相乘算法
- 01-10Android 友盟第三方登錄與分享的實(shí)現(xiàn)代
- 01-10android實(shí)現(xiàn)指紋識(shí)別功能
- 01-10如何給Flutter界面切換實(shí)現(xiàn)點(diǎn)特效
- 01-10Android實(shí)現(xiàn)圓形漸變加載進(jìn)度條
- 01-10Emoji表情在Android JNI中的兼容性問題詳
隨機(jī)閱讀
- 01-10C#中split用法實(shí)例總結(jié)
- 01-10delphi制作wav文件的方法
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 08-05織夢dedecms什么時(shí)候用欄目交叉功能?
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 04-02jquery與jsp,用jquery
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05DEDE織夢data目錄下的sessions文件夾有什


