4

所以我有这个扩展活动的类。但是我想在屏幕上画一些东西,所以我需要制作一个画布。但是我不能扩展 View,因为它已经是一项活动。我该怎么办?

我的活动有我用来做一些事情的 onClick 方法,但我想做的是在调用 onClick 方法时绘制一个简单的图像。

谢谢。

public class Stuff extends Activity implements OnClickListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
(...)
}

@Override
public void onClick(View arg0) {
(...)
}
4

3 回答 3

6

STEP 1:通过 extends View 创建一个类:

public class DrawView extends View {  
    public float currentX=40;  
    public float currentY=50;  

    public DrawView(Context context) {  
        super(context);  
        // TODO Auto-generated constructor stub  
    }  

    @Override  
    protected void onDraw(Canvas canvas) {        
        super.onDraw(canvas);   
        Paint paint=new Paint();  
        paint.setColor(Color.RED);  
        canvas.drawCircle(currentX, currentY, 25, paint);  
    }  

} 

第 2 步:在您的材料活动中:

public class Stuff extends Activity implements OnClickListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);  
setContentView(R.layout.main);  
LinearLayout root=(LinearLayout) findViewById(R.id.root); 
(...)
}

@Override
public void onClick(View arg0) {
//DRAW YOUR VIEW ON BUTTON CLICK
final DrawView drawView=new DrawView(this);  
drawView.setMinimumWidth(300);  
drawView.setMinimumHeight(500);
drawView.currentX=200;  
drawView.currentY=200;  
drawView.invalidate(); 
root.addView(drawView);
(...)
}

第 3 步:您的 Activity main.xml 为:

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"  
    android:orientation="vertical"   
    android:background="#99FFCC"  
    android:id="@+id/root">  
</LinearLayout>

最后在这里提问之前尝试在谷歌上搜索。谢谢

于 2012-06-29T21:46:43.307 回答
1

您可以在您的活动中声明一个内部类,以便参考以下代码:

    public class GraphicsTest extends Activity {
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(new GraphicsTestView(this));
  }


  private static class GraphicsTestView extends View
  {
    private ShapeDrawable mDrawable =
        new ShapeDrawable();
    /* Drawable's are objects which can be drawn on a Canvas
       ShapeDrawable is used to draw primitive shapes such as:
       ArcShape, OvalShape, PathShape, RectShape, RoundRectShape
       A Canvas is the object provided by Android on which
       a view tries to draw itself. In addition to ShapeDrawable,
       there are other subclasses of Drawable like PictureDrawable,
       RotateDrawable, ScaleDrawable, ClipDrawable,GradientDrawable, etc
       Some of these we will see when we consider the XML approach to
       graphics 
     */

    public GraphicsTestView (Context context) 
    {
       super(context);
       setFocusable(true);
       this.mDrawable.getPaint().setColor(0xFFFF0000); 
           //argb where a is alpha (transparency)
    }

    @Override
    protected void onDraw(Canvas canvas)
    /* the onDraw method is where a view draws itself
           this is our first time overriding it.
         */
    {
        int x = 10;
        int y = 10;
        int width = 300;
        int height = 50;
        this.mDrawable.setBounds(x, y, x + width, y + height);
        this.mDrawable.draw(canvas);

        ArcShape arc = new ArcShape(45,90); //start angle, sweep angle
        ShapeDrawable test = new ShapeDrawable(arc);

        Paint p = test.getPaint();
        p.setColor(0xFF00FFFF);

        p.setStyle(Paint.Style.STROKE);

        test.setBounds(10, 70, 310, 370); 
            //Top-Left, Bottom Right of rectangle to draw into
        test.draw(canvas);

        }
    }
}
于 2012-06-29T21:41:40.037 回答
0

您是说要在 XML 文件中获取布局视图吗?您可以在其中绘制视图,让您的代码调用它并查看它,然后将图像设置为在单击时响应。

在您的 onCreate 方法中,super.onCreate(savedInstanceState);添加此setContentView(R.id.layoutname)

例如

@Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

至于 onClickListener,你可以设置视图来实现它,因为你已经在 Activity 中实现了它。

    // set this after "setContentView(R.layout.main);"
b1 = (Button)findViewById(R.id.main);
b1.setOnClickListener(this);
于 2012-06-29T21:35:22.313 回答