第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

Android:如何在保持寬高比的同時(shí)將圖像拉伸到屏幕寬度?

Android:如何在保持寬高比的同時(shí)將圖像拉伸到屏幕寬度?

溫溫醬 2019-12-10 13:08:57
我想下載一個(gè)圖像(大小不明,但始終大致為正方形)并顯示它,以便它在任何屏幕尺寸上都水平填充屏幕,并垂直拉伸以保持圖像的縱橫比。這是我的(無(wú)效)代碼。它水平拉伸圖像,但不垂直拉伸圖像,因此將其壓扁...ImageView mainImageView = new ImageView(context);    mainImageView.setImageBitmap(mainImage); //downloaded from server    mainImageView.setScaleType(ScaleType.FIT_XY);    //mainImageView.setAdjustViewBounds(true);     //with this line enabled, just scales image down    addView(mainImageView,new LinearLayout.LayoutParams(             LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
查看完整描述

3 回答

?
隔江千里

TA貢獻(xiàn)1906條經(jīng)驗(yàn) 獲得超10個(gè)贊

我使用自定義視圖完成了此操作。設(shè)置layout_width =“ fill_parent”和layout_height =“ wrap_content”,并將其指向適當(dāng)?shù)目衫L制對(duì)象:


public class Banner extends View {


  private final Drawable logo;


  public Banner(Context context) {

    super(context);

    logo = context.getResources().getDrawable(R.drawable.banner);

    setBackgroundDrawable(logo);

  }


  public Banner(Context context, AttributeSet attrs) {

    super(context, attrs);

    logo = context.getResources().getDrawable(R.drawable.banner);

    setBackgroundDrawable(logo);

  }


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

    super(context, attrs, defStyle);

    logo = context.getResources().getDrawable(R.drawable.banner);

    setBackgroundDrawable(logo);

  }


  @Override protected void onMeasure(int widthMeasureSpec,

      int heightMeasureSpec) {

    int width = MeasureSpec.getSize(widthMeasureSpec);

    int height = width * logo.getIntrinsicHeight() / logo.getIntrinsicWidth();

    setMeasuredDimension(width, height);

  }

}



查看完整回答
反對(duì) 回復(fù) 2019-12-11
?
青春有我

TA貢獻(xiàn)1784條經(jīng)驗(yàn) 獲得超8個(gè)贊

最后,我手動(dòng)生成了尺寸,效果很好:


DisplayMetrics dm = new DisplayMetrics();

context.getWindowManager().getDefaultDisplay().getMetrics(dm);

int width = dm.widthPixels;

int height = width * mainImage.getHeight() / mainImage.getWidth(); //mainImage is the Bitmap I'm drawing

addView(mainImageView,new LinearLayout.LayoutParams( 

        width, height));



查看完整回答
反對(duì) 回復(fù) 2019-12-11
  • 3 回答
  • 0 關(guān)注
  • 439 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)