android开发分享Android自定义View实现五星好评效果

本文实例为大家分享了android实现五星好评效果的具体代码,供大家参考,具体内容如下 这个效果想必大家都非常熟悉,那么android如何自定义实现这种效果呢? 首先自定义属性

android开发分享Android自定义View实现五星好评效果实例为大家分享了android实现五星好评效果的具体代码,供大家参考,具体内容如下

Android自定义View实现五星好评效果

这个效果想必大家都非常熟悉,那么android如何自定义实现这种效果呢?

首先自定义属性:

  <?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>

下面看看具体实现:

  /**   * 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);          //性能优化,减少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开发分享Android自定义View实现五星好评效果的全部内容,希望对大家的学习有所帮助,也希望大家多多支持<计算机技术网(www.ctvol.com)!!>。

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/addevelopment/899458.html

(0)
上一篇 2021年10月23日
下一篇 2021年10月23日

精彩推荐