2

在我的一个应用程序中,我需要从数据库中读取文本并将其显示给用户。为此,我想到了使用 Coverflow。那么这里的任何人都可以告诉我,是否可以使用 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;

        }

    }
}
4

1 回答 1

1

我一直在做和你类似的事情。我意识到你是在你的形象之外画画。

要解决此问题,只需将“基线”更改为“250”,您就会看到您的文本为图像。

例如:

改变

canvas.drawText(text, 0, baseline, paint);

canvas.drawText(text, 0, 250, paint);

希望这对你有帮助

于 2013-01-28T06:14:25.417 回答