0
當用戶輸入文本時,下一次單擊時,圖像上的上一個文本消失。如何在所有點擊事件上保存可繪製的位圖上的所有文本輸入。請建議。如何在下一個clickevents上顯示android bitmapdrawable更改
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);
}
如何保存每次點擊的位圖可拖動文本。每次前面的文字都被最新的文字取代?
你可以建議如何在我的活動類中定義一個existingDrawable,假設我有res ID?我正在尋找示例實現。 – andromania
你已經擁有了。這是您的'myDrawable',您可以將其作爲參數傳遞給我共享的函數。 – Anyonymous2324
你是這個意思嗎? myDrawabletest = writeTextOnDrawable((BitmapDrawable)myDrawable,R.drawable.pic2,value.toString(),touchxpos,touchypos);問題是myDrawable的類型是Drawable而不是BitmapDrawable,所以它失敗了。類型鑄造沒有幫助。 – andromania