我正在構建一個Android應用程序,它將像電子書一樣。標準翻頁,查看圖像等。其中一個要求是,在多個位置,您將看到頁面外觀的預覽。這將是該頁面的確切內容,只是體積較小。Android - 顯示預覽活動/視圖的內容
我不能爲我的生活得到這個預覽工作的方式,我需要它。我編寫了一個活動,該活動將顯示控件以播放音頻或視頻,具體取決於傳入的其他值。這工作正常。預覽應該是此活動,但只能以較小的尺寸顯示,並在單獨的活動中顯示。以下是我試圖獲得的較小版本的活動顯示:
首先,我偶然發現了記錄不佳的ActivityGroup對象。我想我可以使用它來調用活動,然後獲取視圖並以某種方式將其投影到父視圖中。下面是看起來像什麼代碼:
public class GroupTest extends ActivityGroup {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grouptest);
LocalActivityManager manager = getLocalActivityManager();
Intent intent = new Intent();
intent.setClass(getBaseContext(), ViewElement.class);
intent.putExtra("ElementID", 22);
intent.putExtra("PackID", 10);
Window w = manager.startActivity("videoelement", intent);
View v = w.getDecorView();
// Grab the table and add a view to it.
TableLayout tl = (TableLayout)findViewById(R.id.tlTest);
TableRow tr = new TableRow(this);
tr.addView(v);
tl.addView(tr);
}
}
這裏就是佈局看起來很喜歡:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1"
android:id="@+id/tlTest">
</TableLayout>
這種類型的工作。活動ViewElement確實顯示在GroupTest活動中。問題在於,無論何時我試圖向表中添加其他內容(如標籤),ViewElement活動都會消失。我修了一會兒,發現我沒有掌握一些UI約束,然後意識到顯示的ViewElement活動有可點擊的按鈕,這不是我真正想要的。我只是想看看那個頁面看起來像什麼,不能與它進行交互。
所以我嘗試了不同的方法。找到此頁面保存圖像的視圖:http://jtribe.blogspot.com/2008/12/saving-view-as-image-with-android.html。聽起來更像我想要的。
我修改後的代碼現在看起來像:
public class GroupTest extends ActivityGroup {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tocrow);
LocalActivityManager manager = getLocalActivityManager();
Intent intent = new Intent();
intent.setClass(getBaseContext(), ViewElement.class);
intent.putExtra("ElementID", 22);
intent.putExtra("PackID", 10);
Window w = manager.startActivity("videoelement", intent);
View v = w.getDecorView();
Bitmap image = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.RGB_565);
v.draw(new Canvas(image));
ImageView img = (ImageView)findViewById(R.id.ivwElementPreview);
img.setImageBitmap(image);
}
}
而且佈局(這是實際的佈局,我將投入如果我能得到它的工作的預覽):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip">
<ImageView
android:id="@+id/ivwElementPreview"
android:layout_width="75px"
android:layout_height="75px"
android:layout_marginRight="6dip"
android:src="@drawable/icon" />
<LinearLayout
android:orientation="vertical"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="fill_parent">
<TextView
android:id="@+id/txtElementTitle"
android:layout_width="300px"
android:layout_height="0dip"
android:layout_weight="1"
/>
<ImageView
android:id="@+id/ivwPackIcon"
android:layout_width="15px"
android:layout_height="15px"
android:layout_marginRight="6dip"
android:src="@drawable/icon" />
<ImageView
android:id="@+id/ivwPackIcon"
android:layout_width="15px"
android:layout_height="15px"
android:layout_marginRight="6dip"
android:src="@drawable/icon" />
<TextView
android:id="@+id/txtElementComments"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
/>
</LinearLayout>
</LinearLayout>
這只是扁平化根本不起作用。據我可以告訴調用w.getDecorView()不返回一個實際上有任何高度,寬度或繪製屬性的視圖。如果我運行該代碼,我得到的錯誤「寬度和高度必須> 0」如果我把寬度和高度的值,沒有什麼表現出黑色的圖像。於是我開始創建諸如TextView之類的簡單東西,並將其繪製到位圖中以顯示在我的LinearLayout中,但無法使其正常工作。我只是完全厭倦了在Android中操縱UI。
那麼,有沒有其他人做類似的預覽另一個活動會是什麼樣子,你是如何做到的?我是否在這裏完全錯誤的道路上走?我已經在各地搜索了一些東西,我覺得有些人以前必須解決這個問題,但是我已經失去了一整天的工作,現在我無法將更多時間投入其中。
感謝您的任何幫助。