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


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