0

Hi I am a newbie in android. My question is is there any function to display an image from drawable into screen. By the way I am able to move the same image on the co-ordinates I touch, I just need the new image to be placed on the co-ordinate i touch.

Here's my code:

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;
}
Sam Bellerose
  • 1,782
  • 2
  • 18
  • 43
  • First set drawable dimensions and position by calling `setBounds` on it and later draw by calling `draw(Canvas c)` (also a method of `Drawable`) with correct canvas (you can get some from `onDraw` method of any appropriate `View`) passed as argument. – weaknespase Dec 14 '15 at 18:20
  • Ohh can you edit from my code above? – Siddharth Prabhu Dec 14 '15 at 18:26
  • In code above, you are trying to move ImageView by calling `layout` method. It is correct only if you are writing custom `ViewGroup`. Since you use View subclass - look at how different views placed on different layouts in Android. For example: http://www.google.com/search?q=Layouts+in+android, and if you are trying to place element absolutely, try `FrameLayout`. – weaknespase Dec 14 '15 at 18:39

1 Answers1

0

Assuming you placed your image inside 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);
}

Also, width and height could be set to WRAP_CONTENT at design time to tell element to set its size to size of contents.


Adding a new image could be done by creating new 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);
}

Where frameLayout is reference to FrameLayout view group you placed in your layout and R.drawable.example_image is an image you want to show.

weaknespase
  • 1,014
  • 8
  • 15
  • Oh my actual question is that when i touch an image should appear and that image should not move and if again touch on another co-ordinate same image should appear but the previous image should retain. hope you can understand my question. Sorry if my English is bad – Siddharth Prabhu Dec 14 '15 at 18:58