Android Drawing bitmap and button on canvas by X Y Positions

落花浮王杯 提交于 2019-11-29 12:51:47

I would put your

CustomDrawableView

in a FrameLayout

and add the buttons to the FrameLayout then position your button with setX() setY() or with a margin if your app needs to run pre API Level 11

Something like this:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:gravity="center_horizontal"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent">

    <CustomDrawableView android:layout_height="fill_parent"
               android:layout_width="fill_parent"
              />

    <Button

            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="@string/myButtonText"
            android:gravity="center"
            />

</FrameLayout>

now your Button will get added over the CustomDrawableView and you can use it like a normal Button.

Dexter00

Ok, its finally working great thanks to "gabe" and this - link. You can always move whole relative layout inside absolute layout - Link

in my class variables i add :

    Button button;
    private int myNewX = 0;
    private int myNewY = 0;

on update function:

myNewX = (int)xAcceleration;
myNewY = (int)yAcceleration;

Button button = (Button)findViewById(R.id.button);
            AbsoluteLayout.LayoutParams absParams = 
                (AbsoluteLayout.LayoutParams)button.getLayoutParams();
            absParams.x = myNewX;
            absParams.y = myNewY;
            button.setLayoutParams(absParams);

And XML File:

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >

    <FrameLayout
        android:id="@+id/preview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal"
        android:visibility="visible" >


    </FrameLayout>



    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:contentDescription="@string/desc"
        android:orientation="horizontal"
        android:scaleType="centerCrop"
        android:src="@drawable/vinetka" />


     <Button
      android:id="@+id/button"
      android:layout_width="188dp"
        android:layout_height="wrap_content"
        android:text="Button"
        android:layout_x="130px"
        android:layout_y="390px"


/>

</AbsoluteLayout>

THANKS FOR HELP!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!