使用代码

时间:2019-12-28 17:25:11

标签: android-recyclerview imageview share screenshot cardview

我正在开发一个android应用程序,必须从recyclerView共享cardView。我添加了共享按钮,当我单击它以共享特定的cardView时,我只能获取文本,但确切的图像没有出现在屏幕截图中。我得到的是Picasso占位符图像,而不是将图像放入屏幕截图中,如果互联网无法正常工作,我会保留该图像。请帮我。请查看屏幕截图以更好地了解。我添加了调试我的问题所需的所有东西。

谢谢。

This is the screenshot I took normally with phone buttons

This is the screenshot of the card I took with a button click from my app

这是当我单击“共享”按钮时将触发的Onclick事件。

       @Override
        public void onShareClick(View v, int position) throws IOException {
            mRecyclerView = view.findViewById(R.id.feed_recyclerView);
            Date now = new Date();
            android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);
            String mPath = Environment.getExternalStorageDirectory().toString() + "/" + now + ".jpg";
            Bitmap bitmap = onShare(mRecyclerView,position);
            File file = new File(mPath);
            FileOutputStream outputStream = new FileOutputStream(file);
            int quality = 100;
                bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
                outputStream.flush();
                outputStream.close();
            openScreenshot(file);
        }

这是我编写的onShare方法,用于创建屏幕截图。

 public Bitmap onShare(RecyclerView view, int position) {
            RecyclerView.Adapter adapter = view.getAdapter();
            Bitmap bigBitmap = null;
            if (adapter != null) {
                int size = adapter.getItemCount();
                int height = 0;
                Paint paint = new Paint();
                int iHeight = 0;
                final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);

                // Use 1/8th of the available memory for this memory cache.
                final int cacheSize = maxMemory / 8;
                LruCache<String, Bitmap> bitmaCache = new LruCache<>(cacheSize);
                for (int i = 0; i < 1; i++) {
                    RecyclerView.ViewHolder holder = adapter.createViewHolder(view, adapter.getItemViewType(position));
                    adapter.onBindViewHolder(holder, position);
                    holder.itemView.measure(View.MeasureSpec.makeMeasureSpec(view.getWidth(), View.MeasureSpec.EXACTLY),
                            View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
                    holder.itemView.layout(0, 0, holder.itemView.getMeasuredWidth(), holder.itemView.getMeasuredHeight());
                    holder.itemView.setDrawingCacheEnabled(true);
                    holder.itemView.buildDrawingCache();
                    Bitmap drawingCache = holder.itemView.getDrawingCache();
                    if (drawingCache != null) {

                        bitmaCache.put(String.valueOf(position), drawingCache);
                    }
                    height += holder.itemView.getMeasuredHeight();
                }

                bigBitmap = Bitmap.createBitmap(view.getMeasuredWidth(), height, Bitmap.Config.ARGB_8888);
                Canvas bigCanvas = new Canvas(bigBitmap);
                bigCanvas.drawColor(Color.WHITE);

                for (int i = 0; i < 1; i++) {
                    Bitmap bitmap = bitmaCache.get(String.valueOf(position));

                    bigCanvas.drawBitmap(bitmap, 0f, iHeight, paint);

                    iHeight += bitmap.getHeight();
                }

            }
            return bigBitmap;
        }

这是来自recyclerview适配器的Image View支架。

 public class ImageViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnCreateContextMenuListener,
        MenuItem.OnMenuItemClickListener {

    public TextView TextViewName;
    public TextView DescView;
    public ImageView imageView;
    public ImageButton copy;
    public ImageButton download;
    public ImageButton share;

    public ImageViewHolder(@NonNull View itemView) {
        super(itemView);
        TextViewName = itemView.findViewById(R.id.feed_name);
        DescView = itemView.findViewById(R.id.feed_desc);
        imageView = itemView.findViewById(R.id.feed_image);
        copy = itemView.findViewById(R.id.copy_feed);
        copy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            mListener.onCopyClick(v,getAdapterPosition());
            }
        });

        download = itemView.findViewById(R.id.download_feed);
        download.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mListener.onDownloadClick(v,getAdapterPosition());
            }
        });
        share  = itemView.findViewById(R.id.share_feed);
        share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    mListener.onShareClick(v,getAdapterPosition());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
        itemView.setOnClickListener(this);
        itemView.setOnCreateContextMenuListener(this);

    }

这是来自回收者视图适配器的OncreateViewHolder:

 @NonNull
@Override
public ImageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(mContext).inflate(R.layout.feed_card_view, parent, false);

    return new ImageViewHolder(v);
}

这是名片视图的XML代码:

<LinearLayout 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="wrap_content"
    >


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/fcardRelative"
        android:orientation="vertical">


        <androidx.cardview.widget.CardView
            android:id="@+id/fcard"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/spacing_middle"
            android:layout_marginTop="10dp"
            android:layout_marginRight="@dimen/spacing_middle"
            android:layout_marginBottom="10dp"
            android:visibility="visible"
            app:cardCornerRadius="2dp"
            app:cardElevation="2dp">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/white"
                android:orientation="vertical">

                <RelativeLayout
                    android:layout_width="wrap_content"
                    android:layout_height="250dp">

                    <ImageView
                        android:id="@+id/feed_image"
                        android:layout_width="match_parent"
                        android:layout_height="250dp" />
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentBottom="true"
                        android:layout_margin="@dimen/spacing_large"
                        android:textSize="20sp"
                        android:text="FCIM International Ministries"
                        android:textAppearance="@style/TextAppearance.AppCompat.Headline"
                        android:textColor="@android:color/white" />
                   <TextView
                       android:layout_width="wrap_content"
                       android:layout_height="wrap_content"
                       android:layout_alignParentEnd="true"
                       android:layout_alignParentTop="true"
                       android:text="26/12/2019"
                       android:layout_margin="@dimen/spacing_large"
                       android:textColor="@color/white_transparency"
                       android:textAppearance="@style/TextAppearance.AppCompat.Light.Widget.PopupMenu.Small"
                       />

                </RelativeLayout>


                <TextView
                    android:id="@+id/no"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />


                <TextView
                    android:id="@+id/feed_name"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_margin="15dp"
                    android:textAppearance="@style/TextAppearance.AppCompat.Headline" />

                <TextView
                    android:id="@+id/feed_desc"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="bottom"
                    android:layout_marginLeft="@dimen/spacing_large"
                    android:layout_marginRight="@dimen/spacing_large"
                    android:layout_marginBottom="@dimen/spacing_large"
                    android:maxLength="275"
                    android:textAppearance="@style/TextAppearance.AppCompat.Subhead" />

                <View
                    android:layout_width="wrap_content"
                    android:layout_height="1dp"
                    android:background="@color/teal_700" />

                  <LinearLayout
                      android:layout_width="match_parent"
                      android:layout_height="?attr/actionBarSize"
                      android:orientation="horizontal"
                      android:gravity="right"
                      android:background="@color/teal_700"
                      android:layout_gravity="center_horizontal">

                      <ImageButton
                          android:layout_width="?attr/actionBarSize"
                          android:layout_height="?attr/actionBarSize"
                          android:background="?attr/selectableItemBackgroundBorderless"
                          android:tint="@color/grey_3"
                          app:srcCompat="@drawable/ic_favorites" />

                      <ImageButton
                          android:layout_width="?attr/actionBarSize"
                          android:layout_height="?attr/actionBarSize"
                          android:background="?attr/selectableItemBackgroundBorderless"
                          android:tint="@color/grey_3"
                          app:srcCompat="@drawable/ic_bookmark" />

                      <ImageButton
                          android:layout_width="?attr/actionBarSize"
                          android:layout_height="?attr/actionBarSize"
                          android:background="?attr/selectableItemBackgroundBorderless"
                          android:tint="@color/black"
                          android:id="@+id/copy_feed"
                          app:srcCompat="@drawable/ic_content_copy"/>

                      <ImageButton
                          android:layout_width="?attr/actionBarSize"
                          android:layout_height="?attr/actionBarSize"
                          android:background="?attr/selectableItemBackgroundBorderless"
                          android:tint="@color/black"
                          android:id="@+id/download_feed"
                          app:srcCompat="@drawable/ic_file_download"/>
                      <ImageButton
                          android:layout_width="?attr/actionBarSize"
                          android:layout_height="?attr/actionBarSize"
                          android:background="?attr/selectableItemBackgroundBorderless"
                          android:tint="@color/black"
                          android:id="@+id/share_feed"
                          app:srcCompat="@drawable/ic_share" />


                  </LinearLayout>
            </LinearLayout>
        </androidx.cardview.widget.CardView>


    </RelativeLayout>

</LinearLayout>

这是Recyclerview和View页面的XML代码

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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=".feed_feed">


    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab_feed"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end|right"
        android:layout_margin="@dimen/spacing_smlarge"
        android:clickable="true"
        android:tint="@android:color/white"
        app:fabSize="normal"
        app:rippleColor="@android:color/white"
        app:srcCompat="@drawable/ic_create" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent">

        <View
            android:layout_width="2dp"
            android:layout_height="match_parent"
            android:layout_marginStart="50dp"
            android:background="@color/grey_80" />

    </LinearLayout>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/feed_recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />


</FrameLayout>

0 个答案:

没有答案