Android的绘图位图和按钮,在画布上用XY位置位图、画布、按钮、位置

2023-09-04 23:15:33 作者:杀生予夺

我是新进Android和我有一些问题,我的应用程序。任何帮助将AP preciated:)

我已经绘制位图在画布上用xpositon和ypostion由传感器(当我移动我的手机,位图移动)

我要创造感动在我的位图可点击中心按钮,当我点击他们,我会去到下一个活动。

位图绘制完美,但idont知道如何画按钮在他们身上,将我的位图由x和y的值相同是移动的按钮。我怎样才能做到这一点?

这是我的一块code:

 私人无效updateBall(){

        //计算新速度
        // xVe​​locity + =(xAcceleration * frameTime的);
        // yVelocity + =(yAcceleration * frameTime的);

        在那段时间// Calc的行驶距离
        //浮动XS =(xVelocity / 2)* frameTime的;
        //浮动YS =(yVelocity / 2)* frameTime的;

        //添加到位置,由于传感器的负
        //读数是相反我们想要的!
        // xPosition位置 -  = XS;

        // ------------------------------- MOVINGÿ--------------- -----------------------------------

        通过=(-IMG_H + DDY)/ 2F;
        AY =由/ 90F;

        浮DY =(AY * zAcceleration +的);

        如果(Math.abs(zAcceleration)所述; 90&安培;&安培; Math.abs(yAcceleration)→100)
            yPosition  -  =(yPosition  -  DY)/ 20;
        否则如果(zAcceleration大于0){
            如果(yPosition> -IMG_H){
                如果(Math.abs(yAcceleration)→100){
                    DY =(AY * 90 +的);
                    yPosition  -  =(yPosition  -  DY)/ 20;
                } 其他 {
                    yPosition = -IMG_H;
                }
            }

        } 其他 {
            如果(yPosition< IMG_H){
                如果(Math.abs(yAcceleration)→100){
                    DY =(-ay * 90 +的);
                    yPosition  -  =(yPosition  -  DY)/ 20;
                } 其他 {
                    yPosition = DDY;
                }
            }
        }

        // y模块
        yPosition = 0;

        // -------------------------------移动式x --------------- -----------------------------------

        //对于理解:)
        // DDX = 1280-(浮点)display.getWidth();

        AX = -IMG_W /(180°F); // -2 * XSTART);
        BX = -ax * XSTART;

        // XSTART pozycjapoczątkowa

        如果(xAcceleration> XSTART和放大器;&安培; xAcceleration< = 180 + XSTART){
            DX =(AX * xAcceleration + BX);
            如果(DIRR == FALSE){
                xPosition位置= DX;
                DIRR =真;
            }

            xPosition位置 -  =(xPosition位置 -  DX)/ 20;
            xPosition1 = xPosition位置+ IMG_W;
        } 其他 {
            DX1 =(AX *(xAcceleration  -  180°F)+ BX);
            如果(DIRR ==真){
                xPosition1 = DX1;
                DIRR = FALSE;
            }

            xPosition1  -  =(xPosition1  -  DX1)/ 20;
            xPosition位置= xPosition1 + IMG_W;

        }

        xPosition位置= 0;
        xPosition1 = 0;
        // xPosition位置= yPosition = 150;
        / *
         *如果(xPosition位置> XMAX){xPosition位置= XMAX; }否则如果(xPosition位置℃,){
         * xPosition位置= 0; }
         * /
        / *
         *如果(yPosition> YMAX){yPosition = YMAX; }否则如果(yPosition℃,){
         * yPosition = 0; }
         * /

        //打印值
        Log.i(QLA,×:+ xAcceleration +的dx:+ DX +的dx1:+ DX1 +XPOS:+ xPosition位置++xpos1:+ xPosition1);

    }

    //我选择不实现此方法
    公共无效onAccuracyChanged(传感器为arg0,INT ARG1){
        // TODO自动生成方法存根
    }

    @覆盖
    保护无效onResume(){
        super.onResume();
        sensorManager.registerListener(这一点,
                sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION)
                SensorManager.SENSOR_DELAY_GAME);
    }

    @覆盖
    保护无效的onStop(){
        //注销监听器
        sensorManager.unregisterListener(本);
        super.onStop();
    }

    公共类CustomDrawableView扩展视图{
        公共CustomDrawableView(上下文的背景下){
            超(上下文);
            位图球= BitmapFactory.de codeResource(getResources()
                    R.drawable.test);
            最终诠释dstWidth = 1280;
            最终诠释dstHeight = 960;
            mBitmap = Bitmap.createScaledBitmap(球,dstWidth,dstHeight,真正的);

            位图球1 = BitmapFactory.de codeResource(getResources()
                    R.drawable.tt);
            最终诠释dstWidth1 = 1280;
            最终诠释dstHeight1 = 960;
            mBitmap1 = Bitmap.createScaledBitmap(球1,dstWidth1,dstHeight1,真正的);
            // mWood = BitmapFactory.de codeResource(getResources()
            // R.drawable.wood);

            / *
             *的ImageButton明星=(的ImageButton)findViewById(R.id.imageButton1);
             * star.setOnClickListener(新View.OnClickListener(){公共无效
             *的onClick(视图查看){意向myIntent =新
             *意图(view.getContext(),Main.class);
             * startActivityForResult(myIntent,0);
             * overridePendingTransition(R.anim.fade_in,R.anim.fade_out); }
             *
             *});
             * /
            // ImageView的DDSS = star.getDrawable();

        }

        保护无效的OnDraw(帆布油画){
            最后的位图位= mBitmap;
            // canvas.drawBitmap(mWood,0,0,NULL);
            canvas.drawBitmap(位图,xPosition位置,yPosition,NULL);

            // ss.getChildAt(0).setX();

            最后的位图bitmap1 = mBitmap1;
            // canvas.drawBitmap(mWood,0,0,NULL);
            canvas.drawBitmap(bitmap1,xPosition1,yPosition,NULL);

            无效();

        }
    }

//类END
};
 

和XML:

 < XML版本=1.0编码=UTF-8&GT?;
< AbsoluteLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
  机器人:方向=横向
  机器人:layout_width =FILL_PARENT
  机器人:layout_height =FILL_PARENT
  机器人:ID =@ + ID /布局>


  <的FrameLayout机器人:ID =@ + ID / preVIEW
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:能见度=看见
    机器人:方向=横向
    >

   <按钮

            机器人:layout_height =WRAP_CONTENT
            机器人:layout_width =WRAP_CONTENT
            机器人:文本=sdgvgvgfcvcv
            机器人:重力=中心
            />

  < /的FrameLayout>


< ImageView的
          机器人:ID =@ + ID / imageView1
          机器人:layout_width =FILL_PARENT
          机器人:layout_height =WRAP_CONTENT
          机器人:scaleType =centerCrop
          机器人:contentDescription =@字符串/递减
          机器人:adjustViewBounds =真
          机器人:SRC =@可绘制/ vinetka
          机器人:方向=横向/>

< / AbsoluteLayout>
 
如何制作防静电标签

解决方案

好了,它的最后努力加布,这十分感谢 - 的链接。您可以随时将里面绝对布局整体相对布局 - Link

在我的类变量我补充一下:

 按钮按钮;
    私人诠释myNewX = 0;
    私人诠释myNewY = 0;
 

在更新功能:

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

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

和XML文件:

 < XML版本=1.0编码=UTF-8&GT?;
< AbsoluteLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@ + ID /布局
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:方向=横向>

    <的FrameLayout
        机器人:ID =@ + ID / preVIEW
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =FILL_PARENT
        机器人:方向=横向
        机器人:能见度=有形与GT;


    < /的FrameLayout>



    < ImageView的
        机器人:ID =@ + ID / imageView1
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:adjustViewBounds =真
        机器人:contentDescription =@字符串/递减
        机器人:方向=横向
        机器人:scaleType =centerCrop
        机器人:SRC =@可绘制/ vinetka/>


     <按钮
      机器人:ID =@ + ID /按钮
      机器人:layout_width =188dp
        机器人:layout_height =WRAP_CONTENT
        机器人:文本=按钮
        机器人:layout_x =130px
        机器人:layout_y =390px


/>

< / AbsoluteLayout>
 

感谢您的帮助!

i'm new into android and i have some problems with my app. Any help will be appreciated :)

I have drawing bitmap on canvas by xpositon and ypostion by sensors (when i'm moving my phone, bitmaps are moving )

I need to create moving clickable button on center of my bitmaps and when i click them i will go to next activity .

Bitmaps are drawing perfectly but idont know how to draw buttons on them, buttons that will be moving with my bitmap by the same values of x and y. How i can do that?

This is my piece of code:

    private void updateBall() {

        // Calculate new speed
        // xVelocity += (xAcceleration * frameTime);
        // yVelocity += (yAcceleration * frameTime);

        // Calc distance travelled in that time
        // float xS = (xVelocity/2)*frameTime;
        // float yS = (yVelocity/2)*frameTime;

        // Add to position negative due to sensor
        // readings being opposite to what we want!
        // xPosition -= xS;

        // ------------------------------- MOVING Y --------------------------------------------------

        by = (-IMG_H + ddy) / 2f;
        ay = by / 90f;

        float dy = (ay * zAcceleration + by);

        if (Math.abs(zAcceleration) < 90 && Math.abs(yAcceleration) > 100)
            yPosition -= (yPosition - dy) / 20.;
        else if (zAcceleration > 0) {
            if (yPosition > -IMG_H) {
                if (Math.abs(yAcceleration) > 100) {
                    dy = (ay * 90 + by);
                    yPosition -= (yPosition - dy) / 20.;
                } else {
                    yPosition = -IMG_H;
                }
            }

        } else {
            if (yPosition < IMG_H) {
                if (Math.abs(yAcceleration) > 100) {
                    dy = (-ay * 90 + by);
                    yPosition -= (yPosition - dy) / 20.;
                } else {
                    yPosition = ddy;
                }
            }
        }

        // block y
        yPosition = 0;

        // ------------------------------- MOVING X --------------------------------------------------

        // For understanding :)
        // ddx= 1280-(float)display.getWidth();

        ax = -IMG_W / (180f);// -2*xStart);
        bx = -ax * xStart;

        // xStart pozycja początkowa

        if (xAcceleration > xStart && xAcceleration <= 180 + xStart) {
            dx = (ax * xAcceleration + bx);
            if (dirr == false) {
                xPosition = dx;
                dirr = true;
            }

            xPosition -= (xPosition - dx) / 20.;
            xPosition1 = xPosition + IMG_W;
        } else {
            dx1 = (ax * (xAcceleration - 180f) + bx);
            if (dirr == true) {
                xPosition1 = dx1;
                dirr = false;
            }

            xPosition1 -= (xPosition1 - dx1) / 20.;
            xPosition = xPosition1 + IMG_W;

        }

        xPosition = 0;
        xPosition1 = 0;
        // xPosition=yPosition=150;
        /*
         * if (xPosition > xmax) { xPosition = xmax; } else if (xPosition < 0) {
         * xPosition = 0; }
         */
        /*
         * if (yPosition > ymax) { yPosition = ymax; } else if (yPosition < 0) {
         * yPosition = 0; }
         */

        // Print values
        Log.i("QLA", " x: " + xAcceleration + "   dx: " + dx + "    dx1: "+ dx1 + "  xpos: " + xPosition + " " + " xpos1: " + xPosition1);

    }

    // I've chosen to not implement this method
    public void onAccuracyChanged(Sensor arg0, int arg1) {
        // TODO Auto-generated method stub
    }

    @Override
    protected void onResume() {
        super.onResume();
        sensorManager.registerListener(this,
                sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
                SensorManager.SENSOR_DELAY_GAME);
    }

    @Override
    protected void onStop() {
        // Unregister the listener
        sensorManager.unregisterListener(this);
        super.onStop();
    }

    public class CustomDrawableView extends View {
        public CustomDrawableView(Context context) {
            super(context);
            Bitmap ball = BitmapFactory.decodeResource(getResources(),
                    R.drawable.test);
            final int dstWidth = 1280;
            final int dstHeight = 960;
            mBitmap = Bitmap.createScaledBitmap(ball, dstWidth, dstHeight, true);

            Bitmap ball1 = BitmapFactory.decodeResource(getResources(),
                    R.drawable.tt);
            final int dstWidth1 = 1280;
            final int dstHeight1 = 960;
            mBitmap1 = Bitmap.createScaledBitmap(ball1, dstWidth1, dstHeight1, true);
            // mWood = BitmapFactory.decodeResource(getResources(),
            // R.drawable.wood);

            /*
             * ImageButton star = (ImageButton) findViewById(R.id.imageButton1);
             * star.setOnClickListener(new View.OnClickListener() { public void
             * onClick(View view) { Intent myIntent = new
             * Intent(view.getContext(), Main.class);
             * startActivityForResult(myIntent, 0);
             * overridePendingTransition(R.anim.fade_in, R.anim.fade_out); }
             * 
             * });
             */
            // ImageView ddss=star.getDrawable();

        }

        protected void onDraw(Canvas canvas) {
            final Bitmap bitmap = mBitmap;
            // canvas.drawBitmap(mWood, 0, 0, null);
            canvas.drawBitmap(bitmap, xPosition, yPosition, null);

            // ss.getChildAt(0).setX();

            final Bitmap bitmap1 = mBitmap1;
            // canvas.drawBitmap(mWood, 0, 0, null);
            canvas.drawBitmap(bitmap1, xPosition1, yPosition, null);

            invalidate();

        }
    }

// END of class
};

AND XML:

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


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

   <Button

            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="sdgvgvgfcvcv"
            android:gravity="center"
            />

  </FrameLayout>


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

</AbsoluteLayout>

解决方案

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!