Android应用程序,带有绘制形状的按钮

时间:2013-03-10 17:40:56

标签: android

我正在尝试编写一个包含按钮和视图的程序。所以当我按下按钮时,在视图区域中绘制一个随机形状。我写了一些代码,但它不起作用。你能帮忙吗?

package org.example.anothertrydraw;

import android.os.Bundle;

import android.app.Activity;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void GoToDraw(View v)
    {
        ShapeDrawableView mTheView = (ShapeDrawableView)findViewById(R.id.TheView);
        mTheView.DrawShape();
    }
}

ShapeDrawableView类是:

package org.example.anothertrydraw;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.graphics.drawable.shapes.RectShape;
import android.graphics.drawable.shapes.Shape;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import java.util.*;
public class ShapeDrawableView extends View {
    private List<ShapeDrawable> shapes = new ArrayList<ShapeDrawable>();
    private Integer[] mColors =
        { Color.BLACK, Color.BLUE, Color.GREEN, Color.RED };



    public ShapeDrawableView(Context context) {
        super(context);
    }



    public ShapeDrawableView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        for(ShapeDrawable shape: shapes) {
            shape.draw(canvas);
        }
    }
    public void DrawShape()
    {
        int w=getWidth();
        int h=getHeight();
        int x=RandomUtils.randomInt(w);
        int y=RandomUtils.randomInt(h);
         shapes.add(makeShapeDrawable(x, y));
         invalidate();
    }
    private ShapeDrawable makeShapeDrawable(int x, int y) {
        int maxWidth = getWidth()/10;
        int maxHeight = getHeight()/10;
        Shape shape;
        if (Math.random() < 0.5) {
            shape = new OvalShape();
        } else {
            shape = new RectShape();
        }
        ShapeDrawable shapeD = new ShapeDrawable(shape);
        int width = RandomUtils.randomInt(maxWidth)+5;
        int height = RandomUtils.randomInt(maxHeight)+5;
        shapeD.setBounds(x-width/2, y-height/2, x+width/2, y+height/2);
        shapeD.getPaint().setColor(RandomUtils.randomElement(mColors));
        return(shapeD);
    }


}

RandomUtils类只生成随机数,所以我不打算把它的代码放在这里

xml文件是:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="@string/BB"
        android:onClick="GoToDraw" />
    <view
        class="org.AnotherTryDraw.MainActivity.ShapeDrawableView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" 
        android:id="@+id/TheView"/>
</LinearLayout>

0 个答案:

没有答案