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); 
} 

如何保存每次點擊的位圖可拖動文本。每次前面的文字都被最新的文字取代?

回答

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);保持不變。如果您希望將新文本置於現有文本之下,則需要自行控制。

+0

你可以建議如何在我的活動類中定義一個existingDrawable,假設我有res ID?我正在尋找示例實現。 – andromania

+0

你已經擁有了。這是您的'myDrawable',您可以將其作爲參數傳遞給我共享的函數。 – Anyonymous2324

+0

你是這個意思嗎? myDrawabletest = writeTextOnDrawable((BitmapDrawable)myDrawable,R.drawable.pic2,value.toString(),touchxpos,touchypos);問題是myDrawable的類型是Drawable而不是BitmapDrawable,所以它失敗了。類型鑄造沒有幫助。 – andromania

相關問題