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的難點就是 如何根據需求對於自定義內容的值的動態修改

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章