Implement Glide to view images

This commit is contained in:
Dusk 2022-02-18 22:48:14 +01:00
parent 1b64e7a043
commit b5f98f5465
7 changed files with 48 additions and 26 deletions

View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="deploymentTargetDropDown">
<runningDeviceTargetSelectedWithDropDown>
<Target>
<type value="RUNNING_DEVICE_TARGET" />
<deviceKey>
<Key>
<type value="VIRTUAL_DEVICE_PATH" />
<value value="$USER_HOME$/.android/avd/Pixel_3a_XL_API_30.avd" />
</Key>
</deviceKey>
</Target>
</runningDeviceTargetSelectedWithDropDown>
<timeTargetWasSelectedWithDropDown value="2022-02-18T21:41:13.698736Z" />
</component>
</project>

View File

@ -5,7 +5,7 @@
<map>
<entry key="app/src/main/res/layout/activity_main.xml" value="0.33" />
<entry key="app/src/main/res/layout/fragment_image_grid.xml" value="0.2515625" />
<entry key="app/src/main/res/layout/image_grid_item.xml" value="0.35052083333333334" />
<entry key="app/src/main/res/layout/image_grid_item.xml" value="0.35260416666666666" />
</map>
</option>
</component>

View File

@ -3,6 +3,7 @@
package="com.example.testes">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:allowBackup="true"

View File

@ -7,16 +7,20 @@ import org.json.JSONObject
data class BooruImage (
var id: String,
var rating: String,
var tags: String,
var url: String,
var file_url: String,
var preview_url: String,
) {
companion object {
@Throws(JSONException::class)
fun imageFromJson(json: JSONObject): BooruImage {
val id = json.getInt("id").toString()
val rating = json.getString("rating")
val tags = json.getString("tags")
val url = json.getString("file_url")
return BooruImage(id, tags, url)
val file_url = json.getString("file_url")
val preview_url = json.getString("preview_url")
return BooruImage(id, rating, tags, file_url, preview_url)
}
@Throws(JSONException::class)

View File

@ -1,17 +1,24 @@
package com.example.testes
import android.content.Context
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat.getDrawable
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
class ImageGridAdapter(val imageList: List<BooruImage>) :
RecyclerView.Adapter<ImageGridAdapter.ViewHolder>() {
lateinit var context: Context;
/* Creates and inflates view and return FlowerViewHolder. */
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
context = parent.context;
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.image_grid_item, parent, false)
return ViewHolder(view)
@ -19,16 +26,14 @@ class ImageGridAdapter(val imageList: List<BooruImage>) :
/* Gets current flower and uses it to bind view. */
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.bind(imageList[position])
Glide.with(context)
.load(imageList[position].preview_url)
.placeholder(getDrawable(context, R.drawable.ic_launcher_foreground))
.into(holder.imageview)
}
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
private val text: TextView = itemView.findViewById(R.id.text)
//private val image: ImageView = itemView.findViewById(R.id.image)
fun bind(image: BooruImage) {
text.text = image.url
}
var imageview: ImageView = itemView.findViewById(R.id.image)
}
override fun getItemCount(): Int = imageList.size

View File

@ -12,6 +12,7 @@ import org.json.JSONObject
class MainActivity : AppCompatActivity() {
private val url = "https://gelbooru.com/index.php?page=dapi&s=post&q=index&json=1&limit=18"
lateinit var binding: ActivityMainBinding
private fun imageListFromJson(json: JSONObject) {
@ -35,7 +36,7 @@ class MainActivity : AppCompatActivity() {
setContentView(binding.root)
Request.getRequest(
this,
"https://gelbooru.com/index.php?page=dapi&s=post&q=index&json=1",
url,
::imageListFromJson
)
}

View File

@ -7,20 +7,14 @@
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@mipmap/ic_launcher_round"
android:contentDescription="Image"
/>
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="HOLAAA"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:textAlignment="center"
/>
app:layout_constraintDimensionRatio="H,1:1"
android:contentDescription="Image"
android:scaleType="centerCrop"
android:src="@mipmap/ic_launcher_round" />
</androidx.constraintlayout.widget.ConstraintLayout>