(第二阶段)冲刺十

摘要:实现签到功能

主要代码:

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#f4f0de"
    android:orientation="vertical">

    <include layout="@layout/head_title" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">


        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">


            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="130dp"
                android:background="#ffd600"
                android:gravity="center_horizontal"
                android:orientation="vertical">

                <LinearLayout
                    android:id="@+id/ll_sign"
                    android:layout_width="130dp"
                    android:layout_height="30dp"
                    android:layout_marginTop="20dp"
                    android:background="@drawable/shape_btn_e"
                    android:gravity="center"
                    android:orientation="horizontal">

                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@mipmap/icon_qd" />

                    <TextView
                        android:id="@+id/tv_sign_state"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="8dp"
                        android:text="签到"
                        android:textColor="@color/white"
                        android:textSize="17sp"
                        android:textStyle="bold" />
                </LinearLayout>

                <TextView
                    android:id="@+id/tv_sign_data"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="12dp"
                    android:text="已连续签到2天,继续加油!"
                    android:textColor="@color/white"
                    android:textSize="14.5sp" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="28dp"
                android:layout_marginTop="90dp"
                android:layout_marginRight="28dp"
                android:background="@drawable/shape_btn_f"
                android:gravity="center_horizontal"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/tv_date"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="20dp"
                    android:text="2018-12"
                    android:textColor="@color/text_one"
                    android:textSize="16sp" />

                <View
                    android:layout_width="match_parent"
                    android:layout_height="1px"
                    android:layout_marginLeft="25dp"
                    android:layout_marginTop="25dp"
                    android:layout_marginRight="25dp"
                    android:layout_marginBottom="10dp"
                    android:background="@color/line_x" />

                <com.lh.std_everything.ui.sign.SignViewX.SignView
                    android:id="@+id/activity_main_cv"
                    android:layout_width="match_parent"
                    android:layout_height="300dp"
                    android:background="@color/white" />
            </LinearLayout>
        </FrameLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginRight="8dp"
        android:background="@drawable/shape_btn_g"
        android:orientation="vertical">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="12dp"
            android:src="@mipmap/icon_zd" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:gravity="center"
            android:orientation="horizontal">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="您当前积分:"
                android:textColor="@color/text_ten"
                android:textSize="13sp" />

            <TextView
                android:id="@+id/tv_score"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="2dp"
                android:text="500"
                android:textColor="@color/text_ten"
                android:textSize="14sp" />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="4dp"
                android:src="@mipmap/icon_integral_b" />
        </LinearLayout>
    </LinearLayout>

</LinearLayout>
View Code
package com.lh.std_everything.ui.sign.SignViewX;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;


import com.lh.std_everything.R;

import java.util.Calendar;

/**
 * 签到日历控件
 * Created by David  2020/5/1.
 */
public class SignView extends View {
    private static final String[] WEEK_MARK = {"", "", "", "", "", "", ""};

    private static final int MAX_COLUMN = 7;
    /**
     * 周内
     */
    private static final int COLOR_MARKER_WEEKDAY = 0xFF999999;
    private static final int COLOR_MARKER_WEEKEND = 0xFF1B89CD;
    /**
     * 已签到背景色
     */
    private static final int COLOR_BACKGROUND_HIGHLIGHT = 0xFF1B89CD;
    /**
     * 未签到背景色
     */
    private static final int COLOR_BACKGROUND_NORMAL = 0xFFFFFFFF;
    /**
     * 等待签到背景色
     */
    private static final int COLOR_BACKGROUND_WAIT = 0xFFFE7471;
    /**
     * 已签到文字颜色
     */
    private static final int COLOR_TEXT_HIGHLIGHT = 0xFFFFFFFF;
    /**
     * 未签到文字颜色
     */
    private static final int COLOR_TEXT_NORMAL = 0xFF606060;
//    /**
//     * 不可用文字颜色
//     */
//    private static final int COLOR_TEXT_DISABLED = 0xFFD4D4D4;

    private static final int MARKER_TEXT_SIZE = 40;
    private static final int CELL_TEXT_SIZE = 40;

    private static final int VERTICAL_SPACE = 51;
    private static final int VERTICAL_MARGIN = 62;
    private static final int HORIZONTAL_MARGIN = 39;
    private static final int CELL_SIZE = 80;
    private static final int WAIT_LINE_SIZE = 14;

    private int dayOfMonthToday;
    private int markerTextY;
    private int verticalCellTop;
    private int sumDayOfMonth;
    private int daysOfFirstWeek;
    private int horizontalSpace;
    private int deltaTextCellY;
    private int deltaTextMarkerY;

    private int verticalSpace;
    private int verticalMargin;
    private int horizontalMargin;
    private int cellSize;
    private int waitLineSize;

    private Path waitPath;
    private Rect waitRect;
    private Paint paintWeekday;
    private Paint paintWeekend;
    private Paint paintTextNormal;
    private Paint paintTextHighlight;
    private Paint paintBackgroundWait;
    private Paint paintBackgroundNormal;
    private Paint paintBackgroundHighlight;
    private CalendarAdapter adapter;
    private ResolutionUtil resolutionUtil;
    private Paint paintReward;
    private Paint paintRewardX;
    public SignView(Context context) {
        this(context, null);
    }

    public SignView(Context context, AttributeSet attrs) {
        this(context, attrs, -1);
    }

    public SignView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initResolution();
        initPaint();
        initData();
    }

    private void initResolution() {
        resolutionUtil = ResolutionUtil.getInstance();
        verticalSpace = resolutionUtil.formatVertical(VERTICAL_SPACE);
        verticalMargin = resolutionUtil.formatVertical(VERTICAL_MARGIN);
        horizontalMargin = resolutionUtil.formatHorizontal(HORIZONTAL_MARGIN);
        cellSize = resolutionUtil.formatVertical(CELL_SIZE);
        waitLineSize = resolutionUtil.formatVertical(WAIT_LINE_SIZE);
    }

    private void initPaint() {
        int markerTextSize = resolutionUtil.formatVertical(MARKER_TEXT_SIZE);
        int cellTextSize = resolutionUtil.formatVertical(CELL_TEXT_SIZE);

        paintWeekday = new Paint();
        paintWeekday.setAntiAlias(true);
        paintWeekday.setColor(COLOR_MARKER_WEEKDAY);
        paintWeekday.setTextSize(markerTextSize);
        paintWeekday.setTextAlign(Paint.Align.CENTER);

        paintWeekend = new Paint();
        paintWeekend.setAntiAlias(true);
        paintWeekend.setColor(COLOR_MARKER_WEEKEND);
        paintWeekend.setTextSize(markerTextSize);
        paintWeekend.setTextAlign(Paint.Align.CENTER);

        paintTextNormal = new Paint();
        paintTextNormal.setAntiAlias(true);
        paintTextNormal.setColor(COLOR_TEXT_NORMAL);
        paintTextNormal.setTextSize(cellTextSize);
        paintTextNormal.setTextAlign(Paint.Align.CENTER);

        //签到画笔颜色
        paintTextHighlight = new Paint();
        paintTextHighlight.setAntiAlias(true);
        paintTextHighlight.setAlpha(00);//透明度
        paintTextHighlight.setTextSize(cellTextSize);
        paintTextHighlight.setTextAlign(Paint.Align.CENTER);

        paintBackgroundWait = new Paint();
        paintBackgroundWait.setAntiAlias(true);
        paintBackgroundWait.setColor(COLOR_BACKGROUND_WAIT);
        paintBackgroundWait.setStrokeWidth(2);
        paintBackgroundWait.setStyle(Paint.Style.STROKE);

        //未签到 背景
        paintBackgroundNormal = new Paint();
        paintBackgroundNormal.setAntiAlias(true);
        paintBackgroundNormal.setColor(COLOR_BACKGROUND_NORMAL);
        paintBackgroundNormal.setStrokeWidth(2);
        paintBackgroundNormal.setStyle(Paint.Style.FILL);


        //已签到 背景
        paintBackgroundHighlight = new Paint();
        paintBackgroundHighlight.setAntiAlias(true);
       // paintBackgroundHighlight.setColor(COLOR_BACKGROUND_HIGHLIGHT);
        paintBackgroundHighlight.setStrokeWidth(2);
        paintBackgroundHighlight.setStyle(Paint.Style.FILL);

        //奖项
        paintReward = new Paint();
        paintReward.setAntiAlias(true);
        paintReward.setStrokeWidth(2);
        paintReward.setStyle(Paint.Style.FILL);

        paintRewardX = new Paint();
        paintRewardX.setAntiAlias(true);
        paintRewardX.setAlpha(00);//透明度
        paintRewardX.setTextSize(cellTextSize);
        paintRewardX.setTextAlign(Paint.Align.CENTER);
    }

    private void initData() {
        Paint.FontMetricsInt fmiMarker = paintWeekday.getFontMetricsInt();
        deltaTextMarkerY = -(fmiMarker.bottom - fmiMarker.top) / 2 - fmiMarker.top;
        markerTextY = verticalMargin + cellSize / 2;
        Paint.FontMetricsInt fmiCell = paintTextNormal.getFontMetricsInt();
        deltaTextCellY = -(fmiCell.bottom - fmiCell.top) / 2 - fmiCell.top;
        verticalCellTop = verticalMargin + cellSize;

        Calendar calendarToday = Calendar.getInstance();
        dayOfMonthToday = calendarToday.get(Calendar.DAY_OF_MONTH);
        int dayOfWeek;
        sumDayOfMonth = calendarToday.getActualMaximum(Calendar.DAY_OF_MONTH);

        Calendar calendarFirstDay = Calendar.getInstance();
        calendarFirstDay.set(Calendar.DAY_OF_MONTH, 1);
        dayOfWeek = calendarFirstDay.get(Calendar.DAY_OF_WEEK);
        if (dayOfWeek == Calendar.SUNDAY) {
            dayOfWeek = 7;
        } else {
            dayOfWeek = dayOfWeek - 1;
        }
        daysOfFirstWeek = MAX_COLUMN - dayOfWeek + 1;
    }

    private void createWaitBackground(int topX, int topY) {
        waitPath = new Path();
        waitPath.moveTo(topX, topY + waitLineSize);
        waitPath.lineTo(topX, topY);
        waitPath.lineTo(topX + waitLineSize, topY);

        waitPath.moveTo(topX + cellSize - waitLineSize, topY + cellSize);
        waitPath.lineTo(topX + cellSize, topY + cellSize);
        waitPath.lineTo(topX + cellSize, topY + cellSize - waitLineSize);

        waitRect = new Rect(topX, topY, topX + cellSize, topY + cellSize);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        horizontalSpace = (w - MAX_COLUMN * cellSize - horizontalMargin * 2) / (MAX_COLUMN - 1);
    }

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);
        drawWeekMark(canvas);
        drawCellsBackground(canvas);
        drawCells(canvas);
    }

    private void drawWeekMark(Canvas canvas) {
        int y = markerTextY + deltaTextMarkerY;
        for (int i = 0; i < 7; i++) {
            int x = horizontalMargin + i * (horizontalSpace + cellSize)
                    + cellSize / 2;
            if (i < 5) {
                canvas.drawText(WEEK_MARK[i], x, y, paintWeekday);
            } else {
                canvas.drawText(WEEK_MARK[i], x, y, paintWeekend);
            }
        }
    }

    private void drawCellsBackground(Canvas canvas) {
        for (int i = 1; i <= dayOfMonthToday; i++) {
            drawCellBackground(canvas, i, getColumnIndex(i), getRowIndex(i));
        }
    }

    /**
     * 根据行列序号绘制日期背景
     *
     * @param canvas     画布
     * @param dayOfMonth 日期
     * @param column     列序号
     * @param row        行序号
     */
    private void drawCellBackground(Canvas canvas, int dayOfMonth, int column, int row) {
        int x = horizontalMargin + column * (horizontalSpace + cellSize)
                + cellSize / 2;
        int y = verticalCellTop + verticalSpace * (row + 1) + cellSize * row + cellSize / 2;
        if (adapter != null) {
            DayType dayType = adapter.getType(dayOfMonth);
            switch (dayType) {
                case WAITING:
                    if (waitPath == null) {
                        createWaitBackground(x - cellSize / 2, y - cellSize / 2);
                    }
                    canvas.drawPath(waitPath, paintBackgroundWait);
                    break;
                case SIGNED:
                    canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.icon_m), x-30, y-30, paintBackgroundHighlight);
                    break;
                case REWARD:
                   canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.icon_j), x-30, y-30, paintReward);
                   // canvas.drawCircle(x, y, cellSize / 2, paintBackgroundNormal);
                    break;
                default:
                    canvas.drawCircle(x, y, cellSize / 2, paintBackgroundNormal);
                    break;
            }
        } else {
            canvas.drawCircle(x, y, cellSize / 2, paintBackgroundNormal);
        }
    }

    private void drawCells(Canvas canvas) {
        for (int i = 1; i <= sumDayOfMonth; i++) {
            drawCell(canvas, i, getColumnIndex(i), getRowIndex(i));
        }
    }

    /**
     * 根据行列序号绘制日期
     *
     * @param canvas     画布
     * @param dayOfMonth 日期
     * @param column     列序号
     * @param row        行序号
     */
    private void drawCell(Canvas canvas, int dayOfMonth, int column, int row) {
        int x = horizontalMargin + column * (horizontalSpace + cellSize)
                + cellSize / 2;
        int y = verticalCellTop + verticalSpace * (row + 1) + cellSize * row + cellSize / 2
                + deltaTextCellY;
        if (adapter != null && dayOfMonth <= dayOfMonthToday) {
            DayType dayType = adapter.getType(dayOfMonth);
            Paint paint;
            switch (dayType) {
                case SIGNED:
                    paint = paintTextHighlight;
                    break;
                case REWARD:
                    paint = paintRewardX;
                    break;
                default:
                    paint = paintTextNormal;
                    break;
            }
            canvas.drawText(String.valueOf(dayOfMonth), x, y, paint);
        } else {
            canvas.drawText(String.valueOf(dayOfMonth), x, y, paintTextNormal);
        }
    }

    /**
     * 获取列序号
     *
     * @param dayOfMonth 日期
     * @return 列序号
     */
    private int getColumnIndex(int dayOfMonth) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
        if (dayOfWeek == Calendar.SUNDAY) {
            dayOfWeek = 6;
        } else {
            dayOfWeek = dayOfWeek - 2;
        }
        return dayOfWeek;
    }

    /**
     * 获取行序号
     *
     * @param dayOfMonth 日期
     * @return 行序号
     */
    private int getRowIndex(int dayOfMonth) {
        float weight = (dayOfMonth - daysOfFirstWeek) / (MAX_COLUMN * 1f);
        double rowIndexDouble = Math.abs(Math.ceil(weight));
        return (int) rowIndexDouble;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_UP) {
            float x = event.getX();
            float y = event.getY();
            if (waitPath != null) {
                if (adapter.getType(dayOfMonthToday).equals(DayType.WAITING)) {
                    if (x >= waitRect.left && y >= waitRect.top && x <= waitRect.right && y <= waitRect.bottom) {
                        if (onTodayClickListener != null) {
                            onTodayClickListener.onTodayClick();
                        }
                    }
                }
            }
        }
        return true;
    }

    public void setAdapter(CalendarAdapter adapter) {
        this.adapter = adapter;
        this.invalidate();
    }

    public int getDayOfMonthToday() {
        return dayOfMonthToday;
    }

    public void notifyDataSetChanged() {
        invalidate();
    }

    private OnTodayClickListener onTodayClickListener;

    public void setOnTodayClickListener(OnTodayClickListener onTodayClickListener) {
        this.onTodayClickListener = onTodayClickListener;
    }

    public interface OnTodayClickListener {
        void onTodayClick();
    }

    public enum DayType {
        /**
         * 已签到状态,时间已过
         */
        SIGNED(0),
        /**
         * 未签到状态,时间已过
         */
        UNSIGNED(1),
        /**
         * 等待状态,即当日还未签到
         */
        WAITING(2),
        /**
         * 不可达到状态,未到时间
         */
        UNREACHABLE(3),
        /**
         * 不可用状态,非当前月份
         */
        DISABLED(5),

        /**
         * 奖赏
         */
        REWARD(4);

        private int value;

        DayType(int value) {
            this.value = value;
        }

        public int getValue() {
            return value;
        }

        public static DayType valueOf(int value) {
            switch (value) {
                case 0:
                    return SIGNED;
                case 1:
                    return UNSIGNED;
                case 2:
                    return WAITING;
                case 3:
                    return UNREACHABLE;
                case 4:
                    return REWARD;
                case 5:
                    return DISABLED;
                default:
                    return DISABLED;
            }
        }
    }
}
View Code
package com.lh.std_everything.ui.sign.SignViewX;


public final class StringUtil {
    /**
     * Check whether the input string is empty
     *
     * @param input String
     * @return boolean
     */
    public static boolean isEmpty(String input) {
        if (input == null || "".equals(input))
            return true;

        for (int i = 0; i < input.length(); i++) {
            char c = input.charAt(i);
            if (c != ' ' && c != '\t' && c != '\r' && c != '\n') {
                return false;
            }
        }
        return true;
    }

    public static int getStringLength(int number) {
        return String.valueOf(number).length();
    }
}
View Code
package com.lh.std_everything.ui.sign;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;

import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lh.std_everything.R;
import com.lh.std_everything.ui.sign.SignViewX.ResolutionUtil;
import com.lh.std_everything.ui.sign.SignViewX.SignAdapter;
import com.lh.std_everything.ui.sign.SignViewX.SignEntity;
import com.lh.std_everything.ui.sign.SignViewX.SignView;
import com.zhy.autolayout.AutoLinearLayout;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class SignActivity extends AppCompatActivity {


    private List<SignEntity> data;
    private TextView headTitle;
    private TextView tvSignState;
    private LinearLayout llSign;
    private TextView tvSignData;
    private TextView tvDate;
    private SignView signView;
    private TextView tvScore;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        ResolutionUtil.getInstance().init(this);
        setContentView(R.layout.activity_sign);
        initView();
        initData();
    }


    private void initView() {
        headTitle = findViewById(R.id.head_title);
        tvSignState = findViewById(R.id.tv_sign_state);
        llSign = findViewById(R.id.ll_sign);
        tvSignData = findViewById(R.id.tv_sign_data);
        tvDate = findViewById(R.id.tv_date);
        signView = findViewById(R.id.activity_main_cv);
        tvScore = findViewById(R.id.tv_score);


    }


    private void initData() {
        headTitle.setText("每日签到");
        if (signView != null) {
            signView.setOnTodayClickListener(onTodayClickListener);
        }
        onReady();

        llSign.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onSign();
            }
        });


    }

    private void onReady() {
        Calendar calendar = Calendar.getInstance();
        int month = calendar.get(Calendar.MONTH);
        tvSignData.setText("您已经连续签到2天");
        tvScore.setText(String.valueOf(305));
        tvDate.setText(String.valueOf(calendar.get(Calendar.YEAR)) + "-" + String.valueOf(calendar.get(Calendar.MONTH) + 1));
        Calendar calendarToday = Calendar.getInstance();
        int dayOfMonthToday = calendarToday.get(Calendar.DAY_OF_MONTH);
        data = new ArrayList<>();
        //已经签到
        List<Integer> integers = new ArrayList<>();
        integers.add(1);
        integers.add(2);
        //奖品设置
        List<Integer> integersX = new ArrayList<>();
        integersX.add(5);
        integersX.add(10);
        for (int i = 0; i < 30; i++) {
            SignEntity signEntity = new SignEntity();
            if (dayOfMonthToday == i + 1) {
                signEntity.setDayType(2);
            } else {

                if (integersX.contains(i)) {
                    signEntity.setDayType(4);
                } else {
                    signEntity.setDayType((integers.contains(i + 1)) ? 0 : 1);
                }

                data.add(signEntity);
            }
        }
        SignAdapter signAdapter = new SignAdapter(data);
        signView.setAdapter(signAdapter);
    }


    private void onSign() {
        FragmentManager fragmentManager = getSupportFragmentManager();
        SignDialogFragment signDialogFragment = SignDialogFragment.newInstance(15);
        signDialogFragment.setOnConfirmListener(onConfirmListener);
        signDialogFragment.show(fragmentManager, SignDialogFragment.TAG);
    }

    private void signToday() {
        data.get(signView.getDayOfMonthToday() - 1).setDayType(SignView.DayType.SIGNED.getValue());
        signView.notifyDataSetChanged();
        llSign.setEnabled(false);
        tvSignState.setText(R.string.have_signed);
        int score = Integer.valueOf((String) tvScore.getText());
        tvScore.setText(String.valueOf(score + 15));
    }

    private SignView.OnTodayClickListener onTodayClickListener = new SignView.OnTodayClickListener() {
        @Override
        public void onTodayClick() {
            onSign();
        }
    };


    private SignDialogFragment.OnConfirmListener onConfirmListener = new SignDialogFragment.OnConfirmListener() {
        @Override
        public void onConfirm() {
            signToday();
        }
    };

}
View Code
package com.lh.std_everything.ui.sign;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.AbsoluteSizeSpan;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatDialog;
import androidx.appcompat.app.AppCompatDialogFragment;
import androidx.appcompat.widget.AppCompatButton;

import com.lh.std_everything.R;
import com.lh.std_everything.ui.sign.SignViewX.ResolutionUtil;
import com.lh.std_everything.ui.sign.SignViewX.StringUtil;


/**
 * SignDialogFragment
 */
public class SignDialogFragment extends AppCompatDialogFragment {
    public static final String TAG = "SIGN";
    public static final String ARG_SCORE_NUMBER = "ARG_SCORE_NUMBER";
    private int score;
    private ResolutionUtil resolutionUtil;

    public SignDialogFragment() {
        resolutionUtil = ResolutionUtil.getInstance();
    }

    public static SignDialogFragment newInstance(int score) {
        SignDialogFragment signDialogFragment = new SignDialogFragment();
        Bundle bundle = new Bundle();
        bundle.putInt(ARG_SCORE_NUMBER, score);
        signDialogFragment.setArguments(bundle);
        return signDialogFragment;
    }


    @SuppressLint("InflateParams")
    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        View contentView = getActivity().getLayoutInflater().inflate(R.layout.fragment_dialog_sign, null);
        getParam();
        initView(contentView);

        AppCompatDialog appCompatDialog = new AppCompatDialog(getContext(), R.style.DialogNoTitle);
        appCompatDialog.setContentView(contentView, new ViewGroup.LayoutParams(resolutionUtil.formatHorizontal(961),
                ViewGroup.LayoutParams.WRAP_CONTENT));
        appCompatDialog.setCanceledOnTouchOutside(false);
        return appCompatDialog;
    }

    private void getParam() {
        if (getArguments() != null) {
            score = getArguments().getInt(ARG_SCORE_NUMBER, 0);
        }
    }

    @SuppressLint("RestrictedApi")
    private void initView(View root) {
        TextView tvMessage = (TextView) root.findViewById(R.id.dialog_sign_tv_message);
        TextView tvScoreNumber = (TextView) root.findViewById(R.id.dialog_sign_tv_score_number);
        AppCompatButton btnSure = root.findViewById(R.id.dialog_sign_btn_sure);

        int scoreLength = StringUtil.getStringLength(score);
        String numberStr = String.format(getString(R.string.much_score_int), score);

        SpannableString spannableString = new SpannableString(numberStr);
        spannableString.setSpan(new AbsoluteSizeSpan(resolutionUtil.formatVertical(120)), 0, scoreLength, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        spannableString.setSpan(new AbsoluteSizeSpan(resolutionUtil.formatVertical(39)), scoreLength, numberStr.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        tvScoreNumber.setText(spannableString);

        btnSure.setSupportBackgroundTintList(getResources().getColorStateList(R.color.color_user_button_submit));
        btnSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getDialog().dismiss();
                if (onConfirmListener != null) {
                    onConfirmListener.onConfirm();
                }
            }
        });

        //------------------------------分辨率适配------------------------------------
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.topMargin = resolutionUtil.formatVertical(67);
        layoutParams.gravity = Gravity.CENTER_HORIZONTAL;
        tvMessage.setLayoutParams(layoutParams);
        tvMessage.setTextSize(TypedValue.COMPLEX_UNIT_PX, resolutionUtil.formatVertical(44));

        layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.topMargin = resolutionUtil.formatVertical(39);
        layoutParams.gravity = Gravity.CENTER_HORIZONTAL;
        tvScoreNumber.setLayoutParams(layoutParams);

        layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.topMargin = resolutionUtil.formatVertical(61);
        layoutParams.bottomMargin = resolutionUtil.formatVertical(67);
        layoutParams.leftMargin = layoutParams.rightMargin = resolutionUtil.formatHorizontal(73);
        btnSure.setLayoutParams(layoutParams);
        btnSure.setTextSize(TypedValue.COMPLEX_UNIT_PX, resolutionUtil.formatVertical(48));
    }

    private OnConfirmListener onConfirmListener;

    public void setOnConfirmListener(OnConfirmListener onConfirmListener) {
        this.onConfirmListener = onConfirmListener;
    }

    public interface OnConfirmListener {
        void onConfirm();
    }
}
View Code

截图:

 

posted @ 2020-06-03 12:27  史莱克三怪队  阅读(130)  评论(0编辑  收藏  举报