简体   繁体   English

如何在Android中使用画布在TextView前面绘制一个圆圈

[英]How to draw a circle in front of a textview with canvas in android

I made a code where a circle moves around, but whenever it moves in front of a textview, the textview gets in front of him and I want him to be in front of the textview. 我编写了一个代码,圆圈在其中移动,但是每当圆圈在textview前面移动时,textview就会在他前面,并且我希望他在textview前面。 I tried drawing the circle after making the textview but it doesn't fix it. 我尝试在制作textview后绘制圆圈,但无法解决问题。 Example: 例:

public MainView(Context context) {
    super(context);
}
public void onDraw(Canvas canvas){
    ((ViewGroup) text.getParent()).removeView(text);//the text was already added to the activity
    Paint paint=new Paint();
    paint.setColor(Color.WHITE);
    canvas.drawCircle(0.0,0.0,500.0, paint);
    main.addContentView(text, parameters);//adding a textview named text in the activity
    invalidate();   
}

Don't create Views inside onDraw, it's called so many times and you will have a lot of TextViews but you only need one, normally created on activity onCreate. 不要在onDraw内创建View,它被调用了很多次,您将拥有很多TextView,但是您只需要一个通常在活动onCreate上创建的TextView。

The TextView seems in front due the children is clipped when drawing, try setting clipChildren to false in the ViewGroup container 由于绘制时会修剪孩子,因此TextView似乎在前面,请尝试在ViewGroup容器中将clipChildren设置为false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM