1.自定义属性values下创建attrs
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="myView"> <attr name="outRadius" format="integer"></attr> <attr name="inRadius" format="integer"></attr> <attr name="ringColor" format="color"></attr> <attr name="text" format="string"></attr> <attr name="textSize" format="integer"></attr> </declare-styleable> </resources>2.activity_main布局
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".activity.MainActivity"> <com.baway.yuekao5.view.MyView android:id="@+id/myView" android:layout_width="wrap_content" android:layout_height="wrap_content" app:outRadius="100" app:inRadius="50" app:ringColor="#ffff00" app:text="圆环" app:textSize="20" android:layout_centerInParent="true"/> </RelativeLayout>3.自定义View
public class MyView extends View{ private int outRadius; private int inRadius; private int ringColor; private int textSize; private String text; private int centerX; private int centerY; private float pointX; private float pointY; private Paint paint; private OnCircleClickListener listener; public MyView(Context context) { super(context); } public MyView(Context context, AttributeSet attrs) { super(context, attrs); initAttr(attrs); } private void initAttr(AttributeSet attrs) { TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.myView); outRadius = typedArray.getInt(R.styleable.myView_outRadius, -1); inRadius = typedArray.getInt(R.styleable.myView_inRadius,-1); ringColor = typedArray.getColor(R.styleable.myView_ringColor,-1); text = typedArray.getString(R.styleable.myView_text); textSize = typedArray.getInt(R.styleable.myView_textSize,-1); } public MyView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } public interface OnCircleClickListener{ void onCircleClick(String text); } public void setOnCircleClickListener(OnCircleClickListener listener){ this.listener=listener; } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); setMeasuredDimension(outRadius*2,outRadius*2); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); centerX=outRadius; centerY=outRadius; canvas.drawColor(Color.GREEN); paint = new Paint(); paint.setStyle(Paint.Style.FILL); paint.setColor(ringColor); paint.setAntiAlias(true); canvas.drawCircle(centerX,centerY,outRadius,paint); paint.setColor(Color.WHITE); canvas.drawCircle(centerX,centerX,inRadius,paint); paint.setTextSize(textSize); paint.setColor(Color.BLACK); canvas.drawText(text,centerX-text.length()/2*textSize,centerY+textSize/2,paint); } @Override public boolean onTouchEvent(MotionEvent event) { if (event.getAction()==MotionEvent.ACTION_DOWN){ pointX=event.getX(); pointY=event.getY(); double range = Math.sqrt((centerX - pointX) * (centerX - pointX) + (centerY - pointY) * (centerY - pointY)); String text=""; if (range<=inRadius){ text="在小圆内"; }else if (range>inRadius&&range<=outRadius){ text="在圆环内"; }else if (range>outRadius){ text="在圆环外"; } if (listener!=null){ listener.onCircleClick(text); } } return true; } }4.MainActivity代码
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); MyView myView = (MyView) findViewById(R.id.myView); myView.setOnCircleClickListener(new MyView.OnCircleClickListener() { @Override public void onCircleClick(String text) { Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT).show(); if (text.equals("在圆环内")){ Intent intent=new Intent(MainActivity.this,SecondActivity.class); startActivity(intent); } } }); } }