亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

ImageView是具有動態寬度的正方形嗎?

ImageView是具有動態寬度的正方形嗎?

慕妹3146593 2019-11-28 13:58:34
我有一個帶有ImageViews的GridView。我每行有3個。我可以使用WRAP_CONTENT和scaleType = CENTER_CROP正確設置寬度,但是我不知道如何將ImageView的大小設置為正方形。這是我到目前為止所做的,除了高度,它是“靜態的”,似乎還可以:imageView = new ImageView(context);     imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);imageView.setLayoutParams(new GridView.LayoutParams(GridView.LayoutParams.WRAP_CONTENT, 300));我正在適配器內做。
查看完整描述

3 回答

?
搖曳的薔薇

TA貢獻1793條經驗 獲得超6個贊

最好的選擇是繼承ImageView自己的子類,以覆蓋度量傳遞:


public class SquareImageView  extends ImageView {


  ...


  @Override

  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

    super.onMeasure(widthMeasureSpec, heightMeasureSpec);


    int width = getMeasuredWidth();

    setMeasuredDimension(width, width);

  }


  ...


}


查看完整回答
反對 回復 2019-11-28
?
九州編程

TA貢獻1785條經驗 獲得超4個贊

另一個答案很好。這只是bertucci解決方案的擴展,以使ImageView相對于xml膨脹版式具有正方形的寬度和高度。


創建一個類,說一個SquareImageView這樣擴展ImageView,


public class SquareImageView extends ImageView {


    public SquareImageView(Context context) {

        super(context);

    }


    public SquareImageView(Context context, AttributeSet attrs) {

        super(context, attrs);

    }


    public SquareImageView(Context context, AttributeSet attrs, int defStyle) {

        super(context, attrs, defStyle);

    }


    @Override

    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);


        int width = getMeasuredWidth();

        setMeasuredDimension(width, width);

    }


}

現在,在您的xml中執行此操作,


        <com.packagepath.tothis.SquareImageView

            android:id="@+id/Imageview"

            android:layout_width="fill_parent"

            android:layout_height="fill_parent" />

如果您需要不在程序中動態創建ImageView而不是在xml中進行固定,那么此實現將非常有用。


查看完整回答
反對 回復 2019-11-28
?
莫回無

TA貢獻1865條經驗 獲得超7個贊

前面的幾個答案就足夠了。我只是在這里為@Andro Selva和@ a.bertucci的解決方案添加一個小的優化:


這是一個很小的優化,但是檢查寬度和高度是否不同可以防止再次進行測量。


public class SquareImageView extends ImageView {


    public SquareImageView(Context context) {

        super(context);

    }


    public SquareImageView(Context context, AttributeSet attrs) {

        super(context, attrs);

    }


    public SquareImageView(Context context, AttributeSet attrs, int defStyle) {

        super(context, attrs, defStyle);

    }


    @Override

    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        super.onMeasure(widthMeasureSpec, widthMeasureSpec);


        int width = getMeasuredWidth();

        int height = getMeasuredHeight();


        // Optimization so we don't measure twice unless we need to

        if (width != height) {

            setMeasuredDimension(width, width);

        }

    }


}


查看完整回答
反對 回復 2019-11-28
  • 3 回答
  • 0 關注
  • 801 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號