是否可以为我的应用程序的活动创建一个像Android一样的最近屏幕

我正在尝试制作一个将在我的应用程序中而不是在Android设备中保存所有最近屏幕的内容。这个想法是像在Android中一样创建最近视图。我目前不知道如何做到这一点。我已经在Android的官方网站上搜索,但没有得到我想要的东西。我有一些活动是声明的,我想采取这一点并保存以显示在.我能够做的设计我已经实现了,但我不知道如何显示最近的屏幕或视图。背景的颜色应基于 url。如果有人不明白什么,请告诉我。在下面,您可以找到一张照片,该照片将显示我正在尝试做的事情,我已经从另一个应用程序复制了这张照片。为了实现这一目标,我计划采取这些步骤。在和它没有定义中将显示的,因为我不知道如何实现这一点。我希望目前蓝色的背景取决于url颜色。有没有办法获得网址的颜色?ActivityWebViewtitle, url and the viewUrlVisualHistoryPojo.classdbimagecardview

创建 POJO.class
创建将保存此数据的
DB 创建适配器
创建片段

我这里有一个日志,它向我展示了这些项目。

[VisualHistoryItem{id=109, title='Wikipedia, the free encyclopedia', url='https://en.m.wikipedia.org/w/index.php?title=Main_Page'}, VisualHistoryItem{id=112, title='', url='https://mail.google.com/'}, VisualHistoryItem{id=113, title='Gmail – kostenloser Speicherplatz und E-Mails von Google', url='https://www.google.com/intl/de/gmail/about/#'}]

这将保存一个可视历史记录项。

mVisualHistory.setUrl(url);
mVisualHistory.setTitle(view.getTitle());
Bitmap topass= getSnapshoot.takeScreenShot(BookmarkActivity.this);
   try {
      String filename = mVisualHistory.getId()+".png";
      FileOutputStream stream = BookmarkActivity.this.openFileOutput(filename, Context.MODE_PRIVATE);
     topass.compress(Bitmap.CompressFormat.PNG, 100, stream);
     stream.close();
     topass.recycle();
      } catch (Exception e) {
          e.printStackTrace();
      }
  mVisualRepository.insertNoteTask(mVisualHistory);

我在下面创建了一个代码。Activity

public class ActivityTabs extends AppCompatActivity implements View.OnClickListener {
    private PopupMenu mPopupMenu;
    private FrameLayout settings;
    private FrameLayout frameLayout;
    private LinearLayout linearLayout;
    private ImageView incognito;
    private TextView textOfHistory;

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_tabs);
        findViewById(R.id.vgGoMain).setOnClickListener(this);
        findViewById(R.id.vgAdd).setOnClickListener(this);
        settings = findViewById(R.id.vgSettingsHis);
        linearLayout = findViewById(R.id.layoutEmptyVisHistory);
        settings.setOnClickListener(this);
        textOfHistory = findViewById(R.id.tvEmptyHistory);



 FragmentVisualHistoryVertical newFragment = new FragmentVisualHistoryVertical();
    getSupportFragmentManager().beginTransaction().add(R.id.frameLayoutVisHistory, newFragment).commit();
    getSupportFragmentManager()
            .beginTransaction()
            .replace(R.id.frameLayoutVisHistory, newFragment)
            .commit();

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.vgGoMain:
                finish();
                return;
            case R.id.vgAdd:
                Intent intent = new Intent(this, ActivitySearchEngine.class);
                startActivity(intent);
                finish();
                return;
            case R.id.vgSettingsHis:
                showMenuSettings();
                return;
                default:
                    break;
        }
    }

    public void showMenuSettings() {
        mPopupMenu = new PopupMenu(this, settings);
        final MenuInflater menuInflater = mPopupMenu.getMenuInflater();
        menuInflater.inflate(R.menu.history_settings, mPopupMenu.getMenu());
        mPopupMenu.show();

    }
}

这就是这个.XMLActivity

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/historyEmptyBack"
    android:gravity="center_horizontal"
    android:orientation="vertical">

    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <FrameLayout
            android:id="@+id/frameLayoutVisHistory"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:paddingBottom="@dimen/bottomPanelHeight"></FrameLayout>

        <LinearLayout
            android:id="@+id/layoutEmptyVisHistory"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical"
            android:visibility="gone">

            <TextView
                android:id="@+id/tvEmptyHistoryTitle"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="@dimen/common_24dp"
                android:gravity="center_horizontal"
                android:lineSpacingExtra="3.0sp"
                android:text="@string/VHVEmptyTite"
                android:textColor="@color/historyEmptyTitle"
                android:textSize="22.0sp" />

            <FrameLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_marginBottom="48.0dip"
                android:layout_weight="1.0">

                <LinearLayout
                    android:id="@+id/horizontalEmpty"
                    style="@style/LayoutEmptySmile"
                    android:layout_width="@dimen/visual_history_element_width"
                    android:layout_height="@dimen/visual_history_element_height"
                    android:orientation="vertical">

                    <ImageView
                        style="@style/EmptyHistorySmile"
                        android:src="@drawable/vh_smile_gray" />

                    <TextView
                        style="@style/EmptyHistoryText"
                        android:layout_marginTop="@dimen/common_16dp"
                        android:gravity="center_horizontal"
                        android:paddingLeft="@dimen/common_16dp"
                        android:paddingRight="@dimen/common_16dp"
                        android:text="@string/VHVEmptyDesc" />
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/verticalEmpty"
                    style="@style/LayoutEmptySmile"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginLeft="@dimen/common_24dp"
                    android:layout_marginRight="@dimen/common_24dp"
                    android:orientation="horizontal"
                    android:paddingLeft="@dimen/common_16dp"
                    android:paddingTop="@dimen/common_16dp"
                    android:paddingRight="@dimen/common_16dp"
                    android:paddingBottom="@dimen/common_16dp">

                    <ImageView
                        style="@style/EmptyHistorySmile"
                        android:src="@drawable/vh_smile_gray" />

                    <TextView
                        style="@style/EmptyHistoryText"
                        android:layout_width="wrap_content"
                        android:gravity="left"
                        android:paddingLeft="@dimen/common_16dp"
                        android:text="@string/VHVEmptyDesc" />
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/layoutIncognito"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:orientation="vertical"
                    android:paddingBottom="@dimen/bottomPanelHeight"
                    android:visibility="visible">

                    <LinearLayout
                        android:id="@+id/layoutEmptyDesc"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:layout_marginTop="@dimen/common_16dp"
                        android:gravity="center"
                        android:orientation="vertical">

                        <ImageView
                            android:id="@+id/ivEmptyHistory"
                            android:layout_width="@dimen/history_private"
                            android:layout_height="@dimen/history_private"
                            android:src="@drawable/incognito_icon_history" />

                        <TextView
                            android:id="@+id/tvEmptyHistory"
                            style="@style/EmptyHistoryText"
                            android:layout_marginTop="@dimen/common_16dp"
                            android:gravity="center_horizontal"
                            android:paddingLeft="@dimen/common_16dp"
                            android:paddingRight="@dimen/common_16dp"
                            android:text="@string/SVSearchPrivateMode"
                            android:textColor="@color/historyTextColor"
                            android:textSize="@dimen/common_18sp" />
                    </LinearLayout>
                </LinearLayout>

            </FrameLayout>

        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:background="@color/historyEmptyBack"
            android:gravity="bottom"
            android:orientation="vertical"
            android:paddingLeft="2.0dip"
            android:paddingRight="2.0dip">

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"
                android:orientation="horizontal">

                <LinearLayout style="@style/VisHistoryMenuSideLayout">

                    <FrameLayout
                        android:id="@+id/vgGoMain"
                        style="@style/VisHistoryFrLayoutMenu"
                        android:paddingRight="14.0dip">

                        <TextView
                            style="@style/VisHistoryTvMenu"
                            android:text="@string/VHVHomeBarButtonItemTitle" />
                    </FrameLayout>

                    <View style="@style/VisHistoryEmptyView" />
                </LinearLayout>

                <FrameLayout
                    android:id="@+id/vgAdd"
                    style="@style/VisHistoryFrLayoutMenu">

                    <ImageView
                        style="@style/VisHistoryMenuIv"
                        android:scaleX="0.8"
                        android:scaleY="0.8"
                        android:src="@drawable/newtab_button" />
                </FrameLayout>

                <LinearLayout style="@style/VisHistoryMenuSideLayout">
                    <View style="@style/VisHistoryEmptyView" />
                    <FrameLayout
                        android:id="@+id/vgTrash"
                        style="@style/VisHistoryFrLayoutMenu">
                        <ImageView
                            style="@style/VisHistoryMenuIv"
                            android:scaleX="1.3"
                            android:scaleY="1.3"
                            android:src="@drawable/trash" />
                    </FrameLayout>

                    <View style="@style/VisHistoryEmptyView" />

                    <FrameLayout
                        android:id="@+id/vgSettingsHis"
                        style="@style/VisHistoryFrLayoutMenu"
                        android:paddingLeft="0.0dip"
                        android:paddingRight="0.0dip">

                        <android.support.v7.widget.AppCompatImageView
                            style="@style/VisHistoryMenuIv"
                            android:layout_gravity="right"
                            app:srcCompat="@drawable/ic_dots_vertical"
                            tools:ignore="VectorDrawableCompat" />
                    </FrameLayout>
                </LinearLayout>
            </LinearLayout>
        </LinearLayout>
    </FrameLayout>
</LinearLayout>

这是另一个 这里将显示的列表Fragment

  public class FragmentVisualHistoryVertical extends FragmentVisualHistory implements VisualRecyclerAdapter.OnVisualHistoryItemListener {
    public View paramView;
    private VisualRecyclerAdapter mVisualHistoryRecyclerAdapter;
    private RecyclerView mRecyclerView;
    private VisualHistoryRepository getmNoteRepository;

    private ArrayList<VisualHistoryItem> mVisualHistoryItems = new ArrayList<>();

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mContext = context;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        paramView = inflater.inflate(R.layout.fragment_vis_history_vertical, container, false);
        mRecyclerView = paramView.findViewById(R.id.rvWebHistory);
        initRecyclerView();
        getmNoteRepository = new VisualHistoryRepository(getActivity());
        retrieveVisualHistory();
        return paramView;
    }

    private void initRecyclerView(){
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        mRecyclerView.setLayoutManager(linearLayoutManager);
        linearLayoutManager.setReverseLayout(true);
        new ItemTouchHelper(itemTouchHelperCallback).attachToRecyclerView(mRecyclerView);
        mVisualHistoryRecyclerAdapter = new VisualRecyclerAdapter(mVisualHistoryItems, this, mContext);
        DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(mRecyclerView.getContext(),
                linearLayoutManager.getOrientation());
        mRecyclerView.addItemDecoration(dividerItemDecoration);
        mRecyclerView.setAdapter(mVisualHistoryRecyclerAdapter);
    }
    private void retrieveVisualHistory() {
        getmNoteRepository.retrieveVisualHistoryTask().observe(this, new Observer<List<VisualHistoryItem>>() {
            @Override
            public void onChanged(@Nullable List<VisualHistoryItem> item) {
                if(mVisualHistoryItems.size() > 0){
                    mVisualHistoryItems.clear();
                }
                if(item != null){
                    mVisualHistoryItems.addAll(item);
                }
                mVisualHistoryRecyclerAdapter.notifyDataSetChanged();
            }
        });
    }


    @Override
    public void onItemClicked(int position) {

    }
    private void deleteNote(VisualHistoryItem item) {
        mVisualHistoryItems.remove(item);
        mVisualHistoryRecyclerAdapter.notifyDataSetChanged();

        getmNoteRepository.deleteVisualHistoryTask(item);
    }
    ItemTouchHelper.SimpleCallback itemTouchHelperCallback = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.RIGHT) {
        @Override
        public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
            return false;
        }

        @Override
        public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
            deleteNote(mVisualHistoryItems.get(viewHolder.getAdapterPosition()));
        }
    };
}

这是 .XML

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:aapt="http://schemas.android.com/aapt" android:id="@+id/rvWebHistory" android:layout_width="match_parent" android:layout_height="match_parent"/>

这是room db

@Database(entities = {VisualHistoryItem.class}, version = 1)
public abstract class VisualHistoryDB extends RoomDatabase {
private static final String DATABASE_NAME = "visualHistory_db";
private static VisualHistoryDB instance;

public static VisualHistoryDB getInstance(final Context context) {
    if (instance == null) {
        instance = Room.databaseBuilder(
                context.getApplicationContext(),
                VisualHistoryDB.class,
                DATABASE_NAME
        ).build();
    }
    return instance;
}

public abstract VisualHistoryDao getVisualHistoryDao();
}

room db Dao

@Dao
public interface VisualHistoryDao {

@Insert
long[] insertVisualHistory(VisualHistoryItem... visualHistoryItems);

@Query("SELECT * FROM visualHistory")
LiveData<List<VisualHistoryItem>> getVisualHistory();

@Delete
int delete(VisualHistoryItem... visualHistoryItems);
}

这是pojo.class

@Entity(tableName = "visualHistory")
public class VisualHistoryItem implements Parcelable {

@PrimaryKey(autoGenerate = true)
private int id;

@ColumnInfo(name = "title")
private String title;

@ColumnInfo(name = "url")
private String url;

public VisualHistoryItem(int id, String title, String url) {
    this.id = id;
    this.title = title;
    this.url = url;
}

@Ignore
public VisualHistoryItem() {
}

protected VisualHistoryItem(Parcel in) {
    id = in.readInt();
    title = in.readString();
    url = in.readString();
}

public static final Creator<VisualHistoryItem> CREATOR = new Creator<VisualHistoryItem>() {
    @Override
    public VisualHistoryItem createFromParcel(Parcel in) {
        return new VisualHistoryItem(in);
    }

    @Override
    public VisualHistoryItem[] newArray(int size) {
        return new VisualHistoryItem[size];
    }
};

public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
public String getTitle() {
    return title;
}
public void setTitle(String title) {
    this.title = title;
}

public String getUrl() {
    return url;
}
public void setUrl(String url) {
    this.url = url;
}

@Override
public String toString() {
    return "VisualHistoryItem{" +
            "id=" + id +
            ", title='" + title + '\'' +
            ", url='" + url + '\'' +
            '}';
}

@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel parcel, int flags) {
    parcel.writeInt(id);
    parcel.writeString(title);
    parcel.writeString(url);
}
}

这是.Adapter

    public class VisualRecyclerAdapter extends RecyclerView.Adapter<VisualRecyclerAdapter.ViewHolder> {

    private ArrayList<VisualHistoryItem> mVisualHistoryItem  = new ArrayList<>();
    private OnVisualHistoryItemListener mItemListener;
    private final Context context;

    public VisualRecyclerAdapter(ArrayList<VisualHistoryItem> mVisualHistoryItem, OnVisualHistoryItemListener mItemListener, Context context) {
        this.context = context;
        this.mVisualHistoryItem = mVisualHistoryItem;
        this.mItemListener = mItemListener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.fragment_visual_history, viewGroup, false);

        return new ViewHolder(view, mItemListener);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        Resources res = viewHolder.itemView.getContext().getResources();
        viewHolder.visFragmentMain.setBackgroundColor(res.getColor(R.color.blue_text));
        viewHolder.tvPageUrl.setText(mVisualHistoryItem.get(i).getUrl());
        viewHolder.tvPageName.setText(mVisualHistoryItem.get(i).getTitle());
        Bitmap bmp = null;
        String filename = mVisualHistoryItem.get(i).getId()+".png";
        try {
            FileInputStream is = context.openFileInput(filename);
            bmp = BitmapFactory.decodeStream(is);
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (bmp!=null) {
            BitmapDrawable ob = new BitmapDrawable(context.getResources(), bmp);
            viewHolder.ivVisualHistory.setBackground(ob);
        }
    }

    @Override
    public int getItemCount() {
        return mVisualHistoryItem.size();
    }
    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        ImageView ivVisualHistory;
        OnVisualHistoryItemListener itemListener;
        TextView tvPageName, tvPageUrl;
        RelativeLayout visFragmentMain;
        CardView cardView;
        public ViewHolder(@NonNull View itemView, OnVisualHistoryItemListener mItemListener) {
            super(itemView);
            itemListener = mItemListener;
            ivVisualHistory = itemView.findViewById(R.id.ivVisualHistory);
            visFragmentMain = itemView.findViewById(R.id.visFragmentMain);
            tvPageName = itemView.findViewById(R.id.tvPageName);
            tvPageUrl = itemView.findViewById(R.id.tvPageUrl);
            cardView = itemView.findViewById(R.id.cardView);
        }

        @Override
        public void onClick(View v) {
            itemListener.onItemClicked(getAdapterPosition());
        }
    }

    public interface OnVisualHistoryItemListener {
        void onItemClicked(int position);
    }
}

这就是我正在努力实现的目标。enter image description here

这是我的实际观点。

enter image description here


答案 1

好吧,您可以获取网站的图标,并使用此处解释的Android的调色板类来获取URL的突出颜色,如果要获取快照,则可以在启动另一个活动之前保存快照并保存实例数据。要拍摄快照,您可以在创建util类后使用takenapshot meathod,代码如下:

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.view.View;

class Util {
    static Bitmap takeScreenShot(Activity activity) {
        View view = activity.getWindow().getDecorView();
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache();
        Bitmap b1 = view.getDrawingCache();
        Rect frame = new Rect();
        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
        int statusBarHeight = frame.top;

        //Find the screen dimensions to create bitmap in the same size.
        int width = activity.getWindowManager().getDefaultDisplay().getWidth();
        int height = activity.getWindowManager().getDefaultDisplay().getHeight();

        Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height - statusBarHeight);
        view.destroyDrawingCache();
        return b;
    }
}

在您的活动中使用它将其保存到png文件,如下所示:

Bitmap topass=Util.takeScreenShot(this);
                try {
                    //Write file
                    String filename = "bitmap.png";
                    FileOutputStream stream = this.openFileOutput(filename, Context.MODE_PRIVATE);
                    topass.compress(Bitmap.CompressFormat.PNG, 100, stream);

                    //Cleanup
                    stream.close();
                    topass.recycle();
                } catch (Exception e) {
                    e.printStackTrace();
                }

不要使用名称“位图.png”,请使用“id.png”(String.valueof(id)+“.png”),因为您已经在数据库中输入了int“id”名称。您可以保存每个活动的屏幕截图及其ID,并且每当您构建它的卡片视图时,请以“id.png”的名称显示屏幕截图。要显示屏幕截图,您可以使用

Bitmap bmp = null;
        String filename = String.valueof(getId())+".png";
        try {
            FileInputStream is = this.openFileInput(filename);
            bmp = BitmapFactory.decodeStream(is);
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(bmp!=null)
        {
            ConstraintLayout cl =findViewById(R.id.shopbk); //it can be your any view
            BitmapDrawable ob = new BitmapDrawable(getResources(), bmp);
            cl.setBackground(ob); /* you should use glide here to show bitmap drawable as your preview will be very small as compared to screenshot actual size*/
        }  

至于获得颜色(你说链接不起作用)是关心的,你可以首先得到位图使用

try {
        URL url = new URL("http://..somepath url../favicon.ico");
        Bitmap image = BitmapFactory.decodeStream(url.openConnection().getInputStream());
    } catch(IOException e) {
        System.out.println(e);
    }  

然后使用调色板提取颜色

Palette.generateAsync(image, new Palette.PaletteAsyncListener() {
    public void onGenerated(Palette palette) {
        // Do something with colors...
    }
});

为此,您需要实现一个依赖性

dependencies {
    ...
    compile 'com.android.support:palette-v7:21.0.+'
}  

答案 2

推荐