热门标签 | HotTags
当前位置:  开发笔记 > Android > 正文

Android自定义星评空间示例代码

这篇文章主要介绍了Android自定义星评空间的实例代码,需要的朋友可以参考下

没事做用自定义view方式写一个星评控件,虽说网上很多这个控件,但是这是自己写的,在这里记录一下。

首先需要自定义属性


    
    
    
    
    
    
    
    
    
  

初始化代码

 protected void init(Context context, AttributeSet attrs) {
    TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.Rate);
    int activeId = 0;
    int disactiveId = 0;
    if (array != null) {
      size = array.getInt(R.styleable.Rate_custom_rate_size, 5);
      activeSize = array.getInt(R.styleable.Rate_custom_rate_active_size, 3);
      rateWidth = array.getDimensionPixelOffset(R.styleable.Rate_custom_rate_width, 0);
      rateHeight = array.getDimensionPixelOffset(R.styleable.Rate_custom_rate_height, 0);
      activeId = array.getResourceId(R.styleable.Rate_custom_rate_active_drawable, 0);
      disactiveId = array.getResourceId(R.styleable.Rate_custom_rate_disactive_drawable, 0);
      padding = array.getDimensionPixelOffset(R.styleable.Rate_custom_rate_padding, 0);
      isTouch = array.getBoolean(R.styleable.Rate_custom_rate_touch, false);
      array.recycle();
    }
    //如果没有宽高就设置一个默认值
    if (rateHeight <= 0){
      rateHeight = 80;
    }
    if (rateWidth <= 0){
      rateWidth = 80;
    }
    if (activeId!=0){
      activeBitmap = BitmapFactory.decodeResource(getResources(), activeId);
      //如果没有设置宽高时候
      if (rateWidth <= 0) {
        rateWidth = activeBitmap.getWidth();
      }
      //把图片压缩到设置的宽高
      activeBitmap = Bitmap.createScaledBitmap(activeBitmap, (int) rateWidth, (int) rateHeight, false);
    }
    if (disactiveId != 0){
      disactiveBitmap = BitmapFactory.decodeResource(getResources(), disactiveId);
      if (rateHeight <= 0) {
        rateHeight = activeBitmap.getHeight();
      }
      disactiveBitmap = Bitmap.createScaledBitmap(disactiveBitmap, (int) rateWidth, (int) rateHeight, false);
    }
    mPaint = new Paint();//初始化bitmap的画笔
    mPaint.setAntiAlias(true);
    activPaint = new Paint();//初始化选中星星的画笔
    activPaint.setAntiAlias(true);
    activPaint.setColor(Color.YELLOW);
    disactivPaint = new Paint();//初始化未选中星星的画笔
    disactivPaint.setAntiAlias(true);
    disactivPaint.setColor(Color.GRAY);
  }

onMeasure方法设置View的宽高,如果设置的每一个星星控件的宽高大于实际view的宽高,就用星星空间的宽高作于view的宽高

@Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int widthMode = MeasureSpec.getMode(widthMeasureSpec);
    int widthSize = MeasureSpec.getSize(widthMeasureSpec);
    int heightMode = MeasureSpec.getMode(heightMeasureSpec);
    int heightSize = MeasureSpec.getSize(heightMeasureSpec);
    //计算宽
    if (widthMode == MeasureSpec.EXACTLY) {
      //如果view的宽度小于设置size*星星的宽度,就用size * (int) (padding + rateWidth)做为控件的宽度度
      if (widthSize 

onDraw方法中绘制

//开始画active
    for (int i = 0; i 

上面用到两个方法drawActivRate和drawDisActivRate,分别是在没有设置活动中的星星和不在活动中星星的图片的时候,绘制在活动和不在活动的默认星星:

/**
   * 绘制黄色的五角星(在活动的)
   * */
  private void drawActivRate(int position,Canvas canvas){
    float radius = rateWidth/2;//根據每一個星星的位置繪製園,確定五角星五個點的位置
    float angle = 360/5;
    float centerX = (rateWidth+padding)*(position+1)-padding-radius;//獲取每一個星星空間的中心位置的X坐標
    float centerY =height/2;//獲取每一個星星空間的中心位置的y坐標
    Path mPath = new Path();
    mPath.moveTo(centerX,centerY-radius);
    mPath.lineTo(centerX+(float) Math.cos((angle*2-90)*Math.PI / 180)*radius,centerY+(float)Math.sin((angle*2-90)*Math.PI / 180)*radius);
    mPath.lineTo( centerX-(float)Math.sin(angle*Math.PI / 180)*radius,centerY-(float)Math.cos(angle*Math.PI / 180)*radius);
    mPath.lineTo( centerX+(float)Math.sin(angle*Math.PI / 180)*radius,centerY-(float)Math.cos(angle*Math.PI / 180)*radius);
    mPath.lineTo( centerX-(float)Math.sin((angle*3-180)*Math.PI / 180)*radius,centerY+(float)Math.cos((angle*3-180)*Math.PI / 180)*radius);
//    mPath.lineTo(centerX,centerY-radius);
    mPath.close();
    canvas.drawPath(mPath,activPaint);
  }
  /**
   * 绘制灰色的五角星
   * */
  private void drawDisActivRate(int position,Canvas canvas){
    float radius = rateWidth/2;
    float angle = 360/5;
    float centerX = (rateWidth+padding)*(position+1)-padding-radius;
    float centerY =height/2;
    Path mPath = new Path();
    mPath.moveTo(centerX,centerY-radius);
    mPath.lineTo(centerX+(float) Math.cos((angle*2-90)*Math.PI / 180)*radius,centerY+(float)Math.sin((angle*2-90)*Math.PI / 180)*radius);
    mPath.lineTo( centerX-(float)Math.sin(angle*Math.PI / 180)*radius,centerY-(float)Math.cos(angle*Math.PI / 180)*radius);
    mPath.lineTo( centerX+(float)Math.sin(angle*Math.PI / 180)*radius,centerY-(float)Math.cos(angle*Math.PI / 180)*radius);
    mPath.lineTo( centerX-(float)Math.sin((angle*3-180)*Math.PI / 180)*radius,centerY+(float)Math.cos((angle*3-180)*Math.PI / 180)*radius);
//    mPath.lineTo(centerX,centerY-radius);
    mPath.close();
    canvas.drawPath(mPath,disactivPaint);
  }

最后在onTouchEvent方法中处理选中和未选中星星的处理

@Override
  public boolean onTouchEvent(MotionEvent event) {
    if (!isTouch){//如果不支持觸摸
      return false;
    }
    switch (event.getAction()) {
      case MotionEvent.ACTION_DOWN:
        touchX = event.getX();
        touchY = event.getY();
        for (int i = 0; i 

以上就是自定义view写的星评控件,代码中的注解已经比较详细了,就不多说了,

源码地址

以上所述是小编给大家介绍的android自定义星评空间的实例代码,希望对大家有所帮助!


推荐阅读
  •  一、Activity的重新认识1.Intent的使用补充:FlAG_ACTIVITY_CLEAN_TOP:类似于singaltask的使用   ,icod ... [详细]
  • 1http:blog.csdn.netlfdfhlarticledetails8220729代码如下:imageView.startAnimation(welcomeAnimation) ... [详细]
  • Emgu 学习之HelloWorld
    安装和配置系统Win10,VS2013,下载Emgu安装包libemgucv-windesktop-3.4.3.3016安装到了E:\OpenCV\emgucv-windeskto ... [详细]
  • EL&&JSTL
    EL表达式概念ExpressionLanguage表达式语言。作用替换和简化jsp页面中java代码的编写语法${表达式}注意jsp默认支持el表达式的。如果要忽略el表达式可以使 ... [详细]
  • Django信号使得某个操作之前能定制化一些任务-内置信号-导入fromdjango.core.signalsimportXX00-注册函数-自定义-自定义-定义信号importd ... [详细]
  • HDU 3487 Play with Chain
    题意:对序列取出连续的一段接到剩下的第k个值后面,或者把一段序列反转。解题思路:splay区间操作。解题代码:1FileName:hdu3487.cpp2Author:darkdr ... [详细]
  • svnstat查看当前目录下svn状态svnremovexxxxsvnaddxx ... [详细]
  • 在Windows下配置安装OMNeT++ 4.0
    在Windows下配置安装OMN ... [详细]
  • ANR一般有三种类型: ... [详细]
  • 6.列表表格列表(1)liststyle基本语法语法取值使用说明设置列表项目相关样式。当liststyleimage和liststyletype都被指定了时,liststyleim ... [详细]
  • 室内外一体化建模
    http:www.fx361.compage20180913422182 ... [详细]
  • 简介if循环ifconditonthencommandselsecommandfielse这部分没有可以省略或者ifconditionthencommandselseifcondi ... [详细]
  • 标准ACL配置
    标准ACL的表号是1~99中的一个数字permit|d ... [详细]
  • 问题描述为某音像店开发一个迷你DVD管理器,最多可存6张DVD,实现碟片的管理。管理器具备的功能主要有:1、查看DVD信息。菜单选择查看功能,展示DVD的信息。2、新增DVD信息选 ... [详细]
  • 1.简述OC中内存管理机制。与retain配对使用的方法是dealloc还是release,为什么?需要与alloc配对使用的方法是dealloc还是release,为什么?rea ... [详细]
author-avatar
Lily賈麗
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有