ImageView

默认

图片是放在ImageView控件里边的
ImageView

<ImageView
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:background="#ffff33"
        android:src="@drawable/img">
</ImageView>

fitXY

ImageView
scaleType=“fitXY”
拉伸图片使得图片占据整个控件

<ImageView
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:background="#ffff33"
        android:src="@drawable/img"
        android:scaleType="fitXY">
    </ImageView>

fitCenter

保持图片原有宽高比,成比例拉伸,直到长或宽到达边界(貌似和默认的效果一样)

ImageView

<ImageView
        android:id="@+id/iv_2"
        android:layout_below="@+id/iv_1"
        android:layout_marginTop="20dp"
        android:layout_width="400dp"
        android:layout_height="200dp"
        android:background="#ffff33"
        android:src="@drawable/img"
        android:scaleType="fitCenter">
</ImageView>

centerCrop

保持宽高比一直扩大,直到全部覆盖,超过的部分被裁减
ImageView

<ImageView
        android:id="@+id/iv_2"
        android:layout_below="@+id/iv_1"
        android:layout_marginTop="20dp"
        android:layout_width="400dp"
        android:layout_height="200dp"
        android:background="#ffff33"
        android:src="@drawable/img"
        android:scaleType="centerCrop">
    </ImageView>

网络图片

Glide文档

repositories {
  google()
  mavenCentral()
}

dependencies {
  implementation 'com.github.bumptech.glide:glide:4.12.0'
  annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0'
}

ImageView
如果有repositories、dependencies块就将里边两行代码加到块里边,不然就直接复制进去

repositories {
    google()
    mavenCentral()
}
dependencies {

    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'com.google.android.material:material:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.navigation:navigation-fragment:2.2.2'
    implementation 'androidx.navigation:navigation-ui:2.2.2'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    
    implementation 'com.github.bumptech.glide:glide:4.12.0'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0'
}

监听设置网络图片

public class ImageViewActivity extends AppCompatActivity {
    private ImageView iv1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_view);
        iv1=findViewById(R.id.iv_1);
        Glide.with(this).load("https://www.baidu.com/i00mg/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png").into(iv1);
    }
}

除此之外,还不能显示出来,没有网络权限
Permission denied (missing INTERNET permission?)
AndroidManifest.xml里面加权限
ImageView

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

SUCCESS
ImageView

上一篇:【阅读笔记】Implementation of tactical maneuvers with maneuver libraries


下一篇:【Android】使用阿里云直播实现手机直播功能