Android : Draw Circle With Text Inside

前端 未结 5 770
闹比i
闹比i 2020-12-01 12:29

I need to draw three circles in my fragment ,the circles differ in size, I refer this link The result i obtained is this

5条回答
  •  北荒
    北荒 (楼主)
    2020-12-01 12:41

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final TextView tv = (TextView) findViewById(R.id.tv);
        Button btn = (Button) findViewById(R.id.btn);
        GradientDrawable gd = new GradientDrawable();
        gd.setShape(GradientDrawable.OVAL);
        gd.setColor(Color.TRANSPARENT);
        gd.setStroke(5, Color.BLUE);
        gd.setSize(getTextViewHeight(tv),getTextViewHeight(tv));
    
        tv.setBackground(gd);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
    
                GradientDrawable gd = new GradientDrawable();
                gd.setShape(GradientDrawable.OVAL);
                gd.setColor(Color.TRANSPARENT);
                gd.setStroke(5, Color.BLUE);
                gd.setSize(tv.getHeight(),tv.getHeight());
                Log.d("DB121","Width "+tv.getHeight()+ "Height ="+tv.getHeight());
                tv.setBackground(gd);
            }
        });
    }
    public static int getTextViewHeight(TextView textView) {
        WindowManager wm =
                (WindowManager) textView.getContext().getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
    
        int deviceWidth;
    
        if(android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2){
            Point size = new Point();
            display.getSize(size);
            deviceWidth = size.x;
        } else {
            deviceWidth = display.getWidth();
        }
    
        int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(deviceWidth, View.MeasureSpec.AT_MOST);
        int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        textView.measure(widthMeasureSpec, heightMeasureSpec);
        return textView.getMeasuredHeight();
    }
    

提交回复
热议问题