Skip to content
  • P
    Projects
  • G
    Groups
  • S
    Snippets
  • Help

android / dayu

  • This project
    • Loading...
  • Sign in
Go to a project
  • Project
  • Repository
  • Issues 0
  • Merge Requests 0
  • Pipelines
  • Wiki
  • Snippets
  • Members
  • Activity
  • Graph
  • Charts
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
  • Files
  • Commits
  • Branches
  • Tags
  • Contributors
  • Graph
  • Compare
  • Charts
Switch branch/tag
  • dayu
  • ..
  • widget
  • WordImageView.java
Find file
Normal viewHistoryPermalink
WordImageView.java 4.98 KB
Newer Older
luofan's avatar
集成图形验证码lib
09b81edf
 
luofan committed 4 years ago
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
package com.example.verificationcodejavademo.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.verificationcodejavademo.R;
import com.example.verificationcodejavademo.model.Point;
import com.example.verificationcodejavademo.utils.DisplayUtil;
import com.google.gson.Gson;

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

/**
 * Date:2020/5/21
 * author:wuyan
 */
public class WordImageView extends FrameLayout {

    private FrameLayout word_fl_content;
    private ImageView word_iv_cover;
    private View word_v_flash;

    private Bitmap cover;
    private int size = 0;//需要点击文字的数量
    private List<Point> mList = new ArrayList<>();
    private Handler mHandler = new Handler();

    public WordImageView(@NonNull Context context) {
        super(context);
        init();
    }

    public WordImageView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public WordImageView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        View.inflate(getContext(), R.layout.word_view, this);
        word_fl_content = findViewById(R.id.word_fl_content);
        word_iv_cover = findViewById(R.id.word_iv_cover);
        word_v_flash = findViewById(R.id.word_v_flash);
        reset();
    }

    public void setSize(int size) {
        this.size = size;
    }

    public void setUp(Bitmap cover) {
        this.cover = cover;

        int w = cover.getWidth();
        int h = cover.getHeight();

        FrameLayout.LayoutParams l = (FrameLayout.LayoutParams) word_iv_cover.getLayoutParams();
        l.width = DisplayUtil.dip2px(getContext(), (float) w);
        l.height = DisplayUtil.dip2px(getContext(), (float) h);

        word_iv_cover.setLayoutParams(l);
        word_iv_cover.setImageBitmap(cover);

        setLocation(cover.getWidth(), cover.getHeight());
    }

    private void setLocation(int w, int h) {
        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) word_fl_content.getLayoutParams();
        layoutParams.width = DisplayUtil.dip2px(getContext(), (float) w);
        layoutParams.height = DisplayUtil.dip2px(getContext(), (float) h);
        word_fl_content.setLayoutParams(layoutParams);
    }

    public void ok() {
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                reset();
            }
        }, 1000);
    }

    public void fail() {
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                reset();
            }
        }, 1000);
    }

    private void reset() {
        mList.clear();
        word_fl_content.removeAllViews();
        word_fl_content.addView(word_iv_cover);
        word_fl_content.addView(word_v_flash);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            size--;
            Point point = new Point();
            point.setX(DisplayUtil.px2dip(getContext(), event.getX()));
            point.setY(DisplayUtil.px2dip(getContext(), event.getY()));
            mList.add(point);

            if (size > 0) {
                //添加小圆点
                addTextView(event);
            } else if (size == 0) {
                addTextView(event);
                if (wordListenner != null) {
                    wordListenner.onWord(new Gson().toJson(mList));
                }
            }
        }
        return true;
    }

    //点击后添加小圆点
    private void addTextView(MotionEvent event) {
        TextView textView = new TextView(getContext());
        LayoutParams l = new LayoutParams(DisplayUtil.dip2px(getContext(), 20 * 1.0f), DisplayUtil.dip2px(getContext(), 20 * 1.0f));
        textView.setLayoutParams(l);
        textView.setGravity(Gravity.CENTER);
        textView.setText(mList.size() + "");
        textView.setTextColor(Color.WHITE);
        textView.setBackground(getResources().getDrawable(R.drawable.shape_dot_bg));
        MarginLayoutParams postion = (MarginLayoutParams) textView.getLayoutParams();
        postion.leftMargin = (int) (event.getX() - 10);
        postion.topMargin = (int) (event.getY() - 10);
        word_fl_content.addView(textView);
    }

    //设置滑动监听
    private WordListenner wordListenner;

    interface WordListenner {
        void onWord(String cryptedStr);
    }

    public void setWordListenner(WordListenner wordListenner) {
        this.wordListenner = wordListenner;
    }
}