2015-12-14 77 views
0

嗨,我是Android的新手。我的問題是,是否有任何功能來顯示圖像可繪製到屏幕上。順便說一下,我可以在我觸摸的座標上移動相同的圖像,我只需要將新圖像放置在我觸摸的座標上。用戶觸摸的新圖像

這裏是我的代碼:

public class MainActivity extends Activity { 



@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

} 

    private void placeImage(float X, float Y) { 
     ImageView flower = (ImageView) findViewById(R.id.img); 

     int touchX = (int) X; 
     int touchY = (int) Y; 


     flower.layout(touchX, touchY, touchX + 48, touchY + 48); 


     int viewWidth = flower.getWidth(); 
     int viewHeight = flower.getHeight(); 
     viewWidth = viewWidth/2; 
     viewHeight = viewHeight/2; 

     flower.layout(touchX - viewWidth, touchY - viewHeight, touchX + viewWidth, touchY + viewHeight); 
    } 



public boolean onTouchEvent(MotionEvent event) { 
    super.onTouchEvent(event); 
    int eventAction = event.getAction(); 
    switch (eventAction) { 
     case MotionEvent.ACTION_DOWN: 
      float TouchX = event.getX(); 
      float TouchY = event.getY(); 
      placeImage(TouchX, TouchY); 
      break; 
    } 
    return true; 
} 
+0

首先通過調用'setBounds'來設置可繪製的尺寸和位置,然後通過調用'draw(Canvas c)'(也是'Drawable'的方法)用正確的畫布進行繪製(你可以從'onDraw'方法獲得一些任何適當的'View')作爲參數傳遞。 – nekavally

+0

哦,你可以編輯我的代碼嗎? –

+0

在上面的代碼中,您正嘗試通過調用'layout'方法來移動ImageView。只有在編寫自定義的'ViewGroup'時纔是正確的。由於您使用了View子類 - 請查看Android中不同佈局的不同視圖。例如:http://www.google.com/search?q=Layouts+in+android,如果您嘗試將元素完全放置,請嘗試使用「FrameLayout」。 – nekavally

回答

0

假設你放在裏面FrameLayout你的形象:

private void placeImage(float X, float Y) { 
    ImageView flower = (ImageView) findViewById(R.id.img); 

    int touchX = (int) X; 
    int touchY = (int) Y; 

    FrameLayout.LayoutParams mlp = (FrameLayout.LayoutParams) flower.getLayoutParams(); 
    mlp.marginLeft = touchX; 
    mlp.marginTop = touchY; 
    mlp.width = FrameLayout.LayoutParams.WRAP_CONTENT; 
    mlp.height = FrameLayout.LayoutParams.WRAP_CONTENT; 
    flower.setLayoutParams(mlp); 
} 

而且,寬度和高度可以在設計時設置爲WRAP_CONTENT告訴元素設置其大小與內容的大小。


添加新圖像可以通過創建新的ImageView來完成。

protected void onCreate(Bundle savedInstanceState){ 
    ... 
    frameLayout = (FrameLayout) findViewById(R.id.box); 
    ... 
} 

private void placeImage(float X, float Y) { 
    ImageView newImage = new ImageView(this); 
    //Set contents of new ImageView 
    newImage.setImage(R.drawable.example_image); 
    //Create layout parameters object with width and height set to size of contents 
    FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT); 
    //Offset image by X and Y from left and top of its parent, in our case FrameLayout 
    lp.marginLeft = X; 
    lp.marginTop = Y; 
    //Add new ImageView into layout 
    frameLayout.AddView(newImage, lp); 
} 

哪裏frameLayout是參考你放置在你的佈局和R.drawable.example_imageFrameLayout視圖組是要顯示的圖像。

+0

哦,我的實際問題是,當我觸摸圖像應該出現,該圖像不應該移動,如果再次觸摸另一個座標相同的圖像應該出現,但應保留以前的圖像。希望你能理解我的問題。對不起,如果我的英語不好 –