当用户输入文本时,在下一次单击时,图像上的上一个文本消失。如何在所有点击事件上保存位图上的所有文本输入。请建议。
onCreate() {
ImageView mView = (ImageView) findViewById(R.id.dstImageView);
Drawable myDrawable = getResources().getDrawable(R.drawable.pic2);
mView.setImageDrawable(myDrawable);
mView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
float x = event.getX();
float y = event.getY();
showNameDialog();
}
}
private void showNameDialog() {
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Photo Tag");
alert.setMessage("Tag Message");
// Set an EditText view to get user input
final EditText input = new EditText(this);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Editable value = input.getText();
// Do something with value!
ImageView mView3 = (ImageView) findViewById(R.id.dstImageView);
myDrawable test= writeTextOnDrawable(R.drawable.pic2,value.toString(),touchxpos,touchypos);
mView3.setImageDrawable(myDrawabletest);
}
});
// write text on drawable
private BitmapDrawable writeTextOnDrawable(int drawableId, String text,float xPos,float yPos)
{
Bitmap bm = BitmapFactory.decodeResource(getResources(), drawableId).copy(Bitmap.Config.ARGB_8888, true);
Typeface tf = Typeface.create("Helvetica", Typeface.BOLD);
Paint paint = new Paint();
paint.setStyle(Style.FILL);
paint.setColor(Color.RED);
paint.setTypeface(tf);
paint.setTextAlign(Align.CENTER);
paint.setTextSize(11);
Rect textRect = new Rect();
paint.getTextBounds(text, 0, text.length(), textRect);
Canvas canvas = new Canvas(bm);
canvas.drawText(text, xPos, yPos, paint);
return new BitmapDrawable(getResources(), bm);
}
如何在每次点击时将文本保存在bitmapdrawable上。每次上一个文本被最新的文本替换?
答案 0 :(得分:0)
每次拨打writeTextOnDrawable()
时,您都会返回一个新的Drawable
,以便前一个被丢弃。您需要有一个函数,您可以将现有Drawable
作为参考传递,如下所示:
private BitmapDrawable writeTextOnDrawable(BitmapDrawable existingDrawable, int drawableId, String text,float xPos,float yPos)
{
Bitmap bm;
if (existingDrawable == null) {
bm = BitmapFactory.decodeResource(getResources(), drawableId).copy(Bitmap.Config.ARGB_8888, true);
} else {
bm = existingDrawable.getBitmap();
}
Typeface tf = Typeface.create("Helvetica", Typeface.BOLD);
Paint paint = new Paint();
paint.setStyle(Style.FILL);
paint.setColor(Color.RED);
paint.setTypeface(tf);
paint.setTextAlign(Align.CENTER);
paint.setTextSize(11);
Rect textRect = new Rect();
paint.getTextBounds(text, 0, text.length(), textRect);
Canvas canvas = new Canvas(bm);
canvas.drawText(text, xPos, yPos, paint);
return new BitmapDrawable(getResources(), bm);
}
但是请注意,这只会在现有文本上写入文本,因为paint.getTextBounds(text, 0, text.length(), textRect);
保持不变。如果您希望将新文本放在现有文本之下,则需要自己控制。