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