2012-11-26 36 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接受答案,如果它幫助你:) –