gpt4 book ai didi

Android CoverFlow 显示文本而不是图像

转载 作者:行者123 更新时间:2023-11-29 02:05:53 29 4
gpt4 key购买 nike

在我的一个应用程序中,我需要从数据库中读取文本并将其显示给用户。为此,我想到了使用 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;

}

}
}

最佳答案

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

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

例如:

改变

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

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

希望对你有所帮助

关于Android CoverFlow 显示文本而不是图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10078068/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com