這篇文章給大家介紹RationBar控件怎么在Android中使用,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
創(chuàng)新互聯(lián)公司是專業(yè)的宏偉網(wǎng)站建設(shè)公司,宏偉接單;提供網(wǎng)站制作、成都網(wǎng)站建設(shè),網(wǎng)頁設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行宏偉網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來合作!
public class SuperRationBar extends View implements View.OnTouchListener { final public static int MIXED = 0; final public static int SCROLL = 1; //不傳默認(rèn)為 MIXED private int mode = MIXED; // 需要建立多少星星 不傳 默認(rèn)為5 private int number = 5; // 單個(gè)星星的寬度 這里寬度和高度相等 必傳 private int startWidth = 50; // 每個(gè)星星之間的間距 默認(rèn)20 (mode == MIXED 用不到) private int startPadding = 10; //是否已經(jīng)初始化試圖 private boolean isInit = false; //被選中的個(gè)數(shù) private int selectNumber = 0; //選中的樣式 private Bitmap bmSel; //未選中的樣式 private Bitmap bmNol; //記錄每個(gè)星星的位置 用 , 分割 private ListpointList; // 畫筆 private Paint mPaint; public SuperRationBar(Context context, AttributeSet attrs) { super(context, attrs); init(context, attrs); init(context); } private void init(Context context) { mPaint = new Paint(); setOnTouchListener(this); } private void init(Context context, AttributeSet attrs) { TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SuperRationBar); mode = a.getInteger(R.styleable.SuperRationBar_mode, MIXED); number = a.getInteger(R.styleable.SuperRationBar_SuperRationBar_number, 5); startWidth = (int) a.getDimension(R.styleable.SuperRationBar_SuperRationBar_startWidth, 50); startPadding = (int) a.getDimension(R.styleable.SuperRationBar_SuperRationBar_startPadding, 10); a.recycle(); } @Override public void draw(Canvas canvas) { super.draw(canvas); if (!isInit) { return; } {//記錄每個(gè)星星的位置 用 , 分割 pointList = new ArrayList<>(); } if (mode == MIXED) { //單個(gè)星星的寬度 int itemWidth = getWidth() / number; //根據(jù)每個(gè)星星之間的間距畫星星 for (int i = 0; i < number; i++) { int left = i == 0 ? 0 : itemWidth * i; int height = getHeight(); int bmHeight = bmSel.getHeight(); int top = (getHeight() - startWidth) / 2; pointList.add(left + "," + top + "," + (left + itemWidth) + "," + (top + itemWidth)); if (i < selectNumber) { canvas.drawBitmap(bmSel, left, top, mPaint); } else { canvas.drawBitmap(bmNol, left, top, mPaint); } } } else if (mode == SCROLL) { int totalWidth = (startWidth + startPadding) * (number - 1) + startWidth; //單個(gè)星星的寬度 int itemWidth = totalWidth / number; //根據(jù)每個(gè)星星之間的間距畫星星 for (int i = 0; i < number; i++) { int left = i == 0 ? 0 : itemWidth * i; int top = (getHeight() - startWidth) / 2; pointList.add(left + "," + top + "," + (left + itemWidth) + "," + (top + itemWidth)); if (i < selectNumber) { canvas.drawBitmap(bmSel, left, top, mPaint); } else { canvas.drawBitmap(bmNol, left, top, mPaint); } } } } @Override protected void onFinishInflate() { super.onFinishInflate(); isInit = true; } /** * 設(shè)置三種圖片樣式的id * * @param selId * @param nolId */ public SuperRationBar setImageResIds(int selId, int nolId) { bmSel = BitmapFactory.decodeResource(getResources(), selId); bmNol = BitmapFactory.decodeResource(getResources(), nolId); bmSel = zoomBitmap(bmSel, startWidth); bmNol = zoomBitmap(bmNol, startWidth); return this; } /** * 調(diào)用這個(gè)方法刷新頁面 */ public void launcher() { if (isInit) { postInvalidate(); } else { post(new Runnable() { @Override public void run() { postInvalidate(); } }); } } @Override public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_MOVE) { if (pointList != null) { int num = contain((int) event.getX(), (int) event.getY()); if (num != -1) { selectNumber = num + 1; } postInvalidate(); } if (event.getAction() == MotionEvent.ACTION_DOWN) { return true; } } return false; } /** * 判斷點(diǎn)擊的位置是不是在星星上邊 并返回星星的下標(biāo) 錯(cuò)誤 返回-1 * * @param x * @param y * @return */ private int contain(int x, int y) { int size = pointList.size(); for (int i = 0; i < size; i++) { String[] pointArray = pointList.get(i).split(","); int rl = Integer.parseInt(pointArray[0]); int rt = Integer.parseInt(pointArray[1]); int rr = Integer.parseInt(pointArray[2]); int rb = Integer.parseInt(pointArray[3]); if (x > rl && x < rr) { //在范圍內(nèi) 返回下標(biāo) return i; } } return -1; } public int getSelectNumber() { return selectNumber; } /** * 等比例縮放bitmap圖片 * * @param bitmap * @param reqWidth * @return */ public Bitmap zoomBitmap(Bitmap bitmap, float reqWidth) { if (bitmap == null) { return null; } final int width = bitmap.getWidth(); Matrix matrix = new Matrix(); float scale = reqWidth / width; matrix.setScale(scale, scale); bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); return bitmap; } }
注釋得還是挺詳細(xì)的 這里直接上使用代碼
SuperRationBar_startWidth 這個(gè)為必傳 而且只能在布局里面?zhèn)? RationBar0.setImageResIds(R.mipmap.img_ration_bar_sel, R.mipmap.img_ration_bar_nol) .launcher();
使用就這么一句 調(diào)用
int number0 = RationBar0.getSelectNumber();
關(guān)于RationBar控件怎么在Android中使用就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。