在我的一个应用程序中,我需要从数据库中读取文本并将其显示给用户。我想到将Coverflow用于此目的。那么这里的任何人都可以让我知道,是否可以使用CoverFlow而不是图像显示文本?我正在尝试生成与this类似的输出。如建议的那样,我将文本转换为位图图像并尝试显示它。但我得到了空白屏幕。请参阅下面的代码
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;
}
}
}
答案 0 :(得分:1)
我一直在做与你类似的事情。而且我意识到你正在画出你的形象。
要解决此问题,只需将“基线”更改为“250”,您就会将文字视为图像。
e.g:
改变
canvas.drawText(text, 0, baseline, paint);
要
canvas.drawText(text, 0, 250, paint);
希望这会对你有所帮助