(搬运) Android Kotlin Fundamentals

01.3: Image resources and compatibility

About this codelab

Written by Google Developers Training team

说明:本系列来自境外 Android Developer。本人只是将其中一些个人觉得重要的部分摘抄下来。如有需要,请自行科学学习。

1. Welcome

This codelab is part of the Android Kotlin Fundamentals course. You’ll get the most value out of this course if you work through the codelabs in sequence. All the course codelabs are listed on the Android Kotlin Fundamentals codelabs landing page.

What you’ll learn

  • How to add files to your app’s resources.
  • How to use images in your app’s layout.
  • How to find views more efficiently in your app’s code.
  • How to use placeholder images in your app’s design with XML namespaces.
  • About Android API levels for your app, and how to understand the minimum, targeted, and compiled API levels.
  • How to use the Jetpack libraries in your app to support older versions of Android.

What you’ll do

  • Modify the DiceRoller app from the last codelab to include images for the dice value, rather than a number.
  • Add image files to your app’s resources.
  • Update the app’s layout and code to use images for the dice value, rather than a number.
  • Update your code to find views more efficiently.
  • Update your code to use an empty image when the app starts.
  • Update your app to use the Android Jetpack libraries for backward-compatibility with older versions of Android.

2. App overview

In this codelab, you build on the DiceRoller app you started in the previous codelab, and you add dice images that change when the dice is rolled. The final DiceRoller app looks like this:

overview
If you did not work through the last codelab, you can download the starting app here: DiceRoller.

3. Task: Add and update image resources

Double-click ic_launcher_background.xml. Note that these are XML files that describe the icon as a vector image. Vectors enable your images to be drawn at many different sizes and resolutions. Bitmap images such as PNG or GIF may need to be scaled for different devices, which can result in some loss of quality.

Update the source of the ImageView with the setImageResource() method and the reference to the dice image you just found.

4. Task: Find views efficiently

Because rollDice() is the click handler for the Roll button, every time the user taps that button, your app calls findViewById() and gets another reference to this ImageView. Ideally, you should minimize the number of calls to findViewById(), because the Android system is searching the entire view hierarchy each time, and that’s an expensive operation.

Instead it is a best practice to just call findViewById() once and store the View object in a field.

Ideally you would initialize this variable up here when it’s declared, or in a constructor—but Android activities don’t use constructors. In fact, the views in the layout are not accessible objects in memory at all until after they have been inflated in the onCreate() method, by the call to setContentView().

In onCreate(), after the setContentView() method, use findViewById() to get the ImageView.

Checking whether a lateinit var is initialized
To check whether a lateinit var has already been initialized, use .isInitialized on the reference to that property:

if (foo::bar.isInitialized) {
    println(foo.bar)
}

This check is only available for the properties that are lexically accessible, when declared in the same type or in one of the outer types, or at top level in the same file.

5. Task: Use a default image

It’s fairly common that the contents of a design might be defined dynamically at runtime—for example, any app that grabs data from the internet should probably start with a blank or empty screen. But it’s helpful when you’re designing an app to have some sort of placeholder data in the layout so you know what you’re laying out.

android:src="@drawable/empty_dice" 
tools:src="@drawable/dice_1" />

The tools namespace is used when you want to define placeholder content that is only used in the preview or the design editor in Android Studio. Attributes using the tools namespace are removed when you compile the app.
Namespaces are used to help resolve ambiguity when referring to attributes that have the same name.

6. Task: Understand API levels and compatibility

When you write for Android, you don’t write completely separate apps for each of these different devices—even apps that run on radically different form factors such as watches and TVs can share code. But there are still constraints and compatibility strategies that you need to be aware of to support all of this.

In this task, you learn how to target your app for specific Android API levels (versions), and how to use the Android Jetpack libraries to support older devices.

In 2011, the team released the first support library, a Google-developed library that offers backward-compatible classes and helpful functions. In 2018, Google announced Android Jetpack, which is a collection of libraries that includes many of the previous classes and functions of the support library, while also expanding on the support library.


Vector drawables are only natively supported in versions of Android higher than API 21. In older versions, Gradle generates PNG images for those drawables when your app is built.

The good news is that there is an Android X compatibility library for vector drawables all the way back to API level 7.

Open build.gradle (Module: app). Add this line to the defaultConfig section:

vectorDrawables.useSupportLibrary = true

Change the android:src attribute in the <ImageView> element to be app:srcCompat.

app:srcCompat="@drawable/empty_dice"

The app:srcCompat attribute uses the Android X library to support vector drawables in older versions of Android, back to API level 7.

学习更多

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值