5. ImageView with image url
페이지 정보
작성자 관리자 댓글 0건 조회 2,310회 작성일 21-05-18 10:15본문
1. 프로젝트 생성
프로젝트명 : ImageViewTest
2. 레이아웃 수정
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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=".MainActivity">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
3. MainActivity.kt 수정
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
DownloadImageFromInternet(findViewById(R.id.imageView))
.execute("https://www.leelab.co.kr/img/logo.png")
}
private inner class DownloadImageFromInternet(var imageView: ImageView) : AsyncTask<String, Void, Bitmap?>() {
init {
Toast.makeText(applicationContext, "Please wait, it may take a few minute...",
Toast.LENGTH_SHORT).show()
}
override fun doInBackground(vararg urls: String): Bitmap? {
val imageURL = urls[0]
var image: Bitmap? = null
try {
val `in` = java.net.URL(imageURL).openStream()
image = BitmapFactory.decodeStream(`in`)
}
catch (e: Exception) {
Log.e("Error Message", e.message.toString())
e.printStackTrace()
}
return image
}
override fun onPostExecute(result: Bitmap?) {
imageView.setImageBitmap(result)
}
}
}
4. Manifast 수정
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="kr.co.leelab.imageviewtest">
<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/Theme.ImageViewTest">
<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>
5. 실행
댓글목록
등록된 댓글이 없습니다.