【问题标题】:Android Drawing bitmap and button on canvas by X Y PositionsAndroid 通过 X Y 位置在画布上绘制位图和按钮
【发布时间】:2012-11-30 07:13:02
【问题描述】:

我是 android 新手,我的应用程序有一些问题。任何帮助将不胜感激:)

我通过 xpositon 在画布上绘制位图,并通过传感器绘制位图(当我移动手机时,位图正在移动)

我需要在我的位图中心创建移动的可点击按钮,当我点击它们时,我将进入下一个活动。

位图绘制完美,但不知道如何在其上绘制按钮,这些按钮将随着我的位图移动相同的 x 和 y 值。我该怎么做?

这是我的一段代码:

    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
};

和 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>

【问题讨论】:

    标签: android bitmap android-canvas draw imagebutton


    【解决方案1】:

    我会把你的

    CustomDrawableView
    

    FrameLayout

    并将按钮添加到FrameLayout,然后将按钮放置在setX() setY() 或者如果您的应用需要运行 API 级别 11 之前的版本,则留有余量

    类似这样的:

    <?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>
    

    现在您的 Button 将添加到 CustomDrawableView 上,您可以像使用普通 Button 一样使用它。

    【讨论】:

    • 它不工作你能看看我的代码,我做错了什么?
    • 什么不起作用?如果按钮未显示,则在 CustomView 之后添加您的按钮,否则按钮位于视图后面并且您看不到它。
    • 不客气。如果它有效,您是否介意接受正确的答案。这也会提高您的接受率,让人们更有可能帮助您。
    【解决方案2】:

    好的,感谢“gabe”和这个——link,它终于可以正常工作了。您始终可以在绝对布局中移动整个相对布局 - Link

    在我的类变量中我添加:

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

    关于更新功能:

    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);
    

    还有 XML 文件:

    <?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>
    

    感谢您的帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-13
      • 2012-11-01
      相关资源
      最近更新 更多