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

nid*_*eba 17 java android android-fragments android-activity android-recyclerview

我正在尝试制作一个Activity将在我的应用程序中而不是在android设备中保存所有最近屏幕的工具。其想法是像在android中一样创建最近视图。我暂时不知道该如何做。我在android的官方网站中进行了搜索,但没有得到我想要的。我有没有被宣布的一些活动WebView,我想采取title, url and the view的是Url和保存在显示VisualHistory。我已经能够完成设计,但是不知道如何显示最近屏幕或视图。背景颜色应基于网址。如果有人不懂,请告诉我。 在下面,您可以找到一张照片,该照片将显示我的工作意图,这是我从其他应用复制了这张照片。 我已计划按照以下步骤实现这一目标。在Pojo.class和中db未定义image将在中显示的内容,cardview因为我不知道如何实现。我希望目前蓝色的背景取决于网址颜色。有没有办法获取网址的颜色?

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

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

[VisualHistoryItem {id = 109,title ='维基百科,免费百科全书',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 – 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);
Run Code Online (Sandbox Code Playgroud)

我在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();

    }
}
Run Code Online (Sandbox Code Playgroud)

这就是XML为此Activity

<?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>
Run Code Online (Sandbox Code Playgroud)

这是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()));
        }
    };
}
Run Code Online (Sandbox Code Playgroud)

这是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"/>
Run Code Online (Sandbox Code Playgroud)

这是 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();
}
Run Code Online (Sandbox Code Playgroud)

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);
}
Run Code Online (Sandbox Code Playgroud)

这是 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);
}
}
Run Code Online (Sandbox Code Playgroud)

这就是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;
    

Van*_*aga 4

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

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

在您的活动中使用它将其保存到 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();
                }
Run Code Online (Sandbox Code Playgroud)

不要使用名称“bitmap.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*/
        }  
Run Code Online (Sandbox Code Playgroud)

就获取颜色(你说链接不起作用)而言,你可以首先使用位图

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

然后使用调色板提取颜色

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

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

dependencies {
    ...
    compile 'com.android.support:palette-v7:21.0.+'
}  
Run Code Online (Sandbox Code Playgroud)