Android制作微信添加多个图片放大图片功能
1.添加依赖
implementation 'com.github.bumptech.glide:glide:4.12.0' //裁剪图片等等 implementation 'androidx.recyclerview:recyclerview:1.1.0' //recycleview依赖
2.使用recycleview
<androidx.recyclerview.widget.RecyclerView android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="wrap_content" />
3.编写两个item页面为recycleview的子页面
<?xml version="1.0" encoding="utf-8"?> <Button xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/btnAdd" android:layout_width="100dp" android:layout_height="120dp" android:layout_margin="4dp" android:layout_weight="1" android:text="Add"/> //点击添加按钮
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="8dp"> <ImageView android:id="@+id/imgView" android:layout_width="100dp" android:layout_height="100dp" /> </LinearLayout>
4.写recycleview的适配器
package com.example.myapplication; import android.content.Context; import android.content.Intent; import android.net.Uri; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.ImageView; import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; import com.bumptech.glide.Glide; import java.util.ArrayList; public class ImageAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { private final int VIEW_TYPE_IMAGE = 0; private final int VIEW_TYPE_ADD_BUTTON = 1; private ArrayList<Uri> imageUris; private Context context; private LayoutInflater inflater; private OnAddButtonClickListener addButtonClickListener; public interface OnAddButtonClickListener { void onAddButtonClick(); } public ImageAdapter(Context context, OnAddButtonClickListener listener) { this.context = context; this.imageUris = new ArrayList<>(); this.inflater = LayoutInflater.from(context); this.addButtonClickListener = listener; } public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { if (viewType == VIEW_TYPE_IMAGE) { View itemView = inflater.inflate(R.layout.iteam, parent, false); return new ImageViewHolder(itemView); } else { View itemView = inflater.inflate(R.layout.iteambutton, parent, false); Button addButton = itemView.findViewById(R.id.btnAdd); addButton.setOnClickListener(v -> addButtonClickListener.onAddButtonClick()); return new AddButtonViewHolder(itemView); } } @Override public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) { if (getItemViewType(position) == VIEW_TYPE_IMAGE) { Uri uri = imageUris.get(position); ImageViewHolder imageHolder = (ImageViewHolder) holder; Glide.with(context) .load(uri) .centerCrop() .into( imageHolder.imageView); //点击图片 放大图片,跳转页面 imageHolder.imageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent=new Intent(context,emptyActivity.class); intent.putExtra("url",uri.toString()); context.startActivity(intent); } }); } } @Override public int getItemCount() { // Plus one for the add button return imageUris.size() + 1 } @Override public int getItemViewType(int position) { return position == imageUris.size() ? VIEW_TYPE_ADD_BUTTON : VIEW_TYPE_IMAGE; } public void addImage(Uri uri) { imageUris.add(uri); notifyItemInserted(imageUris.size() - 1); } static class ImageViewHolder extends RecyclerView.ViewHolder { ImageView imageView; ImageViewHolder(View itemView) { super(itemView); imageView = itemView.findViewById(R.id.imgView); } } static class AddButtonViewHolder extends RecyclerView.ViewHolder { AddButtonViewHolder(View itemView) { super(itemView); } } }
5.编写java文件
package com.example.myapplication; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.provider.MediaStore; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; import androidx.recyclerview.widget.GridLayoutManager; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import java.util.ArrayList; public class RecyCleActivity extends AppCompatActivity implements ImageAdapter.OnAddButtonClickListener{ private static final int REQUEST_CODE_PICK_IMAGE = 1; private ImageAdapter imageAdapter; private RecyclerView recyclerView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_recy_cle); recyclerView = findViewById(R.id.recyclerView); recyclerView.setLayoutManager(new GridLayoutManager(this,3)); imageAdapter = new ImageAdapter(this, this); recyclerView.setAdapter(imageAdapter); } @Override public void onAddButtonClick() { Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(intent, REQUEST_CODE_PICK_IMAGE); } protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == REQUEST_CODE_PICK_IMAGE && resultCode == RESULT_OK) { if (data != null) { Uri selectedImageUri = data.getData(); if (selectedImageUri != null) { imageAdapter.addImage(selectedImageUri); } } } } }
6.编写点击图片后放大图片,创建emptyActivity页面
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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" android:orientation="vertical"> <ImageView android:id="@+id/img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true"/> </RelativeLayout>
package com.example.myapplication; import androidx.appcompat.app.AppCompatActivity; import android.net.Uri; import android.os.Bundle; import android.widget.ImageView; import com.bumptech.glide.Glide; import javax.microedition.khronos.opengles.GL; public class emptyActivity extends AppCompatActivity { private ImageView img; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_empty); img=findViewById(R.id.img); String imageUriString = getIntent().getStringExtra("url"); Glide.with(emptyActivity.this) .load(imageUriString) .into(img); } }
到此这篇关于Android制作微信添加多个图片,放大图片的文章就介绍到这了,更多相关Android微信添加多个图片内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
Android对话框AlertDialog与DatePickerDialog及TimePickerDialog使用详解
这篇文章主要介绍了Android对话框中的提醒对话框AlertDialog、日期对话框DatePickerDialog、时间对话框TimePickerDialog使用方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习吧2022-09-09Android编程之PopupWindow隐藏及显示方法示例(showAtLocation,showAsDropDown
这篇文章主要介绍了Android编程之PopupWindow隐藏及显示方法,结合实例形式分析了showAtLocation及showAsDropDown方法实现PopupWindow控件隐藏及显示功能相关操作技巧,需要的朋友可以参考下2017-02-02Android应用借助LinearLayout实现垂直水平居中布局
这篇文章主要介绍了Android应用借助LinearLayout实现垂直水平居中布局的方法,文中列举了LinearLayout线性布局下居中相关的几个重要参数,需要的朋友可以参考下2016-04-04Android Studio不能获取远程依赖包的完美解决方法
这篇文章主要介绍了Android Studio不能获取远程依赖包的解决方法,非常不错,具有参考借鉴价值,需要的朋友可以参考下2017-11-11Compose自定义View实现绘制Rainbow运动三环效果
这篇文章主要为大家介绍了一个基于Compose自定义的一个Rainbow彩虹运动三环,业务上类似于iWatch上的那个运动三环,感兴趣的小伙伴可以了解一下2023-02-02
最新评论