Android Studio [ImageView/使用第三方庫加載圖片]

ImageViewActivity.classandroid

package com.xdw.a122; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.ImageView; import com.bumptech.glide.Glide; public class ImageViewActivity extends AppCompatActivity { private ImageView mIv4; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_imageview); mIv4=findViewById(R.id.iv_2); Glide.with(this).load("https://www.baidu.com/img/bd_logo1.png?where=super").into(mIv4); } }

activity_imageview.xmlgit

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".ImageViewActivity" android:padding="15dp">

    <ImageView android:id="@+id/iv_1" android:layout_width="match_parent" android:layout_height="200dp" android:src="@drawable/back_1" android:scaleType="fitCenter"/>
    <ImageView android:id="@+id/iv_2" android:layout_width="match_parent" android:layout_height="200dp" android:scaleType="fitCenter" android:background="#000000" android:layout_below="@id/iv_1" android:layout_marginTop="20dp"/>
</RelativeLayout>

並在build.gradle中添加github

repositories {
mavenCentral()
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.github.bumptech.glide:glide:4.9.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'
}

在AndroidManifest.xml中的<application/>中添加網絡權限網絡

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

 

 

我學習的:app

//導入網絡圖片
 
mIv4=findViewById(R.id.iv_2); Glide.with(this).load("https://www.baidu.com/img/bd_logo1.png?where=super").into(mIv4);

 

//ImageView塊
scaleType= 填充方式
fitCenter 按比例填充直到填滿
fitXY 直接填充可能拉伸圖片
center crop 按比例填充滿,多餘切割不顯示
<ImageView   android:id="@+id/iv_2"   android:layout_width="match_parent"   android:layout_height="200dp"   android:scaleType="fitCenter"   android:background="#000000"   android:layout_below="@id/iv_1"   android:layout_marginTop="20dp"/>
相關文章
相關標籤/搜索