簡體   English   中英

如何在下次clickevents上顯示android bitmapdrawable更改

[英]how to show android bitmapdrawable change on next clickevents

當用戶輸入文本時,在下次單擊時,圖像上的先前文本消失了。 如何在所有單擊事件上將所有文本輸入保存在可繪制的位圖上。 請提出建議。

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上。 每次以前的文本被最新的替換?

每次調用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); 保持原樣。 如果要將新文本放在現有文本下,則需要控制自己。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM