ShineButton动画点赞的封装

ShineButton是个非常好的点赞控件,它包含了4个不同的样式,基本满足我们开发的需求样式。而且在点击的时候也会有很漂亮的动画出现。唯一可惜的是它是单个单个的控件,而我们一般使用点赞都是4~5个等级,所有使用时很不方便,为了方便使用,我把它封装起来了,使用更加方便,等级可以随心所设,只需写一个控件即可。


1.ShineButton的封装(直接粘贴复制就好)

1)先在build.gradle,添加:

		compile 'com.sackcentury:shinebutton:0.1.6'
		这样就可以使用ShineButton控件了。
	2)封装(直接剪切)
		
	
public  class NewShineButton extends LinearLayout {
    private int number;
    private int raw;
    private int color;
    private int fillcolor;
    private Context context;
    private List<ShineButton>list=new ArrayList<>();
    private TextView textView;

    private int HEIGHT=100;
    private int WIDTH=100;
    private int LEFTMARGIN=12;

    public NewShineButton(Context context) {
        super(context);
        this.context=context;
    }

    public NewShineButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context=context;
    }

    public NewShineButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context=context;
    }


    //设置数量和颜色
    public void setNumber(int i,int color,int fillcolor){
        number=i;
        this.color=color;
        this.fillcolor=fillcolor;
    }
    public void setStyl(int raw){
        this.raw=raw;
    }

    public void setData(){
        textView=new TextView(context);
        for (int i=0;i<number;i++){
            ShineButton button=new ShineButton(context);

            button.setShapeResource(raw);


            button.setBtnColor(color);
            button.setBtnFillColor(fillcolor);

            button.setAllowRandomColor(true);

            LayoutParams p = new LayoutParams(//
                    WIDTH, HEIGHT);
            p.leftMargin=LEFTMARGIN;

            this.addView(button, p);

            list.add(button);
        }

//        for (ShineButton button:list){
//            ShineButton1(button);
//        }
        for (int i=0;i<list.size();i++){
            ShineButton1(list.get(i),i);
        }

    }

    public void  setSize(int width, int height,int leftMargin){
        WIDTH=width;
        HEIGHT=height;
        LEFTMARGIN=leftMargin;
    }

    private int number1;
    private int ShineButton1(final ShineButton shineButton, final int num){


        shineButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                for (int i=0;i<list.size();i++){
                    ShineButton b=list.get(i);
                    b.setChecked(false);
                }
                for (int i=0;i<=num;i++){
                    ShineButton button=list.get(i);
                    button.setChecked(true);
                }
                number1=num+1;

                //为了可以让使用拿到点击第几个
                textView.setText(number1+"");

                Toast.makeText(getContext(),number1+"",Toast.LENGTH_SHORT).show();
            }
        });

        return number1;
    }



    public TextView getText(){
        return textView;
    }


}
	创建一个类,直接粘贴进去就已经封装好了。

 	2.xml文件中的使用:
	
	
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_shine"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
>



<comr.example.administrator.dome.ShineButton.NewShineButton
    android:layout_width="match_parent"
    android:layout_marginTop="20dp"
    android:id="@+id/shine_button"
    android:layout_height="50dp">


</comr.example.administrator.dome.ShineButton.NewShineButton>


    <TextView
        android:text="点击了个"
        android:gravity="center"
        android:layout_marginTop="50dp"
        android:id="@+id/shine_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        />


    <comr.example.administrator.dome.ShineButton.NewShineButton
        android:layout_width="match_parent"
        android:layout_marginTop="20dp"
        android:id="@+id/shine_button1"
        android:layout_height="50dp">


    </comr.example.administrator.dome.ShineButton.NewShineButton>

    <comr.example.administrator.dome.ShineButton.NewShineButton
        android:layout_width="match_parent"
        android:id="@+id/shine_button2"
        android:layout_marginTop="20dp"
        android:layout_height="50dp">


    </comr.example.administrator.dome.ShineButton.NewShineButton>


</LinearLayout>
	(上面写了3个封装控件,还有一个TextView是为了显示点击个数(级别))


	3.java文件的内容:


		
public class ShineActivity extends AppCompatActivity {
    private NewShineButton newShineButton;
    private NewShineButton newShineButton1;
    private NewShineButton newShineButton2;
    private TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shine);
        newShineButton= (NewShineButton) findViewById(R.id.shine_button);
        newShineButton1= (NewShineButton) findViewById(R.id.shine_button1);
        newShineButton2= (NewShineButton) findViewById(R.id.shine_button2);
        textView=(TextView)findViewById(R.id.shine_text);

        newShineButton.setNumber(6, Color.parseColor("#f45f1d"),Color.parseColor("#a0ce8a"));
        newShineButton.setStyl(R.raw.star);
        newShineButton.setData();


        //setNumber第一个参数:个数,第二个参数:没点击的颜色,第三个参数:点击的颜色
        newShineButton1.setNumber(5, Color.parseColor("#7F7F7F"),Color.parseColor("#1396EC"));
        //setStyl是设置他的style,可以有starlikesmileheart
        newShineButton1.setStyl(R.raw.like);
        //setSize是为了设置每个小控件的大小,参数,weith:宽带 ,height:高度, leftMargin:左边距
        //默认为(100,100,12        newShineButton1.setSize(70,70, 12);
        //setData方法必须要在最后,要设置好数据才可以生成
        newShineButton1.setData();

        newShineButton2.setNumber(6, Color.parseColor("#f45f1d"),Color.parseColor("#a0ce8a"));
        newShineButton2.setStyl(R.raw.heart);
        newShineButton2.setData();


        //这是为了拿到每个NewShineButton里面的TextView,然后根据text改变监听来获取点击到第几个
         TextView text=newShineButton.getText();
        text.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                textView.setText("点击了"+s.toString()+"");
            }
        });



    }
}
	

非常容易的使用,让你打App点赞变得更加有活力~~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值