如何在Android中的扩展视图中添加画布?

时间:2013-07-20 16:14:23

标签: android android-canvas android-framelayout

这是我的扩展视图:

public class PieMenu extends FrameLayout{



    private View _view;
    private ArrayList<PieItem> mItems;
    private Context _context;


    public PieMenu(View view, Context context) {
        super(context);
        _context = context;
        _view = view;
        setWillNotDraw(false);
        // TODO Auto-generated constructor stub
    }



    public void addPieMenu(int x, int y){

        mItems = new ArrayList<PieItem>();
        Path path = new Path();
        path.addCircle(9, 9, 9, Path.Direction.CW);
        PieItem item = new PieItem(_view,_context,path, x,y,40);
        mItems.add(item);
        //FrameLayout.LayoutParams lyp = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
        //pieView.setLayoutParams(lyp); 
        //addView(pieView);
        // animateIn();

        invalidate();

    }



      @Override
        protected void onDraw(Canvas canvas) {

         Paint  paint1 = new Paint();
         paint1.setColor(Color.RED); 
         canvas.drawCircle(50, 50, 25, paint1);

          this.draw(canvas); 
          //_view.draw(canvas);

      } 




}

这是我的主要活动:

public class MainActivity extends Activity {
    PieMenu pieMenu;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        pieMenu = new PieMenu(getWindow().getDecorView().findViewById(android.R.id.content) ,MainActivity.this);


        FrameLayout fl = (FrameLayout)findViewById(R.id.flayout);
        fl.addView(pieMenu);  


    }
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int x = (int)event.getX();
        int y = (int)event.getY();
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
            {
                pieMenu.addPieMenu(x,y);
            }
            case MotionEvent.ACTION_MOVE:
            case MotionEvent.ACTION_UP:
        }
    return false;
    }

}

这是我的主要活动文件:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/flayout"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world" />

    <com.example.piemenu.PieMenu
      android:id="@+id/circle_view"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent" 
      android:layout_centerInParent="true"  />  

</FrameLayout>

当我编译它时,应用程序崩溃时出现此错误:

07-20 21:38:45.906: E/AndroidRuntime(10088): Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]

我哪里错了?

1 个答案:

答案 0 :(得分:2)

您需要在PieMenu班级中使用这些构造函数:

public PieMenu(View view, Context context) {
    super(context);
    // ~
}

public PieMenu(View view, Context context, AttributeSet attrs) {
    super(context, attrs);
    // ~
}

修改1

public PieMenu(View view, Context context, AttributeSet attrs, int style) {
    super(context, attrs, style);
    // ~
}