如何在活动上创建视图

4

我有一个继承自Activity的类。但是我想在屏幕上画一些东西,所以我需要创建一个画布(Canvas)。然而,由于已经是一个活动(Activity),我无法继承View。我该怎么办?

我的活动(Activity)有一个onClick方法,我用它来做一些事情,但是当我调用onClick方法时,我想画一个简单的图像。

谢谢。

public class Stuff extends Activity implements OnClickListener {

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

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

1
如果你无法通过那个阶段,你确实需要跟随一些Android教程。可以从这里开始尝试...http://developer.android.com/training/index.html - Squonk
3个回答

6

步骤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);
(...)
}

步骤三:将您的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>

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

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);

        }
    }
}

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);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接