Android
Android [프로필 리사이클앱] 이미지 레이아웃 배치 / 선택한 이미지 크게보기
leopard4
2023. 2. 7. 17:03
XML코드
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<androidx.cardview.widget.CardView
android:id="@+id/cardView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
app:cardBackgroundColor="#AE2E2E"
app:cardCornerRadius="10dp"
app:cardElevation="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="vertical">
<TextView
android:id="@+id/txtTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="제목"
android:textColor="#FFFFFF"
android:textSize="26sp"
android:textStyle="bold|italic" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/imgPhoto"
android:layout_width="55dp"
android:layout_height="55dp"
app:srcCompat="@drawable/ic_baseline_person_search_24" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/txtId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:outlineProvider="paddedBounds"
android:text="ID"
android:textColor="#FFFFFF"
android:textSize="18sp" />
<TextView
android:id="@+id/txtAlbumId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:outlineProvider="paddedBounds"
android:text="AlbumId"
android:textColor="#FFFFFF"
android:textSize="18sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
이 아래부터는 전혀다른 코드
봐야할부분
1. 스크롤을 아래로 내리면 일부사진만 불러오면서 디폴트 이미지가 보임 (네트웤자원을 아끼기위함, Glide의 기능)
2. 이미지를 선택하면 새로운액티비티가 열리면서 확대된 이미지를 불러옴 (메인액티비티로부터 받아온 이미지)
MainActivity
package com.blockent.simpleimgapp;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.app.DownloadManager;
import android.os.Bundle;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.blockent.simpleimgapp.adapter.PostAdapter;
import com.blockent.simpleimgapp.model.Post;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
PostAdapter adapter;
ArrayList<Post> postList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
JsonObjectRequest request = new JsonObjectRequest(
Request.Method.GET,
"https://block1-image-test.s3.ap-northeast-2.amazonaws.com/photos.json",
null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray data = response.getJSONArray("data");
for(int i = 0 ; i < data.length(); i++){
Post post = new Post(
data.getJSONObject(i).getInt("albumId"),
data.getJSONObject(i).getInt("id"),
data.getJSONObject(i).getString("title"),
data.getJSONObject(i).getString("url"),
data.getJSONObject(i).getString("thumbnailUrl")
);
postList.add(post);
}
} catch (JSONException e) {
return;
}
adapter = new PostAdapter(MainActivity.this, postList);
recyclerView.setAdapter(adapter);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}
);
queue.add(request);
}
}
PhotoActivity
package com.blockent.simpleimgapp;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
public class PhotoActivity extends AppCompatActivity {
ImageView imgPhoto;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_photo);
imgPhoto = findViewById(R.id.imgPhoto);
// 이미지를 표시하려면, url 있어야 한다.
// 따라서, 메인액티비티로부터, url을 받아온다!!
String url = getIntent().getStringExtra("url");
Glide.with(PhotoActivity.this)
.load(url)
.placeholder(R.drawable.ic_outline_insert_photo_24)
.into(imgPhoto);
}
}
PostAdapter
package com.blockent.simpleimgapp.adapter;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.blockent.simpleimgapp.PhotoActivity;
import com.blockent.simpleimgapp.R;
import com.blockent.simpleimgapp.model.Post;
import com.bumptech.glide.Glide;
import java.util.ArrayList;
public class PostAdapter extends RecyclerView.Adapter<PostAdapter.ViewHolder> {
Context context;
ArrayList<Post> postList;
public PostAdapter(Context context, ArrayList<Post> postList) {
this.context = context;
this.postList = postList;
}
@NonNull
@Override
public PostAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.post_row, parent, false);
return new PostAdapter.ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull PostAdapter.ViewHolder holder, int position) {
Post post = postList.get(position);
holder.txtTitle.setText(post.title);
holder.txtId.setText(post.id+"");
holder.txtAlbumId.setText(post.albumId+"");
// 이미지뷰에 사진 셋팅.
// URL을 가지고, 네트워크를 통해서 사진데이터를 받아온다.
Glide.with(context).load(post.thumbnailUrl)
.placeholder(R.drawable.ic_outline_insert_photo_24)
.into(holder.imgPhoto);
}
@Override
public int getItemCount() {
return postList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
TextView txtTitle;
TextView txtId;
TextView txtAlbumId;
ImageView imgPhoto;
public ViewHolder(@NonNull View itemView) {
super(itemView);
txtTitle = itemView.findViewById(R.id.txtTitle);
txtId = itemView.findViewById(R.id.txtId);
txtAlbumId = itemView.findViewById(R.id.txtAlbumId);
imgPhoto = itemView.findViewById(R.id.imgPhoto);
imgPhoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int index = getAdapterPosition();
Post post = postList.get(index);
Intent intent = new Intent(context, PhotoActivity.class);
intent.putExtra("url", post.url);
context.startActivity(intent);
}
});
}
}
}
Post (클래스)
package com.blockent.simpleimgapp.model;
public class Post {
public int albumId;
public int id;
public String title;
public String url;
public String thumbnailUrl;
public Post(){
}
public Post(int albumId, int id, String title, String url, String thumbnailUrl) {
this.albumId = albumId;
this.id = id;
this.title = title;
this.url = url;
this.thumbnailUrl = thumbnailUrl;
}
}
https://github.com/leopard4/SimpleImgApp
GitHub - leopard4/SimpleImgApp
Contribute to leopard4/SimpleImgApp development by creating an account on GitHub.
github.com