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

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

縮放圖像以填充ImageView寬度并保持寬高比

縮放圖像以填充ImageView寬度并保持寬高比

躍然一笑 2019-11-05 14:51:40
我有一個(gè)GridView。的數(shù)據(jù)GridView是來自服務(wù)器的請(qǐng)求。這是中的項(xiàng)目布局GridView:<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:background="@drawable/analysis_micon_bg"    android:gravity="center_horizontal"    android:orientation="vertical"    android:paddingBottom="@dimen/half_activity_vertical_margin"    android:paddingLeft="@dimen/half_activity_horizontal_margin"    android:paddingRight="@dimen/half_activity_horizontal_margin"    android:paddingTop="@dimen/half_activity_vertical_margin" >    <ImageView        android:id="@+id/ranking_prod_pic"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:adjustViewBounds="true"        android:contentDescription="@string/app_name"        android:scaleType="centerCrop" />    <TextView        android:id="@+id/ranking_rank_num"        android:layout_width="wrap_content"        android:layout_height="wrap_content" />    <TextView        android:id="@+id/ranking_prod_num"        android:layout_width="wrap_content"        android:layout_height="wrap_content" />    <TextView        android:id="@+id/ranking_prod_name"        android:layout_width="wrap_content"        android:layout_height="wrap_content" /></LinearLayout>我從服務(wù)器請(qǐng)求數(shù)據(jù),獲取圖像網(wǎng)址并將圖像加載到 Bitmappublic static Bitmap loadBitmapFromInputStream(InputStream is) {    return BitmapFactory.decodeStream(is);}public static Bitmap loadBitmapFromHttpUrl(String url) {    try {        return loadBitmapFromInputStream((InputStream) (new URL(url).getContent()));    } catch (Exception e) {        Log.e(TAG, e.getMessage());        return null;    }}而且getView(int position, View convertView, ViewGroup parent)適配器中有方法的代碼Bitmap bitmap = BitmapUtil.loadBitmapFromHttpUrl(product.getHttpUrl());prodImg.setImageBitmap(bitmap);圖片大小為210*210。我在Nexus 4上運(yùn)行我的應(yīng)用程序。圖像可以填充ImageView寬度,但I(xiàn)mageView高度不能縮放。ImageView不顯示整個(gè)圖像。我該如何解決這個(gè)問題?
查看完整描述

3 回答

?
RISEBY

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

不使用任何自定義類或庫:


<ImageView

    android:id="@id/img"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:adjustViewBounds="true"

    android:scaleType="fitCenter" />

scaleType="fitCenter" (省略時(shí)為默認(rèn))


將使其寬度達(dá)到父級(jí)允許的范圍,并根據(jù)需要向上/向下縮放以保持寬高比。

scaleType="centerInside"


如果的固有寬度src小于父級(jí)寬度,

則會(huì)使圖像水平居中

如果的固有寬度src大于父級(jí)寬度,

則會(huì)使其達(dá)到父級(jí)允許的寬度,并縮小比例并保持寬高比。

不管您使用android:src還是ImageView.setImage*方法,密鑰都可能是adjustViewBounds。


查看完整回答
反對(duì) 回復(fù) 2019-11-05
?
白衣非少年

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

我曾經(jīng)有過類似的問題。我通過制作自定義ImageView解決了它。


public class CustomImageView extends ImageView

然后覆蓋imageview的onMeasure方法。我相信我做了這樣的事情:


    @Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

    try {

        Drawable drawable = getDrawable();


        if (drawable == null) {

            setMeasuredDimension(0, 0);

        } else {

            float imageSideRatio = (float)drawable.getIntrinsicWidth() / (float)drawable.getIntrinsicHeight();

            float viewSideRatio = (float)MeasureSpec.getSize(widthMeasureSpec) / (float)MeasureSpec.getSize(heightMeasureSpec);

            if (imageSideRatio >= viewSideRatio) {

                // Image is wider than the display (ratio)

                int width = MeasureSpec.getSize(widthMeasureSpec);

                int height = (int)(width / imageSideRatio);

                setMeasuredDimension(width, height);

            } else {

                // Image is taller than the display (ratio)

                int height = MeasureSpec.getSize(heightMeasureSpec);

                int width = (int)(height * imageSideRatio);

                setMeasuredDimension(width, height);

            }

        }

    } catch (Exception e) {

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    }

這將拉伸圖像以適合屏幕,同時(shí)保持寬高比。


查看完整回答
反對(duì) 回復(fù) 2019-11-05
  • 3 回答
  • 0 關(guān)注
  • 865 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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