繁体   English   中英

如何在Android中以编程方式获取图像按钮的宽度和高度

[英]How to programmatically get an Image Button's width and height in Android

我试图以编程方式在Android中获取图像按钮的宽度和高度的整数变量。 我要这样做是因为图像的大小会因设备而异。 我怎样才能做到这一点? 我已经尝试过了

width = Image.getWidth(); height = image.getHeight();

但是它返回零? API 22+中不推荐使用BitMap。 同样,获取实际图像的大小也将不起作用,因为我略微扭曲了图像以适应我的需求。 所以drawable.getIntrinsicHeight(); 不在桌子上(是的,我知道您必须先声明一个drawable,然后才说drawable.getIntrinsicHeight但IDC)。 我很混乱。 请帮忙!

完整代码:

package com.example.e99900004533.candycollector;

//imports for android, and Random
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.graphics.Point;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Display;
import android.widget.EditText;
import android.widget.ImageButton;
import android.os.CountDownTimer;
import android.graphics.drawable.Drawable;
import java.util.Random;

public class MainActivity extends ActionBarActivity {

    //Original global variables. Non-static.
    public EditText collectedTextEdit;
    public EditText timerTextEdit;
    public ImageButton candyEdit;
    public int screenWidth;
    public int screenHeight;
    public int candyX;
    public int candyY;
    public int collected = 0;
    public long time;
    public boolean candyBag = false;
    public String currentCandy = "candy";
    public boolean running = true;
    public int candyWidth;
    public int candyHeight;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        collectedTextEdit = (EditText) findViewById(R.id.collectedText);
        timerTextEdit = (EditText) findViewById(R.id.timerText);
        candyEdit = (ImageButton) findViewById(R.id.candy);
        candyEdit.setBackgroundResource(R.drawable.candy);
        collectedTextEdit.setText("Collected: " + collected);
        timerTextEdit.setText("Time: ");

        //Sets timer to 30 seconds. Displays time. Ends game when time runs out.
        new CountDownTimer(30000, 1000) {

            public void onTick(long millisUntilFinished) {
                time = millisUntilFinished / 1000;
                timerTextEdit.setText("Time: " + time);
            }

            public void onFinish() {
                //When game timer is complete.
                timerTextEdit.setText("GAME OVER!!!");
                collectedTextEdit.setText("Score: " + collected);
                timerTextEdit.setX(screenWidth / 2); //Sets to middle of screen. Directly above score.
                timerTextEdit.setY(screenHeight / 2);
                collectedTextEdit.setX(screenWidth / 2); //Sets to middle of screen. Directly below score.
                collectedTextEdit.setY(screenHeight / 3);
                running = false;
                candyEdit.setVisibility(View.INVISIBLE); //Makes candy Image invisible.
            }
        }.start();

        //Gets screen width and height. Sets to two already created variables.
        Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        screenWidth = size.x;
        screenHeight = size.y;

        candyWidth = candyEdit.getMeasuredWidthAndState(); //PROBLEM HERE!!
        candyHeight = candyEdit.getMeasuredHeightAndState(); //PROBLEM HERE!!

        addListenerOnButton();
    }

    //Onclick for ImageButton candy.
    public void addListenerOnButton() {

        candyEdit.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (running) {
                    //Adds to collected and changes text.
                    collected += 1;
                    collectedTextEdit.setText("Collected: " + collected);

                    //Checks if CandyBag. Then resets.
                    if (candyBag) {
                        candyBag = false;
                        if (currentCandy.equals("candy")) {
                            candyEdit.setBackgroundResource(R.drawable.candy);
                        }
                        if (currentCandy.equals("candyTwo")) {
                            candyEdit.setBackgroundResource(R.drawable.candy2);
                        }
                        if (currentCandy.equals("candyThree")) {
                            candyEdit.setBackgroundResource(R.drawable.candy3);
                        }
                    }

                    //Gets new candy images at certain score.
                    if (collected >= 15 && collected < 30) {
                        candyEdit.setBackgroundResource(R.drawable.candy2);
                        currentCandy = "candyTwo";
                    }
                    if (collected >= 30) {
                        candyEdit.setBackgroundResource(R.drawable.candy3);
                        currentCandy = "candyThree";
                    }

                    //sets candy X and Y variables.
                    Random random = new Random();
                    candyX = random.nextInt(screenWidth - candyWidth * 2); //minus height and width of image.
                    candyY = random.nextInt(screenHeight - candyHeight * 2);
                    candyX += candyWidth;
                    candyY += candyHeight; //Plus extra to keep on screen.

                    //Sets candyBag if random = 1. 1 / x chance.
                    int candyRandom = random.nextInt(10);
                    if (candyRandom == 1) {
                        candyEdit.setBackgroundResource(R.drawable.candybag);
                        candyBag = true;
                        collected += 2;
                    }

                    //Makes sure candy is not off screen. Replaces if so.
                    while (candyX >= screenWidth - candyWidth || candyY >= screenHeight - candyHeight) {
                        if (candyX >= screenWidth || candyY >= screenHeight) {
                            candyX = random.nextInt(screenWidth - candyWidth * 2);
                            candyY = random.nextInt(screenHeight - candyHeight * 2);
                            candyX += candyWidth;
                            candyY += candyHeight;
                        }
                    }

                    //Sets candy X and Y.
                    System.out.println(candyX + " : " + candyY);
                    System.out.println((screenWidth - candyWidth * 2) + " : " + (screenHeight - candyHeight * 2));
                    System.out.println(candyWidth + " : " + candyHeight);
                    candyEdit.setX(candyX);
                    candyEdit.setY(candyY);
                }
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
    android:background="@drawable/background">

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/collectedText"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:inputType="none"
        android:text="@string/collectedText"
        android:textColor="#ffffff"
        android:textSize="25sp"
        android:textStyle="bold"
        android:singleLine="true"
        android:clickable="false"
        android:textIsSelectable="false"
        android:editable="false" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/timerText"
        android:layout_alignTop="@+id/collectedText"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:inputType="none"
        android:text="@string/collectedText"
        android:textColor="#ffffff"
        android:textSize="25sp"
        android:textStyle="bold"
        android:singleLine="true"
        android:clickable="false"
        android:textIsSelectable="false"
        android:editable="false" />

    <ImageButton
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:id="@+id/candy"
        android:background="@drawable/candy"
        android:contentDescription="@string/candyImg"
        android:clickable="true" />

</RelativeLayout>

可能是您调用这些方法为时过早。 在重写的run()方法imageButton.post(new Runnable..)与getWidth和getHeight一起使用。

前段时间有同样的问题。 这是因为过早调用width和height方法会导致它们为0,因为视图未正确初始化/测量。

您正在尝试在呈现布局之前测量尺寸,这是在onCreate方法期间发生的,因此该方法需要完成。 解决此问题的最简单方法是创建一个可运行的对象,该对象将在onCreate方法完成后执行代码:

    myButton = (ImageButton) findViewById(R.id.YourButton);
    myButton.post(new Runnable() {
        @Override
        public void run() {
            int w = myButton.getMeasuredWidthandState();
            int h = myButton.getMeasuredHeightandState();

            ...
        }

});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM