android自定义View之从入门到放弃(二)实现评分控件 记录学习

本次实现的是滑动屏幕实现评分的功能
在这里插入图片描述在这里插入图片描述
因本人不会上传gif 担待一下下哈
思路:根据手指滑动的距离长短 决定点亮几个小星星
直接撸代码

//属性的应用
 <declare-styleable name="RatingBarView">
        <attr name="normal" format="reference"/>
        <attr name="select" format="reference"/>
        <attr name="star" format="integer"/>
   </declare-styleable>

主要实现的代码:

public class RatingBarView extends View {
    private Bitmap normal,select;
    private int num = 5;
    private int current = 0;
    float moves;

    public RatingBarView(Context context) {
        this(context,null);
    }

    public RatingBarView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public RatingBarView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.RatingBarView);
        int starNormal = ta.getResourceId(R.styleable.RatingBarView_normal,0);
        if(starNormal==0){
            throw new RuntimeException("请设置属性 starNormal ");
        }
        int startSelect = ta.getResourceId(R.styleable.RatingBarView_select,0);
        if(startSelect==0){
            throw new RuntimeException("请设置属性 startSelect ");
        }
        normal = BitmapFactory.decodeResource(getResources(),starNormal);
        select = BitmapFactory.decodeResource(getResources(),startSelect);
        //回收
        num = ta.getInt(R.styleable.RatingBarView_star,num);
        ta.recycle();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);//主要是重新给setDemission进行赋值了  测量控件的大小
        int width = (select.getWidth()+getPaddingLeft())*num;
        int height = select.getHeight();
        setMeasuredDimension(width,height);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        for(int i=0;i<num;i++){
            int x = i*(select.getWidth()+getPaddingLeft());
            if(current>i){
                canvas.drawBitmap(select,x,0,null);
            }else {
                canvas.drawBitmap(normal,x,0,null);
            }
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                moves = event.getX();
                Log.i("down","我是按下的"+moves);
                break;
            case MotionEvent.ACTION_MOVE:
                float x = event.getX();
                Log.i("down","我是移动的"+x);
                float moveX = x-moves;
                int currentgredle =  (int) (moveX/(select.getWidth()+getPaddingLeft())+1);
                if(currentgredle<0){
                    currentgredle=0;
                }
                if(currentgredle >current){
                    current = currentgredle;
                }

                current = currentgredle;
                 invalidate();

                break;
        }
        return true;
    }
}

在布局中的引用:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".StartActivity">
    <com.example.zidingyidemo.widget.RatingBarView

        app:select="@drawable/select"
        app:normal="@drawable/normal"
        app:star="5"
        android:layout_width="wrap_content"
        android:paddingLeft="20dp"
        android:layout_height="wrap_content" />
</LinearLayout>

over!!!
随着自定义View的慢慢学习,对于自定义View的学习的深入了解,自定义View的难点就是 如何根据需求对于自定义内容的值的动态修改

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

安东尼肉店

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值