Android制作微信添加多个图片,放大图片

1.添加依赖

复制代码
implementation 'com.github.bumptech.glide:glide:4.12.0' //裁剪图片等等
implementation 'androidx.recyclerview:recyclerview:1.1.0' //recycleview依赖

2.使用recycleview

java 复制代码
    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

3.编写两个item页面为recycleview的子页面

java 复制代码
<?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"/>
//点击添加按钮
java 复制代码
<?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的适配器

java 复制代码
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文件

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页面

java 复制代码
<?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>
java 复制代码
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);

    }
}
相关推荐
帅次6 分钟前
Android CoordinatorLayout:打造高效交互界面的利器
android·gradle·android studio·rxjava·android jetpack·androidx·appcompat
枯骨成佛1 小时前
Android中Crash Debug技巧
android
kim56596 小时前
android studio 更改gradle版本方法(备忘)
android·ide·gradle·android studio
咸芝麻鱼6 小时前
Android Studio | 最新版本配置要求高,JDK运行环境不适配,导致无法启动App
android·ide·android studio
无所谓จุ๊บ6 小时前
Android Studio使用c++编写
android·c++
csucoderlee7 小时前
Android Studio的新界面New UI,怎么切换回老界面
android·ui·android studio
kim56597 小时前
各版本android studio下载地址
android·ide·android studio
饮啦冰美式7 小时前
Android Studio 将项目打包成apk文件
android·ide·android studio
夜色。7 小时前
Unity6 + Android Studio 开发环境搭建【备忘】
android·unity·android studio
ROCKY_8179 小时前
AndroidStudio-滚动视图ScrollView
android