0

我认为我的问题很小,但我找不到解决方案。我正在尝试制作可以帮助我制作 WEB 页面的应用程序,并且我想制作可以将图形元素放在屏幕上的应用程序。

我制作了矩形的可绘制 png 图像,我可以将它们放在我想要的屏幕上,没问题,但我不能制作新的图形/位图。

当我按下meni DIV或iFrame时,如何将新的图形对象放在屏幕上?

public class Objects extends View {
    private float X;
    private float Y;
    private Paint myPaint;
    final String C_DIV = "DIV";
    final String C_TABLE = "Table";
    final String C_IFRAME = "iFrame";
    final String C_LIST = "List";
    Canvas canvas;
    Bitmap divimg = BitmapFactory.decodeResource(getResources(), R.drawable.div);
    Bitmap iframeimg = BitmapFactory.decodeResource(getResources(), R.drawable.iframe);
    Bitmap img = null;
    String objectname = " ";

    public Objects(Context context) {
        super(context);
        myPaint = new Paint();
    }

    public void HTMLObjects(String object) {
        Log.d(object, "see");
        if (object.equals(C_DIV)) {
            myPaint.setColor(Color.GREEN);
            img = Bitmap.createBitmap(divimg);
            objectname = "DIV";
        }
        if (object.equals(C_IFRAME)) {
            myPaint.setColor(Color.BLUE);
            img = Bitmap.createBitmap(iframeimg);
            objectname = "iFrame";
        }
    }

    public boolean onTouchEvent(MotionEvent event) {
        int action = event.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                X = (int) event.getX();
                Y = (int) event.getY();
                break;
            case MotionEvent.ACTION_MOVE:
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                X = (int) event.getX();
                Y = (int) event.getY();
                break;
        }
        return true;
    }

    public void onDraw(Canvas canvas) {
        if (img != null) {

            canvas.drawBitmap(img, X - 50, Y - 50, myPaint);
            canvas.drawText(objectname, X, Y - 50, myPaint);
        }
        invalidate();
    }
}

这是我从活动 Workspace 中调用的一个类

ob.HTMLObjects((String) item.getTitle());

我正在发送按下哪个菜单的信息。

当我按下 DIV 或 iFrame 时,我不知道如何制作新的/不同的对象/位图。

4

1 回答 1

3

试试这些版本。我用以下列表替换x, y, img, objectname

public class Objects extends View {
    private Paint myPaint;
    public final String C_DIV="DIV";
    public final String C_TABLE="Table";
    public final String C_IFRAME="iFrame";
    public final String C_LIST="List";
    private Canvas canvas;
    private Bitmap divimg= BitmapFactory.decodeResource(getResources(), R.drawable.div);
    private Bitmap iframeimg=BitmapFactory.decodeResource(getResources(), R.drawable.iframe);
    private List<Bitmap> images;
    private List<Float> xs;
    private List<Float> ys;
    private List<String> objectNames;

    public Objects(Context context) {
        super(context);
        myPaint = new Paint();
        images = new ArrayList<Bitmap>();
        xs = new ArrayList<Float>();
        ys = new ArrayList<Float>();
        objectNames = new ArrayList<String>();
    }

    public void HTMLObjects(String object){
        Log.d(object, "see");
        if (object.equals(C_DIV)) {
            myPaint.setColor(Color.GREEN);
            images.add(Bitmap.createBitmap(divimg));
            objectNames.add("DIV");
            xs.add(0F);
            ys.add(0F);
        }
        if (object.equals(C_IFRAME)){
            myPaint.setColor(Color.BLUE);
            images.add(Bitmap.createBitmap(iframeimg));
            objectNames.add("iFrame");
            xs.add(0F);
            ys.add(0F);
        }
    }
    public boolean onTouchEvent(MotionEvent event) {
        int action=event.getAction();
        if (xs.isEmpty()) {
            return true;
        }
        int pos = xs.size() - 1;
        switch(action) {
            case MotionEvent.ACTION_DOWN:
                xs.set(pos, event.getX());
                ys.set(pos, event.getY());
                break;
            case MotionEvent.ACTION_MOVE:
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                xs.set(pos, event.getX());
                ys.set(pos, event.getY());
                break;
        }
        return true;
    }
    public void onDraw(Canvas canvas){
        for (int i = 0; i < images.size(); i++) {
            float x = xs.get(i);
            float y = ys.get(i);
            canvas.drawBitmap(images.get(i), x-50, y-50, myPaint);
            canvas.drawText(objectNames.get(i), x, y-50, myPaint);
        }
        invalidate();
    }
}
于 2012-11-26T13:12:04.980 回答