下面列出了android.widget.TextView#draw ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@NonNull
private BitmapDescriptor createClusterIcon(int clusterBucket) {
@SuppressLint("InflateParams")
TextView clusterIconView = (TextView) LayoutInflater.from(mContext)
.inflate(R.layout.map_cluster_icon, null);
clusterIconView.setBackground(createClusterBackground());
clusterIconView.setTextColor(mIconStyle.getClusterTextColor());
clusterIconView.setTextSize(TypedValue.COMPLEX_UNIT_PX,
mIconStyle.getClusterTextSize());
clusterIconView.setText(getClusterIconText(clusterBucket));
clusterIconView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
clusterIconView.layout(0, 0, clusterIconView.getMeasuredWidth(),
clusterIconView.getMeasuredHeight());
Bitmap iconBitmap = Bitmap.createBitmap(clusterIconView.getMeasuredWidth(),
clusterIconView.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(iconBitmap);
clusterIconView.draw(canvas);
return BitmapDescriptorFactory.fromBitmap(iconBitmap);
}
private static Bitmap captureTextBitmap(TextView textView) {
Drawable background = textView.getBackground();
textView.setBackground(null);
int width = textView.getWidth() - textView.getPaddingLeft() - textView.getPaddingRight();
int height = textView.getHeight() - textView.getPaddingTop() - textView.getPaddingBottom();
if (width == 0 || height == 0) {
return null;
}
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.translate(-textView.getPaddingLeft(), -textView.getPaddingTop());
textView.draw(canvas);
textView.setBackground(background);
return bitmap;
}
private static Bitmap captureTextBitmap(TextView textView) {
Drawable background = textView.getBackground();
textView.setBackground(null);
int width = textView.getWidth() - textView.getPaddingLeft() - textView.getPaddingRight();
int height = textView.getHeight() - textView.getPaddingTop() - textView.getPaddingBottom();
if (width == 0 || height == 0) {
return null;
}
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.translate(-textView.getPaddingLeft(), -textView.getPaddingTop());
textView.draw(canvas);
textView.setBackground(background);
return bitmap;
}
private static Bitmap captureTextBitmap(TextView textView) {
Drawable background = textView.getBackground();
textView.setBackground(null);
int width = textView.getWidth() - textView.getPaddingLeft() - textView.getPaddingRight();
int height = textView.getHeight() - textView.getPaddingTop() - textView.getPaddingBottom();
if (width == 0 || height == 0) {
return null;
}
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.translate(-textView.getPaddingLeft(), -textView.getPaddingTop());
textView.draw(canvas);
textView.setBackground(background);
return bitmap;
}
private static Bitmap captureTextBitmap(TextView textView) {
Drawable background = textView.getBackground();
textView.setBackground(null);
int width = textView.getWidth() - textView.getPaddingLeft() - textView.getPaddingRight();
int height = textView.getHeight() - textView.getPaddingTop() - textView.getPaddingBottom();
if (width == 0 || height == 0) {
return null;
}
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.translate(-textView.getPaddingLeft(), -textView.getPaddingTop());
textView.draw(canvas);
textView.setBackground(background);
return bitmap;
}
/** OP's original implementation fixed for real centering */
public static Bitmap imageWithText(Context context, Bitmap bitmap, GenerateParams params) {
TextView view = new TextView(context);
view.setText(params.text);
view.setTextColor(params.color);
view.setBackgroundColor(params.background);
view.setTypeface(null, Typeface.BOLD);
view.setGravity(Gravity.CENTER);
view.setTextSize(20);
Canvas canvas = new Canvas(bitmap);
view.measure(makeMeasureSpec(canvas.getWidth(), EXACTLY), makeMeasureSpec(canvas.getHeight(), EXACTLY));
view.layout(0, 0, canvas.getWidth(), canvas.getHeight());
view.draw(canvas);
return bitmap;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final FrameLayout root = new FrameLayout(this);
setContentView(root);
final TextView textView = new TextView(this);
textView.setText("test");
textView.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER));
textView.setTextColor(Color.WHITE);
textView.setTextSize(40);
textView.setGravity(Gravity.CENTER_HORIZONTAL);
textView.setBackgroundColor(Color.RED);
textView.setPadding(100, 20, 100, 20);
final int measureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
textView.measure(measureSpec, measureSpec);
textView.layout(0, 0, textView.getMeasuredWidth(), textView.getMeasuredHeight());
final Bitmap b = Bitmap.createBitmap(textView.getWidth(), textView.getHeight(), Bitmap.Config.ARGB_8888);
final Canvas c = new Canvas(b);
textView.draw(c);
final ImageView imageView = new ImageView(this);
imageView.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER));
imageView.setImageBitmap(createReflection(b, 1, 10));
root.addView(imageView);
}
private int findLineFor(String string) {
int line = findExistingString(string);
if (line >= 0) {
return line;
}
int width = (int) Math.ceil(computeWidth(string) + 25);
renderer = new TextView(context.getAndroidContext());
renderer.setTextColor(Color.WHITE);
renderer.setSingleLine(true);
renderer.setTextSize(TypedValue.COMPLEX_UNIT_PX, getScaledSize());
renderer.setText(string);
int firstLine = findLineToUse();
// System.out.println("string cache miss for " + string +
// ", allocating new line: " + firstLine);
int lastLine = firstLine;
for (int x = 0; x < width; x += TEXTURE_WIDTH) {
if (x == 0) {
line = firstLine;
} else {
line = findLineToUse();
nextTile[lastLine] = line;
linestrings[line] = null;
linewidths[line] = -1;
}
// important to not allow cycles.
lastused[line] = Integer.MAX_VALUE;
// just to be sure.
nextTile[line] = -1;
// render the new text to that line.
Bitmap bitmap = Bitmap.createBitmap(TEXTURE_WIDTH, lineheight, Bitmap.Config.ALPHA_8);
Canvas canvas = new Canvas(bitmap);
renderer.layout(0, 0, width, lineheight);
canvas.translate(-x, 0);
renderer.draw(canvas);
// canvas.translate(50, .8f * lineheight);
int points = lineheight * TEXTURE_WIDTH;
ByteBuffer alpha8 = ByteBuffer.allocateDirect(points);
bitmap.copyPixelsToBuffer(alpha8);
ByteBuffer updateBuffer;
if(context instanceof GLES20DrawContext) {
updateBuffer = ByteBuffer.allocateDirect(points*4);
for(int i = 0;i != points;i++) {
updateBuffer.putInt(0xFFFFFF00|alpha8.get(i));
}
} else {
updateBuffer = alpha8;
}
updateBuffer.rewind();
context.updateFontTexture(texture, 0, line*lineheight, TEXTURE_WIDTH, lineheight, updateBuffer);
lastLine = line;
}
lastused[firstLine] = lastUsedCount++;
linestrings[firstLine] = string;
linewidths[firstLine] = width;
checkInvariants();
return firstLine;
}