在我的一個應用程序中,我需要從數據庫中讀取文本並將其顯示給用戶。我想過使用Coverflow來達到這個目的。那麼任何人都可以讓我知道,是否可以使用CoverFlow而不是圖像顯示文本?我正在嘗試生成與this類似的輸出。如所建議的那樣,我將文本轉換爲位圖圖像並嘗試顯示它。但我得到了空白屏幕。請看我的代碼如下Android CoverFlow顯示文字而不是圖像
public class CoverFlowDemoActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
CoverFlow coverFlow;
coverFlow = new CoverFlow(this);
coverFlow.setAdapter(new ImageAdapter(this));
ImageAdapter coverImageAdapter = new ImageAdapter(this);
coverFlow.setAdapter(coverImageAdapter);
coverImageAdapter.populateBitmapArray();
coverFlow.setSpacing(-25);
coverFlow.setSelection(4, true);
coverFlow.setAnimationDuration(1000);
setContentView(coverFlow);
}
public class ImageAdapter extends BaseAdapter {
int mGalleryItemBackground;
private Context mContext;
private Bitmap[] bitmapImages;
public ImageAdapter(Context c) {
mContext = c;
bitmapImages = new Bitmap[9];
}
public void populateBitmapArray() {
for (int i = 0; i < 9; i++) {
Bitmap originalImage = textAsBitmap("Example Test", 50f,
R.color.red);
bitmapImages[i] = originalImage;
}
}
public int getCount() {
return 9;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
// Use this code if you want to load from resources
ImageView i = new ImageView(mContext);
i.setImageBitmap(bitmapImages[position]);
i.setLayoutParams(new CoverFlow.LayoutParams(130, 130));
i.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
// Make sure we set anti-aliasing otherwise we get jaggies
BitmapDrawable drawable = (BitmapDrawable) i.getDrawable();
drawable.setAntiAlias(true);
return i;
// return mImages[position];
}
/**
* Returns the size (0.0f to 1.0f) of the views depending on the
* 'offset' to the center.
*/
public float getScale(boolean focused, int offset) {
/* Formula: 1/(2^offset) */
return Math.max(0, 1.0f/(float) Math.pow(2, Math.abs(offset)));
}
public Bitmap textAsBitmap(String text, float largest, int textColor) {
Paint paint = new Paint();
paint.setTextSize(largest);
paint.setColor(textColor);
// int width = (int) (paint.measureText(text) + 0.5f); // round
int width = 500;
float baseline = (int) (paint.ascent() + 0.5f) + 3f;
// int height = (int) ((baseline + paint.descent() + 0.5f) + 3);
int height = 500;
Bitmap image = Bitmap.createBitmap(width, height,
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(image);
canvas.drawText(text, 0, baseline, paint);
return image;
}
}
}
你是在說關於畫廊窗口小部件?如果是這樣,你可以很容易地將你的文本繪製成一個位圖,並顯示它。 (看看Canvas.drawText()) – Renard 2012-04-09 19:02:28
我想你是在討論類似[THIS](http://code.google.com/p/android-coverflow/)的內容,但是你應該清楚你的OP而不是讓我們猜測。有些人不知道'CoverFlow'小部件是什麼。 – adneal 2012-04-09 19:52:08
@aneal是的。我正在嘗試製作與該視頻類似的輸出。 – Allwyn 2012-04-10 04:05:45