Android自定义CIrcleView
都浩淼
2023-12-01
public class CircleView extends View {
private int viewW,viewH;
private DrawBackground mBackground;
private boolean isInitView = false;//是否初始化过此view里面的所有资源
private Activity mContext;
public CircleView(Context context, AttributeSet attrs) {
super(context, attrs);
this.mContext = (Activity) context;
}
public CircleView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.mContext = (Activity) context;
}
public CircleView(Context context) {
super(context);
this.mContext = (Activity) context;
}
private List<DrawOneRowView> rowView = new ArrayList<DrawOneRowView>();
private String menuStr[][] = new String[][]{
{"00菜单","01菜单","02菜单","03菜单","04菜单","05菜单",},
{"10菜单","11菜单","12菜单","13菜单","14菜单"},
{"20菜单","21菜单","22菜单","23菜单","24菜单","25菜单",},
// {"30菜单","31菜单","32菜单","33菜单","34菜单","35菜单",},
};
private void init(){
int RR = viewH*1/2;//最大半径调整 在此处
if(viewW < viewH){
RR = viewW*1/2;
}
int r = 20;//中间空出部分圆半径
int count = menuStr.length+1;//总的有几个白色圆圈
int gadr = (RR-r)/count;//间隔,两个圆圈之间的
int centerX = viewW/2;//圆的中心位置
// int centerY = viewH/2;
int centerY = viewH;//位于手机底部边沿
mBackground = new DrawBackground(centerX, centerY,RR,r,count);
for(int i=0;i<count-1;i++){
DrawOneRowView row = new DrawOneRowView(centerX, centerY, RR -gadr*i , RR -gadr*i - gadr, menuStr[i], -6*i);
final int arrayIndex = i;
row.setListener(new OnClikListener() {
@Override
public void onClick(int menuIndex) {
// TextView textview = (TextView) mContext.findViewById(R.id.textview);
if(menuIndex>=0 && menuIndex<menuStr[arrayIndex].length){
Log.w("wlb"," 您点击了菜单 : "+menuStr[arrayIndex][menuIndex]);
// textview.setText(" 您点击了菜单 : "+menuStr[arrayIndex][menuIndex]);
((FirstActivity)mContext).removeCircleView();
}
}
});
rowView.add(row);
}
}
@Override
public void onDraw(Canvas canvas) {
if(!isInitView){
isInitView = true;
viewW = this.getWidth();
viewH = this.getHeight();
init();
}
mBackground.drawSelf(canvas);
for(DrawOneRowView view : rowView){
view.drawSelf(canvas);
}
}
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouchEvent(MotionEvent event){
if(event.getAction() != MotionEvent.ACTION_DOWN){
for(DrawOneRowView view : rowView){
if(view.isFocus()){
// Log.i("invalidate", ">>>>>invalidate");
this.invalidate();
return view.onTouchEvent(event);
}
}
}else{
for(DrawOneRowView view : rowView){
if(view.onTouchEvent(event)){
this.invalidate();
return true;
}
}
}
return false;
}
}