所以我有这个类扩展了一个活动。但是我想在屏幕上画一些东西,所以我需要制作一个画布。但是我无法扩展View,因为它已经是一项活动。我该怎么办?
我的活动有onClick方法,我用它来做一些事情,但我想做的是在调用onClick方法时绘制一个简单的图像。
感谢。
public class Stuff extends Activity implements OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
(...)
}
@Override
public void onClick(View arg0) {
(...)
}
答案 0 :(得分:5)
第1步:通过扩展视图创建一个类:
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>
最后尝试在google上搜索,然后在这里提问。谢谢
答案 1 :(得分:1)
您可以在ur活动中声明一个内部类,以便参考此代码:
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);
}
}
}
答案 2 :(得分: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);