In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces the Android scoring RationBar control how to use, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.
The details are as follows
Wrapping it in ViewGroup feels like I can write a lot of View, so I decided to use it to define the control and go directly to the code
/ * comment star *
* both width and height cannot be used. Wrap_content must use a fixed value or match_parent *
* MIXED: divide the stars within the width of the control *
* SCROLL: draw stars according to the width of the stars and the distance between each star * / public class SuperRationBar extends View implements View.OnTouchListener {final public static int MIXED = 0; final public static int SCROLL = 1; / / the default is MIXED private int mode = MIXED; / / how many stars need to be established and the default is 5 private int number = 5; / / the width of a single star is equal to the width and height must be passed private int startWidth = 50 / / the spacing between each star defaults to 20 (mode = = MIXED is not needed) private int startPadding = 10; / / whether the attempt private boolean isInit = false; / / the number of selected private int selectNumber = 0 has been initialized; / / selected style private Bitmap bmSel; / / unselected style private Bitmap bmNol; / / record the position of each star, split private List pointList; / / brush 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 } {/ / record the position of each star, split pointList = new ArrayList ();} if (mode = = MIXED) {/ / the width of a single star int itemWidth = getWidth () / number; / / draw a star for (int I = 0; I) according to the distance between each star
< 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; //单个星星的宽度 int itemWidth = totalWidth / number; //根据每个星星之间的间距画星星 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; } /** * 设置三种图片样式的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; } /** * 调用这个方法刷新页面 */ 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; } /** * 判断点击的位置是不是在星星上边 并返回星星的下标 错误 返回-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) {/ / return subscript return I;}} return-1;} public int getSelectNumber () {return selectNumber;} / * * scale bitmap image * * @ 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;}}
The comments are still very detailed. Here we use the code directly.
SuperRationBar_startWidth is required and only RationBar0.setImageResIds (R.mipmap.img_ration_bar_sel, R.mipmap.img_ration_bar_nol) .launcher () can be passed in the layout.
Use just one sentence to call
Int number0 = RationBar0.getSelectNumber ()
You can get the current score.
Thank you for reading this article carefully. I hope the article "how to use the scoring RationBar Control in Android" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.