Android開源 - 點贊桃心的效果

Wendy32U 8年前發布 | 10K 次閱讀 安卓開發 Android開發 移動開發

仿即刻APP點贊桃心的效果

先看效果圖

likeview.gif

使用方法

布局配置

<cn.izouxiang.likeview.LikeView
            android:id="@+id/lv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:number="99"
            />

注意,一般不需要直接指定寬高,內部會根據字體大小自動測量

代碼配置

holder.likeView.setActivated(entity.isLike);
        holder.likeView.setNumber(entity.likeNum);
        holder.likeView.setCallback(new LikeView.SimpleCallback() {
            @Override
            public void activate(LikeView view) {
                Snackbar.make(view, "你覺得" + entity.name + "很贊!", Snackbar.LENGTH_SHORT).show();
            }

        @Override
        public void deactivate(LikeView view) {
            Snackbar.make(view, "你取消了對" + entity.name + "的贊!", Snackbar.LENGTH_SHORT).show();
        }
    });</code></pre> 

自定義配置

<resources>
    <declare-styleable name="LikeView">
        <!--當前數值,默認0-->
        <attr name="number" format="integer"/>
        <!--數字顏色,默認#888888-->
        <attr name="textColor" format="color"/>
        <!--圖形外邊顏色,默認#888888-->
        <attr name="graphColor" format="color"/>
        <!--當前激活時圖形顏色,默認#ca5f5f-->
        <attr name="animateColor" format="color"/>
        <!--字體大小,決定控件高度以及圖形大小,默認14sp-->
        <attr name="textSize" format="dimension"/>
        <!--動畫時間,默認300-->
        <attr name="animateDuration" format="integer"/>
        <!--圖形與數字間的距離,默認3dp-->
        <attr name="distance" format="dimension"/>
        <!--圖形與數字高度的比例,默認1.3-->
        <attr name="graphTextHeightRatio" format="float"/>
        <!--圖形外邊繪制寬度,默認3dp-->
        <attr name="graphStrokeWidth" format="dimension"/>
        <!--數字繪制寬度,默認2dp-->
        <attr name="textStrokeWidth" format="dimension"/>
        <!--是否激活,默認false-->
        <attr name="isActivated" format="boolean"/>
        <!--是否自動測量數字修改后的寬度改變,防止更改狀態時控件寬度改變,默認開啟-->
        <attr name="autoMeasureMaxWidth" format="boolean"/>
        <!--是否不允許取消點贊,默認false-->
        <attr name="notAllowedCancel" format="boolean"/>
    </declare-styleable>
</resources>

接口

/**

 * 事件監聽接口
 */
public interface Callback {
    /**
     * 點擊事件監聽
     *
     * @param view 當前View
     * @return 返回true則代表不使用默認的點擊事件
     */
    boolean onClick(LikeView view);

    /**
     * 變為激活狀態
     *
     * @param view 當前View
     */
    void activate(LikeView view);

    /**
     * 變為不激活狀態
     *
     * @param view 當前View
     */
    void deactivate(LikeView view);
}

/**
 * 獲取圖形Path接口
 */
public interface GraphAdapter {
    /**
     * 獲取圖形的Path
     *
     * @param view   當前View
     * @param length 可繪制圖形區域正方形的邊長
     * @return 帶有圖形的Path
     */
    Path getGraphPath(LikeView view, int length);
}</code></pre> 

 

 

 

 本文由用戶 Wendy32U 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!