Java初始化的自定义ImageView大小问题

时间:2019-02-03 15:35:41

标签: android

我正在制作扫雷游戏,必须在屏幕中心显示自定义视图的网格。为了实现这一点,我使用了GridLayout和如下所示的自定义视图(继承ImageView)。

下面提供了情况摘要。

activity_gameplay_screen.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".GameplayScreen">

    <GridLayout
        android:id="@+id/MineGridLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_margin="0dp"
        />
</android.support.constraint.ConstraintLayout>

GameplayScreen.java(活动)

public class GameplayScreen extends AppCompatActivity {

    private MineGridData mineGridData;
    private int curX,curY;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gameplay_screen);

        Intent intent = getIntent();
        int width=intent.getIntExtra(MainActivity.EXTRA_HEIGHT,30);
        int height=intent.getIntExtra(MainActivity.EXTRA_WIDTH,40);
        System.out.println(width);
        System.out.println(height);
        initGridLayout(width,height);
    }

    private void initGridLayout(int width,int height){
        mineGridData = new MineGridData(width,height);
        GridLayout mineGridLayout = findViewById(R.id.MineGridLayout);
        mineGridLayout.setColumnCount(width);
        mineGridLayout.setRowCount(height);
        mineGridLayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        for(int y=0;y< height;++y)
            for (int x = 0; x < width; ++x) {
                GameplaySquare tempSquare = new GameplaySquare(this,null,0,mineGridData.getContent((byte)x,(byte)y));
                GridLayout.Spec xSpec = GridLayout.spec(x,1);
                GridLayout.Spec ySpec = GridLayout.spec(y,1);
                GridLayout.LayoutParams layoutParams = new GridLayout.LayoutParams(xSpec,ySpec);
                mineGridLayout.addView(tempSquare,layoutParams);
            }
        System.out.println(mineGridLayout.getMeasuredHeight());
        System.out.println(mineGridLayout.getHeight());
    }
}

GameplaySquare.java(自定义视图)

public class GameplaySquare extends AppCompatImageView {
    private Drawable[] gameplaySquares = new Drawable[4];
    private byte content; // -1 == MINE
    private final int defaultSize=20;
    private final int unopenedIndex=10;
    private final int flaggedIndex=11;
    private final int hatenaIndex=12;
    private final squareStates defaultState = squareStates.OPEN;
    enum squareStates{
        OPEN, CLOSED, HATENA, FLAGGED
    };
    private squareStates squareState;

    public GameplaySquare(Context context) {
        super(context);
        init(null, 0, -1);
    }

    public GameplaySquare(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs, 0,-1);
    }

    public GameplaySquare(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(attrs, defStyle,-1);
    }

    public GameplaySquare(Context context, AttributeSet attrs, int defStyle, int defaultVal) {
        super(context, attrs, defStyle);
        init(attrs, defStyle, defaultVal);

    }

    private void init(AttributeSet attrs, int defStyle, int defaultValue) {
        // Load attributes

        if(defaultValue==-1) defaultValue=9;
        final TypedArray a = getResources().obtainTypedArray(R.array.GameplaySquare);
        try{
            gameplaySquares[0] = a.getDrawable(defaultValue);
            gameplaySquares[1] = a.getDrawable(unopenedIndex);
            gameplaySquares[2] = a.getDrawable(flaggedIndex);
            gameplaySquares[3] = a.getDrawable(hatenaIndex);
        }finally{
            a.recycle();
        }
        setContent((byte)defaultValue);
        setSquareState(defaultState);
        resizeImage(defaultSize);
    }



    private void resetImage(){
        if(getSquareState()==squareStates.OPEN){
            setImageDrawable(gameplaySquares[0]);
        } else if(getSquareState()==squareStates.CLOSED){
            setImageDrawable(gameplaySquares[1]);
        } else if(getSquareState()==squareStates.FLAGGED){
            setImageDrawable(gameplaySquares[2]);
        } else if(getSquareState()==squareStates.HATENA){
            setImageDrawable(gameplaySquares[3]);
        }
    }
    public void resizeImage(int size){
        setAdjustViewBounds(true);
        setLayoutParams(new ViewGroup.LayoutParams(size,size));
        setScaleType(ScaleType.CENTER_INSIDE);
    }
    public int getContent(){return content;}
    private void setContent(byte s){
        content=s;
    }
    public squareStates getSquareState(){return squareState;}
    public void setSquareState(squareStates s){
        squareState=s;
        resetImage();
    }
}

图像显示出来,但大小不理想(defaultSize = 20)。资源表明使用setLayoutConstraint()应该足够了,但是在这种情况下,这显然是错误的,大概是由于视图是用Java代码初始化的,而不是从xml夸大的。

我该怎么做才能使GameplaySquare的尺寸显示为20?

0 个答案:

没有答案