小白开发程序之路(2-7) ImageView(常用属性、加载网络图片、ToggleButtton、Switch)

ImageView网络图片无法加载

Glide 报错“Load failed for xxxx”
1.可能是你给的图片地址的问题,把http改成https(我的蕾姆就是http改https就行了!)图片后缀可以是jpg,png或者webp
2.需要给网络权限
3.google()要删去
具体看下面代码

代码

activity_image_view.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
  <ImageView
      android:id="@+id/iv_1"
      android:layout_width="200dp"
      android:layout_height="100dp"
    android:background="#1199AA"
    android:src="@drawable/bg_iron_man"
      android:scaleType="fitXY"/>
  <ImageView
    android:id="@+id/iv_2"
    android:layout_width="200dp"
    android:layout_height="100dp"
    android:background="#1199AA"
    android:src="@drawable/bg_iron_man"
    android:scaleType="fitCenter"
    android:layout_below="@id/iv_1"
    android:layout_marginTop="10dp"/>
  <ImageView
      android:id="@+id/iv_3"
      android:layout_width="200dp"
      android:layout_height="100dp"
      android:background="#1199AA"
      android:src="@drawable/bg_iron_man"
      android:scaleType="centerCrop"
      android:layout_below="@id/iv_2"
      android:layout_marginTop="10dp"/>
  <ImageView
      android:id="@+id/iv_4"
      android:layout_width="200dp"
      android:layout_height="100dp"
      android:background="#1199AA"
      android:layout_below="@id/iv_3"
      android:layout_marginTop="10dp"/>
  <!--background是背景,src是内容,都可以设为图片
scaleType是缩放的类型:
fitXY-图片撑满控件,会拉伸
fitCenter 保持宽高比缩放,直至完全展示
centerCrop 保持宽高比缩放,直至完全覆盖控件,裁剪显示
-->
  </RelativeLayout>

build.gradle

在这个地方把GitHub上的glide相关代码复制过来
注意google()我们用不了,要删去。

### build.gradle
在这个地方把GitHub上的glide相关代码复制过来

> apply plugin: 'com.android.application'

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.0"

    defaultConfig {
        applicationId "com.example.myapplication"
        minSdkVersion 16
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}
repositories {
    jcenter()
    //把Google()删掉,因为我们访问不了谷歌
}
//把Github上的代码复制过来
dependencies {
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    implementation 'com.github.bumptech.glide:glide:4.11.0'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'
}

ImageViewActivity

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.media.Image;
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_image_view);
        mIv4=findViewById(R.id.iv_4);
        Glide.with(this).load("https://pic1.zhimg.com/v2-bb94fd1e01ac7698f84cfa10f242765b_xl.jpg").into(mIv4);
    }
}

Androidmainfest.xml

这里要给网络权限

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapplication" >
    <uses-permission android:name="android.permission.INTERNET"/>
    <!--申明权限,网络请求    -->
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >
        <activity android:name=".ImageViewActivity" >
        </activity>
        <activity android:name=".CheckBoxActivity" />
        <activity android:name=".RadioButtonActivity" />
        <activity android:name=".EditTextActivity" />
        <activity android:name=".ButtonActivity" />
        <activity android:name=".TextViewActivity" />
        <activity android:name=".Listview.ListViewActivity" />
        <activity android:name=".MainActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

效果

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值