【问题标题】:How to set android xml layout to custom view and be able to draw on it如何将android xml布局设置为自定义视图并能够在其上绘图
【发布时间】:2014-11-09 20:26:24
【问题描述】:

我想将 xml 布局添加到自定义视图。它在代码的注释部分完成。但是我这样做之后就不能画画了。此代码在屏幕中心绘制红色圆圈。取消注释注释行后,不绘制圆圈。

public class Main extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(new MyView(this));
}

public class MyView extends View {

    Paint paint;

    public MyView(Context context) {
         super(context);

//      View view = inflate(context, R.layout.activity_main_zadanie, null);
//      view.setFocusable(true);
//        addView(view);

         paint = new Paint();
    }

    @Override
    protected void onDraw(Canvas canvas) {
       // TODO Auto-generated method stub
       super.onDraw(canvas);
       int x = getWidth();
       int y = getHeight();
       int radius;
       radius = 100;
       paint.setStyle(Paint.Style.FILL);
       paint.setColor(Color.WHITE);
       canvas.drawPaint(paint);
       // Use Color.parseColor to define HTML colors
       paint.setColor(Color.parseColor("#CD5C5C"));
       canvas.drawCircle(x / 2, y / 2, radius, paint);
   }
}

}

【问题讨论】:

    标签: android xml view graphics draw


    【解决方案1】:

    您的新视图可能覆盖了自定义视图上的画布。尝试在 R.layout.activity_main_zadanie 中将背景设置为半透明。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-16
      • 2021-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多