2012-11-26 37 views
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(); 
    } 
} 

这是我从活动工作区调用带有

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

,我送它压的菜单信息的类。

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

+2

看来你知道如何使用'BitmapFactory.decodeResource'资源创建'Bitmap'。我不完全明白你想要什么。 –

+0

我想复制位图。当我按Meni DIV时,我需要新的位图,旧的位图将保留在我所居住的位置,并且我想创建新的位图,我将把它放在新的位置上。对不起,我的英语。 –

+0

看来你已经有了这样的代码。它不工作吗? –

回答

3

试试这些版本。我代替xyimgobjectname与根据列表:

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(); 
    } 
} 
+0

这是工作,非常感谢你。忍受我:) –

+0

@IstarEnki接受答案,如果它帮助你:) –