Android自定義View實現(xiàn)五星好評效果
本文實例為大家分享了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開發(fā)實現(xiàn)Gallery畫廊效果的方法
這篇文章主要介紹了Android開發(fā)實現(xiàn)Gallery畫廊效果的方法,結(jié)合具體實例形式分析了Android使用Gallery實現(xiàn)畫廊功能的具體操作技巧與相關(guān)注意事項,需要的朋友可以參考下2017-06-06Android 實現(xiàn)帶進度條的WebView的實例
這篇文章主要介紹了Android 實現(xiàn)帶進度條的WebView的實例的相關(guān)資料,這里介紹了Webview加載網(wǎng)頁的方法及帶進度的Drawable文件view_progress_webview的實現(xiàn),需要的朋友可以參考下2017-07-07Android 中使用ContentObserver模式獲取短信用正則自動填充驗證碼
這篇文章主要介紹了Android 中使用ContentObserver模式獲取短信用正則自動填充驗證碼,首先使用了ContentObserver監(jiān)聽短信,然后從短信中用正則的分組去拿到驗證碼,具體實現(xiàn)代碼大家參考下本文2017-02-02Android App開發(fā)中HTTP擴展包OkHttp的入門使用指南
OkHttp包為安卓開發(fā)中基于HTTP協(xié)議的網(wǎng)絡(luò)編程提供了很大便利,這里我們就來看一下Android App開發(fā)中HTTP擴展包OkHttp的入門使用指南:2016-07-0720.5 語音合成(百度2016年2月29日發(fā)布的tts引擎)
編寫手機App時,有時需要使用文字轉(zhuǎn)語音(Text to Speech)的功能,比如開車時閱讀收到的短信、導(dǎo)航語音提示、界面中比較重要的信息通過語音強調(diào)2016-03-03