android 旋钮 旋钮开关 自定义控件

android 旋钮 旋钮开关 自定义控件

android 旋钮 旋钮开关 自定义控件

在这里插入图片描述

package com.sun.demo5;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ImageView;
import android.widget.RelativeLayout;


/**
 *
 */
public class KnobView extends RelativeLayout {
    float midx, midy;
    Paint textPaint;
    Paint circlePaint;
    public Paint circlePaint2;
    public Paint linePaint;
    String angle;
    float currdeg, deg = 3, downdeg;
    public static int themeColor = Color.parseColor("#B24242");
    private ImageView imageView;
    String label;

    String name;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    private String avenue;
    private String spareAvenue;

    public String getAvenue() {
        return avenue;
    }

    public void setAvenue(String avenue) {
        this.avenue = avenue;
    }

    public String getSpareAvenue() {
        return spareAvenue;
    }

    public void setSpareAvenue(String spareAvenue) {
        this.spareAvenue = spareAvenue;
    }
    private OnMyMoveListener moveListener;
    public void setOnMyMoveListener(OnMyMoveListener listener){
        this.moveListener=listener;
    }

    public KnobView(Context context) {
        super(context);
        init(context);
    }

    public KnobView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public KnobView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }



    void init(Context context) {
        textPaint = new Paint();
        textPaint.setColor(Color.WHITE);
        textPaint.setStyle(Paint.Style.FILL);
        textPaint.setTextSize(20);
        textPaint.setFakeBoldText(true);
        textPaint.setTextAlign(Paint.Align.CENTER);
        circlePaint = new Paint();
        circlePaint.setColor(Color.parseColor("#222222"));
        circlePaint.setStyle(Paint.Style.FILL);
        circlePaint2 = new Paint();
        circlePaint2.setColor(themeColor);
        circlePaint2.setStyle(Paint.Style.FILL);
        linePaint = new Paint();
        linePaint.setColor(themeColor);
        linePaint.setStrokeWidth(7);
        angle = "0.0";
        label = "Label";

        this.setBackground(getResources().getDrawable(R.mipmap.img_knobview_bg));
        imageView = new ImageView(context);
        imageView.setRotation(45);
        imageView.setBackground(getResources().getDrawable(R.mipmap.img_knobview));
        LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        lp.setMargins(14,14,14,14);
        this.addView(imageView,lp);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        lp.setMargins(this.getWidth() * 8 / 87,this.getHeight() * 8 / 87,this.getWidth() * 8 / 87,this.getHeight() * 8 / 87);
        imageView.setLayoutParams(lp);
        midx = getWidth() / 2;
        midy = getHeight() / 2;
    }


    @Override
    public boolean onTouchEvent(MotionEvent e) {

        if (e.getAction() == MotionEvent.ACTION_DOWN) {
            float dx = e.getX() - midx;
            float dy = e.getY() - midy;
            downdeg = (float) ((Math.atan2(dy, dx) * 180) / Math.PI);
            downdeg -= 90;
            if (downdeg < 0) {
                downdeg += 360;
            }
            return true;
        }
        if (e.getAction() == MotionEvent.ACTION_MOVE) {
            float dx = e.getX() - midx;
            float dy = e.getY() - midy;
            currdeg = (float) ((Math.atan2(dy, dx) * 180) / Math.PI);
            currdeg -= 90;
            if (currdeg < 0) {
                currdeg += 360;
            }
            if (Math.floor(currdeg / 15) == 0 && Math.floor(downdeg / 15) == 23) {
                deg++;
                if (deg > 315) {
                    deg = 315;
                }
                downdeg = currdeg;
            } else if (Math.floor(currdeg / 15) == 23 && Math.floor(downdeg / 15) == 0) {
                deg--;
                if (deg < 45) {
                    deg = 45;
                }
                downdeg = currdeg;
            } else {
                deg += (currdeg - downdeg);
                if (deg > 315) {
                    deg = 315;
                }
                if (deg < 45) {
                    deg = 45;
                }
                downdeg = currdeg;
            }
            imageView.setRotation(deg);
            if(moveListener != null){
                this.moveListener.onSignal(avenue, name, transformFB(deg), 1);
            }
            invalidate();
            return true;
        }
        if (e.getAction() == MotionEvent.ACTION_UP) {
            return true;
        }
        return super.onTouchEvent(e);
    }

    public int transformFB(float num){
        int direction = 0;
        direction = (int) (((num - 45) / 270) * 180);
        //超值锁
        if(direction > 180){
            direction = 180;
        }
        if(direction < 0){
            direction = 0;
        }
        return direction;
    }


    public void setLabel(String txt) {
        label = txt;
    }
}

附 DEMO地址 https://download.csdn.net/download/qq_37630270/88000519

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

百思不得姐1

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值